FieldDefinitionListener.php in Drupal 8
File
core/lib/Drupal/Core/Field/FieldDefinitionListener.php
View source
<?php
namespace Drupal\Core\Field;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
class FieldDefinitionListener implements FieldDefinitionListenerInterface {
protected $entityTypeManager;
protected $keyValueFactory;
protected $cacheBackend;
protected $entityFieldManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, KeyValueFactoryInterface $key_value_factory, CacheBackendInterface $cache_backend) {
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->keyValueFactory = $key_value_factory;
$this->cacheBackend = $cache_backend;
}
public function onFieldDefinitionCreate(FieldDefinitionInterface $field_definition) {
$entity_type_id = $field_definition
->getTargetEntityTypeId();
$bundle = $field_definition
->getTargetBundle();
$field_name = $field_definition
->getName();
$this->entityTypeManager
->getStorage($entity_type_id)
->onFieldDefinitionCreate($field_definition);
$bundle_field_map = $this->keyValueFactory
->get('entity.definitions.bundle_field_map')
->get($entity_type_id);
if (!isset($bundle_field_map[$field_name])) {
$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);
$this->cacheBackend
->delete('entity_field_map');
if ($field_map = $this->entityFieldManager
->getFieldMap()) {
if (!isset($field_map[$entity_type_id][$field_name])) {
$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);
}
}
public function onFieldDefinitionUpdate(FieldDefinitionInterface $field_definition, FieldDefinitionInterface $original) {
$this->entityTypeManager
->getStorage($field_definition
->getTargetEntityTypeId())
->onFieldDefinitionUpdate($field_definition, $original);
}
public function onFieldDefinitionDelete(FieldDefinitionInterface $field_definition) {
$entity_type_id = $field_definition
->getTargetEntityTypeId();
$bundle = $field_definition
->getTargetBundle();
$field_name = $field_definition
->getName();
$this->entityTypeManager
->getStorage($entity_type_id)
->onFieldDefinitionDelete($field_definition);
$bundle_field_map = $this->keyValueFactory
->get('entity.definitions.bundle_field_map')
->get($entity_type_id);
unset($bundle_field_map[$field_name]['bundles'][$bundle]);
if (empty($bundle_field_map[$field_name]['bundles'])) {
unset($bundle_field_map[$field_name]);
}
$this->keyValueFactory
->get('entity.definitions.bundle_field_map')
->set($entity_type_id, $bundle_field_map);
$this->cacheBackend
->delete('entity_field_map');
if ($field_map = $this->entityFieldManager
->getFieldMap()) {
unset($field_map[$entity_type_id][$field_name]['bundles'][$bundle]);
if (empty($field_map[$entity_type_id][$field_name]['bundles'])) {
unset($field_map[$entity_type_id][$field_name]);
}
$this->entityFieldManager
->setFieldMap($field_map);
}
}
}