You are here

public function RealisticDummyContentFieldModifier::addModifier in Realistic Dummy Content 3.x

Same name and namespace in other branches
  1. 8.2 api/src/includes/RealisticDummyContentFieldModifier.php \Drupal\realistic_dummy_content_api\includes\RealisticDummyContentFieldModifier::addModifier()
  2. 7.2 api/src/includes/RealisticDummyContentFieldModifier.php \Drupal\realistic_dummy_content_api\includes\RealisticDummyContentFieldModifier::addModifier()

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

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

string $type: Either 'property' or 'field'.

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

2 calls to RealisticDummyContentFieldModifier::addModifier()
RealisticDummyContentFieldModifier::getFields in api/src/includes/RealisticDummyContentFieldModifier.php
Get fields for the entity, for example body or field_image.
RealisticDummyContentFieldModifier::getProperties in api/src/includes/RealisticDummyContentFieldModifier.php
Get properties for the entity, for example user's picture or node's name.

File

api/src/includes/RealisticDummyContentFieldModifier.php, line 105

Class

RealisticDummyContentFieldModifier
Field modifier class.

Namespace

Drupal\realistic_dummy_content_api\includes

Code

public function addModifier(array &$modifiers, $type, $name) {
  $class = '';
  switch ($type) {
    case 'property':
      $original_class = '\\Drupal\\realistic_dummy_content_api\\includes\\RealisticDummyContentTextProperty';
      $attribute_type = $name;
      break;
    case 'field':
      $original_class = '\\Drupal\\realistic_dummy_content_api\\includes\\RealisticDummyContentValueField';
      $field_info = Framework::instance()
        ->fieldInfoField($name);
      $attribute_type = $field_info['type'];
      break;
    default:
      return;
  }
  $class = $original_class;
  $info = [
    'type' => $type,
    'machine_name' => $attribute_type,
    'entity' => $this
      ->getEntity(),
    'field_name' => $name,
  ];
  Framework::instance()
    ->alter('realistic_dummy_content_attribute_manipulator', $class, $info);

  // PHPStan complains that "Negated boolean expression is always false.",
  // however it is technically possible for an alter hook to set the class
  // to NULL or something.
  // @phpstan-ignore-next-line
  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 {

    // @phpstan-ignore-next-line
    \Drupal::logger('realistic_dummy_content_api')
      ->notice('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.', [
      '@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;
  }
}