You are here

function FieldModifier::AddModifier in Realistic Dummy Content 8

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 Attribute, to which new modifiers will be added.

$type: Either 'property' or 'field_config'

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

Throws

Exception

1 call to FieldModifier::AddModifier()
FieldModifier::GetFields in api/src/manipulators/FieldModifier.php
Get fields for the entity, for example body or field_image.

File

api/src/manipulators/FieldModifier.php, line 177
Define autoload class.

Class

FieldModifier
Field modifier class.

Namespace

Drupal\realistic_dummy_content_api\manipulators

Code

function AddModifier(&$modifiers, $type, $name) {
  $class = '';
  if (!$name) {
    throw new Exception('Name must not be empty');
  }
  if (!is_string($name)) {
    throw new Exception('Name must be a string');
  }
  $info = $this
    ->getBaseInfo($type, $name);
  $original_class = $info['original_class'];
  $attribute_type = $info['attribute_type'];
  $class = $original_class;
  \Drupal::moduleHandler()
    ->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 {
    \Drupal::logger('realistic_dummy_content_api')
      ->notice(t('Class does not exist: @c. This might be because a third-party module has implemented realistic_dummy_content_api_realistic_dummy_content_attribute_manipular_alter() with a class that cannot be implemented, or which is not fully qualified with its namespace. @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;
  }
}