Hey there. Currently I'm working on certain task for CRM system and it needs name field integration with devel generate module.

I've spend a lit bit of time creating this patch which makes Devel module generate names properly.

Comments

cr0ss’s picture

StatusFileSize
new2.89 KB

Patch attached.

alan d.’s picture

Status: Needs review » Needs work

Hi Cr0ss,

Thanks, it would be a good feature. I wonder about the name list through [some words are definitely not fit for the general public, even if I personally consider these fairly tame], maybe a list of say the top 100 people of the 20th century or something may be better, returning the entire component array for each person. An override would allow users to define their own lists.

$func = variable_get('name_devel_generate_names', 'famous_names');
$names =$func();

// Can not remember if filtered..
$components = array_filter($field['settings']['components']);
$rand_index  = array_rand($names);
return array_intersect_assoc($names[$rand_index], $components);

function famous_names() {
  return array(
    array(
      'title' => 'Pope',
      'given' => 'John',
      'middle' => 'Paul',
      'family' => '',
      'generational' => 'II',
      'credentials' => '',
    ),
    .....
  );
}
cr0ss’s picture

Totally agree Alan D., I'm starting work on implementation.

cr0ss’s picture

Status: Needs work » Needs review
StatusFileSize
new5.05 KB

Here is a new version of patch with list of most famous Names, Surnames and Middle names generated just like you've described above.

Is there anything more we can enhance for now?

alan d.’s picture

Looks great. I've made the following change:

/**
 * Helper function to generate set of fake names.
 *
 * @param field
 *   A name field to generate names from.
 */
function _name_devel_generate_name($field) {
  $names = &drupal_static(__FUNCTION__, array());

  // Generate 50 random names based off the field settings. These are stored
  // for future use to prevent the need to regenerate these.
  if (empty($names)) {
    module_load_include('inc', 'name', 'includes/name.content');

    // Parse the settings to find the field title and generational options.
    $titles = _name_field_get_options($field, 'title');
    unset($titles['']);
    $generational = _name_field_get_options($field, 'generational');
    unset($generational['']);

    $given = array('John', ..., 'Dora');
    $middle = array('Aaron', ..., 'Bree');
    $family = array('Smith', ..., 'Hayes');

    // Random use the components to create truly random names.
    for ($i = 0; $i < 50; $i++) {
      $names[] = array(
        'title' => $titles[array_rand($titles)],
        'given' => $given[array_rand($given)],
        'middle' => $middle[array_rand($middle)],
        'family' => $family[array_rand($family)],
        'generational' => $generational[array_rand($generational)],
        'credentials' => ucfirst(devel_generate_word(rand(3, 10))),
      );
    }
  }
  return $names;
}

This pulls out the name options correctly, taking the potential "[vocabulary:xx]" and "-- Empty option" into account. Since the components are keyed, the double array_flip wasn't needed. Finally, for bigger bulk generation operations, the performance should be a bit better, albeit this was probably insignificant to the entity system overhead :)

Committed and pushed to dev.

Thanks!

alan d.’s picture

Status: Needs review » Fixed

I've got a local typo to push sometimes, but I think that we can close this for the time being. Let me know if you want any other changes.

A small enhancement would be a form alter on the devel settings page to allow users to select the devel generate function and to supply a few fun callback options, like your original, maybe a Disney one or another "cute" one for those so inclined... Easy commit credits if you feel like it :)

Right, back to work. Porting ImageField Extended after ruling out File Entities and Field Collections for abig project that we are working on.

cr0ss’s picture

As for me it's awesome for now! Thank you so much for commit. Going further on my work too to get next changes list.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

xcf33’s picture

Assigned: cr0ss » Unassigned
Status: Closed (fixed) » Needs review
StatusFileSize
new484 bytes

Hi sorry to being it up,

It seems that the current 7.x branch of the devel generate is not working due to the use of a wrong function, I've included a patch.

In the previous patch it had which looks like it worked but it's not in the current file

// in _name_devel_generate
return array_flip(array_intersect(array_flip($names[$rand_index]), $components));

I believe we should simply just use array_intersect_key

Thanks

rosk0’s picture

Status: Needs review » Reviewed & tested by the community

Last patch works well for me.
I would be bold and set this as RTBC.

traviscarden’s picture

Confirmed. That does the trick!

traviscarden’s picture

Status: Reviewed & tested by the community » Needs work

Oh... but it doesn't seem to work with taxonomy terms. The hook implementation doesn't even fire. I don't have time to look further into it at the moment.

rosk0’s picture

devel_genarate has a bug, because of this no fields are filed when generating terms.
I provided patch for this in this thread http://drupal.org/node/1589166

rosk0’s picture

Status: Needs work » Reviewed & tested by the community
alan d.’s picture

Status: Reviewed & tested by the community » Fixed

Pushed to dev. Thanks everybody!

rosk0’s picture

At last!
Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.