Allowing the function responsible for specific field generation to be alterable would make replacing the default generated field content quite easy, and would actually solve some other problems for devel_generate, e.g:

#1228470: Support for alternate dictionaries (instead of the default greeking)
#1238344: Allow other modules to generate images for devel_generate
(probably others)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

duellj’s picture

Status: Needs review » Active
FileSize
814 bytes

And here's a patch. Basically it allows modules to override the devel generate function based any number of criteria, including field module, field type, bundle, or object properties. Simple patch, but would open up a whole host of possibilities with devel_generate.

duellj’s picture

Status: Active » Needs review

And here is a simple implementation of hook_devel_generate_field_function_alter() that generates very simple text for all text fields.


/**
 * Implements hook_devel_generate_fields_function_alter().
 */
function simpletext_devel_generate_fields_function_alter(&$function, $module) {
  if ($module == 'text') {
    $function = 'simpletext_devel_generate_text';
  }
}

function simpletext_devel_generate_text($object, $field, $instance, $bundle) {
  if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
    return devel_generate_multiple('_simpletext_devel_generate_text', $object, $field, $instance, $bundle);
  }
  else {
    return _simpletext_devel_generate_text($object, $field, $instance, $bundle);
  }
}

function _simpletext_devel_generate_text($object, $field, $instance, $bundle) {
  $object_field = array(
    'value' => 'This is a simple text field.',
    'format' => 'plain_text',
  );
  return $object_field;
}
 
salvis’s picture

Status: Active » Needs work

I like the idea, but...

+++ b/devel_generate/devel_generate.fields.inc
@@ -47,6 +47,10 @@ function devel_generate_fields(&$object, $obj_type, $bundle) {
+      drupal_alter('devel_generate_fields_function', $function, $module, $object, $field, $instance, $bundle);

... drupal_alter() takes only 4 arguments. You need to wrap them into an array.

duellj’s picture

Status: Needs work » Needs review
FileSize
965 bytes

Good call, missed that. Updated patch.

axe312’s picture

Status: Needs review » Reviewed & tested by the community

Is working for me!

here is a fixed implementation example:

/**
 * Implements hook_devel_generate_fields_function_alter().
 */
function simpletext_devel_generate_fields_function_alter(&$function, $context) {
  if ($context['module'] == 'text') {
    $function = 'simpletext_devel_generate_text';
  }
}

function simpletext_devel_generate_text($object, $field, $instance, $bundle) {
  if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
    return devel_generate_multiple('_simpletext_devel_generate_text', $object, $field, $instance, $bundle);
  }
  else {
    return _simpletext_devel_generate_text($object, $field, $instance, $bundle);
  }
}

function _simpletext_devel_generate_text($object, $field, $instance, $bundle) {
  $object_field = array(
    'value' => 'This is a simple text field.',
    'format' => 'plain_text',
  );
  return $object_field;
}
salvis’s picture

Version: 7.x-1.x-dev » 8.x-1.x-dev
Status: Reviewed & tested by the community » Needs review

I still like the idea, but now it's D8-first...

salvis’s picture

pcambra’s picture

Loving this idea, it would be really useful to standardize all the alteration (i.e. for making a pluggable text generation and reuse it in the image provider module).

Needs a reroll to D8 and adding #5 as an example/docs.

pcambra’s picture

Issue tags: +develcontribute

Tagging, needs reroll

pcambra’s picture

Status: Needs review » Needs work
moshe weitzman’s picture

Issue summary: View changes

Can modules provide their own plugins now, and replace devel's? Using an autoloader hack?

pcambra’s picture

We have an example on this for the image generation in the tests modules http://cgit.drupalcode.org/devel/tree/devel_generate/tests/modules/devel...

moshe weitzman’s picture

Status: Needs work » Closed (works as designed)

Field generate functions are now on the Field item classes (e.g. \Drupal\file\Plugin\Field\FieldType\FileItem::generateSampleValue) so you swap those with however you swap plugins. I dunno yet. Anyway, not a devel problem in D8, and not likely changing in prior versions.