2

I'm using Devel's generate content feature to generate Blog/Forum entries for a site I'm working on and funny enough, I get to have a blog post by an anonymous user or forum topic by an anonymous user.

How can I stop Devel from doing this? I've only given Authenticated Users/ Administrators the permission to create a blog post or forum topic but still it generate for anonymous user. How can correct this?

  • An alternative solution that doesn't require custom code (as in the comment below the existing answer) is to create a custom rule (using the Rules module), as detailed in this answer. – Pierre.Vriens Aug 30 '16 at 14:14
2

The Devel module doesn't have options to decide to which users the generated content is assigned, but you can create a custom module to limit the users to which the created content is assigned. You need to:

  • Implement hook_form_alter() to add the extra form fields necessary to select which users accounts should be used in the form generated by devel_generate_content_form()
  • Replace the form submission handler with one that generates the content using the user accounts selected in the form

You can use the existing form submission handler (devel_generate_content_form_submit()) as guideline to build your submission handler.

function devel_generate_content_form_submit($form_id, &$form_state) {
  module_load_include('inc', 'devel_generate', 'devel_generate');
  $form_state['values']['node_types'] = array_filter($form_state['values']['node_types']);
  if ($form_state['values']['num_nodes'] <= 50 && $form_state['values']['max_comments'] <= 10) {
    module_load_include('inc', 'devel_generate');
    devel_generate_content($form_state);
  }
  else {
    module_load_include('inc', 'devel_generate', 'devel_generate_batch');
    devel_generate_batch_content($form_state);
  }
}

Unfortunately, devel_generate_content() doesn't allow to select the user accounts to use as authors of the nodes. This means you need to write all the code yourself. The bright side is that the function that in your module replaces devel_generate_content() is very similar to that function with a single difference: the function to call instead of devel_generate_content_pre_node().

function mymodule_generate_content($form_state) {
  if (!empty($form_state['values']['kill_content'])) {
    devel_generate_content_kill($form_state['values']);
  }

  if (count($form_state['values']['node_types'])) {
    // Generate nodes.
    mymodule_generate_content_pre_node($form_state['values']);
    $start = time();
    for ($i = 1; $i <= $form_state['values']['num_nodes']; $i++) {
      devel_generate_content_add_node($form_state['values']);
      if (function_exists('drush_log') && $i % drush_get_option('feedback', 1000) == 0) {
        $now = time();
        drush_log(dt('Completed !feedback nodes (!rate nodes/min)', array('!feedback' => drush_get_option('feedback', 1000), '!rate' => (drush_get_option('feedback', 1000) * 60) / ($now -$start))), 'ok');
        $start = $now;
      }
    }
  }
}

function mymodule_generate_content_pre_node(&$results) {
  // Get user id.
  $users = mymodule_get_users($results['exclude']);
  $results['users'] = $users;
}

function mymodule_get_users($exclude) {
  $users = array();
  $result = db_query_range("SELECT uid FROM {users} WHERE uid NOT IN :exclude", 0, 50, array(':exclude' => $exclude);
  foreach ($result as $record) {
    $users[] = $record->uid;
  }

  return $users;
}

The code I shown assume you add a form field (exclude) that contains the user ID for the user accounts that should not be assigned as authors of the generated nodes.

Probably, you also need to write the equivalent of devel_generate_batch_content(), which is the other function used to generate content, for your module.

Notice that the Devel generate module is used to generate content for developing/testing purposes. It should not be used to generate content for a production server/web site. That is the reason why there aren't too much options for the content to generate.

  • First of all thanks for the thorough answer. I must admit I'm not comfortable with coding in Drupal and making custom modules is something I've been avoiding like the plague. I don't think I'll have the time to go about doing what you suggested, since the project is late and besides the data is test data not a real one. Thanks again for the answer! Don't have enough reputation to vote it up, sorry. – tsega Aug 18 '12 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.