View source
<?php
namespace Drupal\entity_usage;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class EntityUsageTrackBase extends PluginBase implements EntityUsageTrackInterface, ContainerFactoryPluginInterface {
protected $usageService;
protected $entityTypeManager;
protected $entityFieldManager;
protected $config;
protected $entityRepository;
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;
}
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'));
}
public function defaultConfiguration() {
return [];
}
public function getId() {
return $this->pluginDefinition['id'];
}
public function getLabel() {
return $this->pluginDefinition['label'];
}
public function getDescription() {
return $this->pluginDefinition['description'] ?: '';
}
public function getApplicableFieldTypes() {
return $this->pluginDefinition['field_types'] ?: [];
}
public function trackOnEntityCreation(EntityInterface $top_entity) {
if (!$this
->isApplicable($top_entity)) {
return;
}
$targets = $this
->getAllBottomLevelTargets($top_entity);
$top_vid = $top_entity instanceof RevisionableInterface && $top_entity
->getRevisionId() ? $top_entity
->getRevisionId() : 0;
foreach ($targets as $target_type_and_id) {
list($target_type, $target_id) = explode("|", $target_type_and_id);
$this->usageService
->registerUsage($target_id, $target_type, $top_entity
->id(), $top_entity
->getEntityTypeId(), $top_entity
->language()
->getId(), $top_vid);
}
}
public function trackOnEntityUpdate(EntityInterface $top_entity, EntityInterface $original) {
if (!$this
->isApplicable($top_entity)) {
return;
}
$top_vid = $top_entity instanceof RevisionableInterface && $top_entity
->getRevisionId() ? $top_entity
->getRevisionId() : 0;
$current_targets = $this
->getAllBottomLevelTargets($top_entity);
if ($original instanceof RevisionableInterface && $top_vid > $original
->getRevisionId()) {
foreach ($current_targets as $target_type_and_id) {
list($target_type, $target_id) = explode('|', $target_type_and_id);
$this->usageService
->registerUsage($target_id, $target_type, $top_entity
->id(), $top_entity
->getEntityTypeId(), $top_entity
->language()
->getId(), $top_vid);
}
}
else {
$original_targets = $this
->getAllBottomLevelTargets($original);
$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);
$this->usageService
->registerUsage($target_id, $target_type, $top_entity
->id(), $top_entity
->getEntityTypeId(), $top_entity
->language()
->getId(), $top_vid);
}
foreach ($removed_ids as $removed_entity) {
list($target_type, $target_id) = explode('|', $removed_entity);
$this->usageService
->deleteUsage($target_id, $target_type, $top_entity
->id(), $top_entity
->getEntityTypeId(), $top_entity
->language()
->getId(), $top_vid);
}
}
}
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;
}
protected function getAllBottomLevelTargets(EntityInterface $entity, array $targets = []) {
if (!$entity instanceof FieldableEntityInterface) {
return $targets;
}
$trackable_field_types = $this
->getApplicableFieldTypes();
$fields = array_keys($this
->getReferencingFields($entity, $trackable_field_types));
foreach ($fields as $field_name) {
if ($entity
->hasField($field_name) && !$entity->{$field_name}
->isEmpty()) {
foreach ($entity->{$field_name} as $field_item) {
$target_entities = $this
->getTargetEntities($field_item);
foreach ($target_entities as $target_type_and_id) {
if (substr_count($target_type_and_id, '|') === 2) {
list($target_type, , $target_revision_id) = explode("|", $target_type_and_id);
$target_entity = $this->entityTypeManager
->getStorage($target_type)
->loadRevision($target_revision_id);
}
else {
list($target_type, $target_id) = explode("|", $target_type_and_id);
$target_entity = $this->entityTypeManager
->getStorage($target_type)
->load($target_id);
}
if (EntityUsageSourceLevel::isBottomLevel($target_entity)) {
$targets[] = $target_type_and_id;
}
else {
$targets = array_merge($targets, $this
->getAllBottomLevelTargets($target_entity, $targets));
}
}
}
}
}
return array_unique($targets);
}
protected function isApplicable(EntityInterface $entity) {
if (!$entity instanceof FieldableEntityInterface) {
return FALSE;
}
$trackable_field_types = $this
->getApplicableFieldTypes();
$fields = array_keys($this
->getReferencingFields($entity, $trackable_field_types));
foreach ($fields as $field_name) {
if ($entity
->hasField($field_name)) {
return TRUE;
}
}
return FALSE;
}
}