FieldDefinitionListener::onFieldDefinitionCreate

public FieldDefinitionListener::onFieldDefinitionCreate(FieldDefinitionInterface $field_definition)

Reacts to the creation of a field.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition created.

Overrides FieldDefinitionListenerInterface::onFieldDefinitionCreate

File

core/lib/Drupal/Core/Field/FieldDefinitionListener.php, line 65

Class

FieldDefinitionListener
Reacts to field definition CRUD on behalf of the Entity system.

Namespace

Drupal\Core\Field

Code

public function onFieldDefinitionCreate(FieldDefinitionInterface $field_definition) {
  $entity_type_id = $field_definition->getTargetEntityTypeId();
  $bundle = $field_definition->getTargetBundle();
  $field_name = $field_definition->getName();

  // Notify the storage about the new field.
  $this->entityTypeManager->getStorage($entity_type_id)->onFieldDefinitionCreate($field_definition);

  // Update the bundle field map key value collection, add the new field.
  $bundle_field_map = $this->keyValueFactory->get('entity.definitions.bundle_field_map')->get($entity_type_id);
  if (!isset($bundle_field_map[$field_name])) {
    // This field did not exist yet, initialize it with the type and empty
    // bundle list.
    $bundle_field_map[$field_name] = [
      'type' => $field_definition->getType(),
      'bundles' => [],
    ];
  }
  $bundle_field_map[$field_name]['bundles'][$bundle] = $bundle;
  $this->keyValueFactory->get('entity.definitions.bundle_field_map')->set($entity_type_id, $bundle_field_map);

  // Delete the cache entry.
  $this->cacheBackend->delete('entity_field_map');

  // If the field map is initialized, update it as well, so that calls to it
  // do not have to rebuild it again.
  if ($field_map = $this->entityFieldManager->getFieldMap()) {
    if (!isset($field_map[$entity_type_id][$field_name])) {
      // This field did not exist yet, initialize it with the type and empty
      // bundle list.
      $field_map[$entity_type_id][$field_name] = [
        'type' => $field_definition->getType(),
        'bundles' => [],
      ];
    }
    $field_map[$entity_type_id][$field_name]['bundles'][$bundle] = $bundle;
    $this->entityFieldManager->setFieldMap($field_map);
  }
}
doc_Drupal
2016-10-29 09:11:26
Comments
Leave a Comment

Please login to continue.