Hi there,

we here at wunderkraut (munich) are really into creating good locking and "rich content" demo content. Devel Generate is a nice tool, but the generated content is not presentable to customers.

Because of this, we created a small module, which alters the generated content with our own paragraphs, sentences and images. This was a nice small solution for us, but all configuration is directly done in code and tweaking the code for each project is not the drupal way. Thats why we cannot commit this module in its actual status to the community.

The main functionality of our module is:

  • Defining default values for specific fields and field types
  • Default values could be sentences, paragraphs, words and files
  • Images are loaded from flickr and wiki commons (not coded yet, at the moment images/files are used from a specific folder)
  • More!? Which ideas do you have?

Our idea is, to extend the devel_generate with our functionalitys and creating a configurable admin interface for this. Another possibility would be creating a module, which hooks into the devel_generate functionality and fills the fields with user selected values.

What do you think about this?

Is one of the maintainers tomorrow at the code sprint at drupal con munich? I am really interested in a personal conversation about that.

Greetings :)

Comments

salvis’s picture

We're both here, but devel_generate is moshe's baby, so you should talk to him.

There have been ideas along these lines in the past, some even with code. Please look up these issues:
#1589056: Allow other modules to generate MEDIA FILES for devel_generate
#1010394: Use devel_create_greeking for text field instead of user_password
#1277654: Generate translated content
#1400840: Friendly role based username and passwords for dummy devel generated user accounts
#1539596: only use valid users as authors
#1228470: Support for alternate dictionaries (instead of the default greeking)
#1397582: allow the generation of content types and fields
#1242352: Don't hard code the 50 node threshold for triggering batch node generation
#1326204: Allow field generate function to be alterable
#1254790: Respect fields' maximum length setting
#1297532: drush:generate-placeholder-images: Support image "generation" from the drupal files table
#616160: Extending devel generate

We want to avoid adding dependencies on external websites / data sources, for performance and maintenance reasons.

I like the direction of your push, but we need an answer to the question of who will maintain this new functionality. If this turns out to be unmaintainable/unmaintained, then we need to be able to fall back to what we have now. With that in mind, I favor adding the required hooks to Devel (or implementing dependency injection) and implementing a separate data provider module.

pcambra’s picture

There's already a module for generate images from external sources to devel: Devel images provider

alberto56’s picture

For documentation purposes, here is how a module can modify generated content.

function MYMODULE_node_presave($node) {
  if (isset($node->devel_generate)) {
    switch ($node->type) {
      case 'article':
        // on my site articles are in markdown, and
        // have a few hyperlinks
        $all_matches = array();
        preg_match_all('/[^ \.][^ \.]* [^ \.][^ \.]*/', $node->body[LANGUAGE_NONE][0]['value'], $all_matches);
        $matches = $all_matches[0];
        for ($i = 0; $i < 3; $i++) {
          if (count($matches)) {
            $match = $matches[rand(0, count($matches) - 1)];
            $node->body[LANGUAGE_NONE][0]['value'] = str_replace($match, '[' . $match . '](http://example.com/)', $node->body[LANGUAGE_NONE][0]['value']);
          }
        }
        $node->body[LANGUAGE_NONE][0]['format'] = 'markdown';
        break;

      default:
        // non-articles work fine with devel_geneate defaults.
        break;
    }
  }
  return $node;
}

EDIT: I forgot to add the line "return $node". Now it works.

In my experience, each site has different "typical" content: some of my clients use wysiwyg a lot so typical pages always contain Word-type gibberish. Others like using filtered_html, and generate clean paragraphs; others have custom input formats, using markdown, as in the above example. Also, different content types often have widely different typical content, but again, this differs per project.

I normally create a master module for each of the sites I develop, and put a hook like the above in that module, so that when I generate content for any given site, the generated content corresponds to the site's typical usage.

Because each site is so different, I'm not sure what more devel_generate could do to generate kick-ass content: maybe that's the role of custom modules per project.

Cheers,

Albert.

pcambra’s picture

As suggested by jmolivas, we might want to rely on Faker for generating content: https://github.com/fzaninotto/Faker

alberto56’s picture

@pcambra. thanksI can also imagine some uses for that with another module I'm working on.

alberto56’s picture

An update to #3: if you apply the patch at #2252965: Let third parties know it's generated content and modify it. (terms), you can create a directory with a bunch of faces in sites/all/modules/my_custom_module/user-pics and use them in generated users. The following code does that, and also trims names if they are too long:

/**
 * Generate better dummy content.
 *
 * See https://drupal.org/node/1748302.
 */
function my_custom_module_user_insert(&$edit, $account, $category) {
  // hook_user_insert can be used for fields attached to the user object,
  // but not the picture.
  if (module_exists('devel_generate') && isset($edit['devel_generate']) && is_numeric($edit['uid'])) {
    $account->field_first_name[LANGUAGE_NONE][0]['value'] = drupal_substr($account->field_first_name[LANGUAGE_NONE][0]['value'], 0, rand(10, 20)); 
    $account->field_last_name[LANGUAGE_NONE][0]['value'] = drupal_substr($account->field_last_name[LANGUAGE_NONE][0]['value'], 0, rand(10, 20));
  }
}

/**
 * Generate better dummy content.
 *
 * See https://drupal.org/node/1748302.
 */
function my_custom_module_user_presave(&$edit, $account, $category) {
  // hook_user_presave can be used for the user picture, but not
  // additional fields attached to the user object.
  if (module_exists('devel_generate') && isset($edit['devel_generate']) && is_numeric($edit['uid'])) {
    $filepath = DRUPAL_ROOT . '/' . drupal_get_path('module', 'my_custom_module') . '/devel-generate-images/user/picture';
    $files = array_values(file_scan_directory($filepath, '/.*\.png$/'));
    $file = $files[rand(0, count($files) -1)];
    $contents = file_get_contents($file->uri);
    $random = md5($filepath) . rand(1000000000, 9999999999);
    $file = file_save_data($contents, 'public://user' . $random . '.png');
    $file->uid = $account->uid;
    $file = file_save($file);
    $edit['picture'] = $file;
  }
}
alberto56’s picture

I wrote this module, which aims to provide an extensible system for generating kick-ass dummy content, based on the code above: Realistic Dummy Content. I have been using it for a week now on a real project and it makes a great difference in client demos! Comments welcome.

moshe weitzman’s picture

Status: Active » Closed (duplicate)

As for devel, I think we can focus efforts at #2238645: Use Ipsum module for devel generate