abstract class EntityUsageTrackBase in Entity Usage 8.2
Same name and namespace in other branches
- 8 src/EntityUsageTrackBase.php \Drupal\entity_usage\EntityUsageTrackBase
- 8.3 src/EntityUsageTrackBase.php \Drupal\entity_usage\EntityUsageTrackBase
Base implementation for track plugins.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\entity_usage\EntityUsageTrackBase implements ContainerFactoryPluginInterface, EntityUsageTrackInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of EntityUsageTrackBase
6 files declare their use of EntityUsageTrackBase
- BlockField.php in src/
Plugin/ EntityUsage/ Track/ BlockField.php - DynamicEntityReference.php in src/
Plugin/ EntityUsage/ Track/ DynamicEntityReference.php - EntityReference.php in src/
Plugin/ EntityUsage/ Track/ EntityReference.php - LayoutBuilder.php in src/
Plugin/ EntityUsage/ Track/ LayoutBuilder.php - Link.php in src/
Plugin/ EntityUsage/ Track/ Link.php
File
- src/
EntityUsageTrackBase.php, line 18
Namespace
Drupal\entity_usageView source
abstract class EntityUsageTrackBase extends PluginBase implements EntityUsageTrackInterface, ContainerFactoryPluginInterface {
/**
* The usage tracking service.
*
* @var \Drupal\entity_usage\EntityUsage
*/
protected $usageService;
/**
* Entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Entity field manager service.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The Entity Update config.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* The EntityRepository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Plugin constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\entity_usage\EntityUsage $usage_service
* The usage tracking service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The EntityTypeManager service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The EntityFieldManager service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The EntityRepositoryInterface service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityUsage $usage_service, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory, EntityRepositoryInterface $entity_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configuration += $this
->defaultConfiguration();
$this->usageService = $usage_service;
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->config = $config_factory
->get('entity_usage.settings');
$this->entityRepository = $entity_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_usage.usage'), $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('config.factory'), $container
->get('entity.repository'));
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [];
}
/**
* {@inheritdoc}
*/
public function getId() {
return $this->pluginDefinition['id'];
}
/**
* {@inheritdoc}
*/
public function getLabel() {
return $this->pluginDefinition['label'];
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->pluginDefinition['description'] ?: '';
}
/**
* {@inheritdoc}
*/
public function getApplicableFieldTypes() {
return $this->pluginDefinition['field_types'] ?: [];
}
/**
* {@inheritdoc}
*/
public function trackOnEntityCreation(EntityInterface $source_entity) {
$trackable_field_types = $this
->getApplicableFieldTypes();
$fields = array_keys($this
->getReferencingFields($source_entity, $trackable_field_types));
foreach ($fields as $field_name) {
if ($source_entity
->hasField($field_name) && !$source_entity->{$field_name}
->isEmpty()) {
/** @var \Drupal\Core\Field\FieldItemInterface $field_item */
foreach ($source_entity->{$field_name} as $field_item) {
// The entity is being created with value on this field, so we just
// need to add a tracking record.
$target_entities = $this
->getTargetEntities($field_item);
foreach ($target_entities as $target_entity) {
list($target_type, $target_id) = explode("|", $target_entity);
$source_vid = $source_entity instanceof RevisionableInterface && $source_entity
->getRevisionId() ? $source_entity
->getRevisionId() : 0;
$this->usageService
->registerUsage($target_id, $target_type, $source_entity
->id(), $source_entity
->getEntityTypeId(), $source_entity
->language()
->getId(), $source_vid, $this->pluginId, $field_name);
}
}
}
}
}
/**
* {@inheritdoc}
*/
public function trackOnEntityUpdate(EntityInterface $source_entity) {
$trackable_field_types = $this
->getApplicableFieldTypes();
$fields = array_keys($this
->getReferencingFields($source_entity, $trackable_field_types));
foreach ($fields as $field_name) {
if ($source_entity instanceof RevisionableInterface && $source_entity
->getRevisionId() != $source_entity->original
->getRevisionId() && $source_entity
->hasField($field_name) && !$source_entity->{$field_name}
->isEmpty()) {
$this
->trackOnEntityCreation($source_entity);
return;
}
// We are updating an existing revision, compare target entities to see if
// we need to add or remove tracking records.
$current_targets = [];
if ($source_entity
->hasField($field_name) && !$source_entity->{$field_name}
->isEmpty()) {
foreach ($source_entity->{$field_name} as $field_item) {
$target_entities = $this
->getTargetEntities($field_item);
foreach ($target_entities as $target_entity) {
$current_targets[] = $target_entity;
}
}
}
$original_targets = [];
if ($source_entity->original
->hasField($field_name) && !$source_entity->original->{$field_name}
->isEmpty()) {
foreach ($source_entity->original->{$field_name} as $field_item) {
$target_entities = $this
->getTargetEntities($field_item);
foreach ($target_entities as $target_entity) {
$original_targets[] = $target_entity;
}
}
}
// If a field references the same target entity, we record only one usage.
$original_targets = array_unique($original_targets);
$current_targets = array_unique($current_targets);
$added_ids = array_diff($current_targets, $original_targets);
$removed_ids = array_diff($original_targets, $current_targets);
foreach ($added_ids as $added_entity) {
list($target_type, $target_id) = explode('|', $added_entity);
$source_vid = $source_entity instanceof RevisionableInterface && $source_entity
->getRevisionId() ? $source_entity
->getRevisionId() : 0;
$this->usageService
->registerUsage($target_id, $target_type, $source_entity
->id(), $source_entity
->getEntityTypeId(), $source_entity
->language()
->getId(), $source_vid, $this->pluginId, $field_name);
}
foreach ($removed_ids as $removed_entity) {
list($target_type, $target_id) = explode('|', $removed_entity);
$source_vid = $source_entity instanceof RevisionableInterface && $source_entity
->getRevisionId() ? $source_entity
->getRevisionId() : 0;
$this->usageService
->registerUsage($target_id, $target_type, $source_entity
->id(), $source_entity
->getEntityTypeId(), $source_entity
->language()
->getId(), $source_vid, $this->pluginId, $field_name, 0);
}
}
}
/**
* {@inheritdoc}
*/
public function getReferencingFields(EntityInterface $source_entity, array $field_types) {
$source_entity_type_id = $source_entity
->getEntityTypeId();
$all_fields_on_bundle = $this->entityFieldManager
->getFieldDefinitions($source_entity_type_id, $source_entity
->bundle());
$referencing_fields_on_bundle = [];
foreach ($all_fields_on_bundle as $field_name => $field) {
if (in_array($field
->getType(), $field_types)) {
$referencing_fields_on_bundle[$field_name] = $field;
}
}
if (!$this->config
->get('track_enabled_base_fields')) {
foreach ($referencing_fields_on_bundle as $key => $referencing_field_on_bundle) {
if ($referencing_field_on_bundle
->getFieldStorageDefinition()
->isBaseField()) {
unset($referencing_fields_on_bundle[$key]);
}
}
}
return $referencing_fields_on_bundle;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityUsageTrackBase:: |
protected | property | The Entity Update config. | |
EntityUsageTrackBase:: |
protected | property | Entity field manager service. | |
EntityUsageTrackBase:: |
protected | property | The EntityRepository service. | |
EntityUsageTrackBase:: |
protected | property | Entity type manager service. | |
EntityUsageTrackBase:: |
protected | property | The usage tracking service. | |
EntityUsageTrackBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
2 |
EntityUsageTrackBase:: |
public | function | ||
EntityUsageTrackBase:: |
public | function |
Returns the field types this plugin is capable of tracking. Overrides EntityUsageTrackInterface:: |
|
EntityUsageTrackBase:: |
public | function |
Returns the tracking method description. Overrides EntityUsageTrackInterface:: |
|
EntityUsageTrackBase:: |
public | function |
Returns the tracking method unique id. Overrides EntityUsageTrackInterface:: |
|
EntityUsageTrackBase:: |
public | function |
Returns the tracking method label. Overrides EntityUsageTrackInterface:: |
|
EntityUsageTrackBase:: |
public | function |
Retrieve fields of the given types on an entity. Overrides EntityUsageTrackInterface:: |
|
EntityUsageTrackBase:: |
public | function |
Track usage updates on the creation of entities. Overrides EntityUsageTrackInterface:: |
|
EntityUsageTrackBase:: |
public | function |
Track usage updates on the edition of entities. Overrides EntityUsageTrackInterface:: |
|
EntityUsageTrackBase:: |
public | function |
Plugin constructor. Overrides PluginBase:: |
2 |
EntityUsageTrackInterface:: |
public | function | Retrieve the target entity(ies) from a field item value. | 6 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |