You are here

function RealisticDummyContentFieldModifier::AddModifier in Realistic Dummy Content 7

Adds a modifier to a list of attribute modifiers.

To abstract away the difference between fields and properties, we call them all attributes. Modifiers will modify attributes depending on what they are. For example, a user picture is modified differently than an image in an article. This is managed through an extensible class hierarchy. Modules, including this one, can implement hook_realistic_dummy_content_attribute_manipular_alter() to determine which class should modify which attribute (field or property).

By default, we will consider that properties are text properties and that fields' [value] property should be modified. This is not the case, however for user pictures (which should load a file), body fields (which contain a text format), and others. These are all defined in subclasses and can be extended by module developers.

Parameters

&$modifiers: Existing array of subclasses of RealisticDummyContentAttribute, to which new modifiers will be added.

$type: Either 'property' or 'field'

$name: Name of the property or field, for example 'body', 'picture', 'title', 'field_image'.

2 calls to RealisticDummyContentFieldModifier::AddModifier()
RealisticDummyContentFieldModifier::GetFields in api/includes/RealisticDummyContentEntityFieldModifier.inc
Get fields for the entity, for example body or field_image.
RealisticDummyContentFieldModifier::GetProperties in api/includes/RealisticDummyContentEntityFieldModifier.inc
Get properties for the entity, for example user's picture or node's name.

File

api/includes/RealisticDummyContentEntityFieldModifier.inc, line 103
Define RealisticDummyContentFieldModifier autoload class.

Class

RealisticDummyContentFieldModifier
Field modifier class.

Code

function AddModifier(&$modifiers, $type, $name) {
  $class = '';
  switch ($type) {
    case 'property':
      $original_class = 'RealisticDummyContentTextProperty';
      $attribute_type = $name;
      break;
    case 'field':
      $original_class = 'RealisticDummyContentValueField';
      $field_info = field_info_field($name);
      $attribute_type = $field_info['type'];
      break;
    default:
      return;
      break;
  }
  $class = $original_class;
  drupal_alter('realistic_dummy_content_attribute_manipulator', $class, $type, $attribute_type);
  if (!$class) {

    // third-parties might want to signal that certain fields cannot be
    // modified (they can be too complex for the default modifier and do not yet
    // have a custom modifier).
    return;
  }
  elseif (class_exists($class)) {
    $modifier = new $class($this, $name);
  }
  else {
    watchdog('realistic_dummy_content_api', 'Class does not exist: @c. This is probably because a third-party module has implemented realistic_dummy_content_api_realistic_dummy_content_attribute_manipular_alter() with a class that cannot be implemented. @original will used instead.', array(
      '@c' => $class,
      '@original' => $original_class,
    ));
    $modifier = new $original_class($this, $name);
  }
  if (isset($modifier)) {

    // It's OK to index by name because attributes and fields can never have
    // the same names.
    $modifiers[$name] = $modifier;
  }
}