25 Apr.2016

Drupal 7: Custom Publishing Options and Devel Generate - together forever!

Custom Publishing Options and Devel Generate

In my work, I often use this great module "Custom Publishing Options" together with "devel_generate" module fo generating "Lorem Ipsum" content.

For example, the website has a view and use "custom_pub" option "Push to important content". Assume we already generated 50 nodes and want to test something. And each time I had to manually change advanced (custom) options for the content because "devel_generate" doesn't know anything about "custom_pub".

However, at one point I thought about it and looked at the source code of the module.  It was found that the problem can be solved very easily by using hook_node_presave()

The fist thing I made, created the patch on https://www.drupal.org/node/2713079 (you can use it directly), however for an unknown reason the patch still has status "Need Review" and have been ignored by the author of the module.

So I am writing this tweak and hope it can be helpful for somebody.

File
MYMODULE.module
/**
 * Allow "devel_generate" module to setup "Custom Publishing Options" randomly.
 *
 * Implements hook_node_presave().
 *
 * @param $node
 */
function MYMODULE_node_presave($node) {
  if (module_exists('custom_pub') 
    && module_exists('devel_generate') && isset($node->devel_generate)) {
    $custom_pub_types = custom_pub_by_node_type($node);
 
    if (count($custom_pub_types)) {
      $custom_pub_types = array_keys($custom_pub_types);
 
      foreach ($custom_pub_types as $machine_name) {
        if (isset($node->{$machine_name})) {
          $node->{$machine_name} = mt_rand(0, 1);
        }
      }
    }
  }
}

Ruslan Piskarov

Ukraine
PHP/WEB Developer / Drupal Expert. More than 11 years of experience delivering Drupal based General Purpose solutions for different sectors such as Job Boards, Product Portfolios, Geo Coding, Real Estate solutions, E-Commerce, Classifieds, Corporate and online Magazines/Newspapers.

Tags

Versions