View source
<?php
namespace Drupal\entity_usage\Plugin\EntityUsage\Track;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\entity_usage\EmbedTrackInterface;
use Drupal\entity_usage\EntityUsage;
use Drupal\entity_usage\EntityUsageTrackBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class EmbedBase extends EntityUsageTrackBase implements EmbedTrackInterface {
protected $moduleHandler;
protected $entityRepository;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityUsage $usage_service, ModuleHandlerInterface $module_handler, EntityRepositoryInterface $entity_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $usage_service);
$this->moduleHandler = $module_handler;
$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('module_handler'), $container
->get('entity.repository'));
}
public function trackOnEntityCreation(ContentEntityInterface $entity) {
$referenced_entities_by_field = $this
->getEmbeddedEntitiesByField($entity);
foreach ($referenced_entities_by_field as $field => $embedded_entities) {
foreach ($embedded_entities as $uuid => $type) {
$this
->incrementEmbeddedUsage($entity, $type, $uuid);
}
}
}
public function trackOnEntityUpdate(ContentEntityInterface $entity) {
$translations = [];
$originals = [];
$languages = $entity
->getTranslationLanguages();
foreach ($languages as $langcode => $language) {
if (!$entity
->hasTranslation($langcode)) {
continue;
}
$translations[] = $entity
->getTranslation($langcode);
if (!$entity->original
->hasTranslation($langcode)) {
continue;
}
$originals[] = $entity->original
->getTranslation($langcode);
}
$current_uuids = [];
foreach ($translations as $translation) {
$current_uuids += $this
->getEmbeddedEntitiesByField($translation, TRUE);
}
$original_uuids = [];
foreach ($originals as $original) {
$original_uuids += $this
->getEmbeddedEntitiesByField($original, TRUE);
}
$added_uuids = array_diff_key($current_uuids, $original_uuids);
$removed_uuids = array_diff_key($original_uuids, $current_uuids);
foreach ($added_uuids as $uuid => $type) {
$this
->incrementEmbeddedUsage($entity, $type, $uuid);
}
foreach ($removed_uuids as $uuid => $type) {
$this
->decrementEmbeddedUsage($entity, $type, $uuid);
}
}
public function trackOnEntityDeletion(ContentEntityInterface $entity) {
$translations = [];
if ($entity === $entity
->getUntranslated()) {
$languages = $entity
->getTranslationLanguages();
foreach ($languages as $langcode => $language) {
if (!$entity
->hasTranslation($langcode)) {
continue;
}
$translations[] = $entity
->getTranslation($langcode);
}
}
else {
$translations = [
$entity,
];
}
foreach ($translations as $translation) {
$referenced_entities_by_field = $this
->getEmbeddedEntitiesByField($translation);
foreach ($referenced_entities_by_field as $field => $embedded_entities) {
foreach ($embedded_entities as $uuid => $type) {
$this
->decrementEmbeddedUsage($entity, $type, $uuid);
}
}
}
}
protected function getEmbeddedEntitiesByField(ContentEntityInterface $entity, $omit_field_names = FALSE) {
$entities = [];
if ($this->moduleHandler
->moduleExists('editor')) {
$formatted_text_fields = _editor_get_formatted_text_fields($entity);
foreach ($formatted_text_fields as $formatted_text_field) {
$text = '';
$field_items = $entity
->get($formatted_text_field);
foreach ($field_items as $field_item) {
$text .= $field_item->value;
if ($field_item
->getFieldDefinition()
->getType() == 'text_with_summary') {
$text .= $field_item->summary;
}
}
if ($omit_field_names) {
$entities += $this
->parseEntitiesFromText($text);
}
else {
$entities[$formatted_text_field] = $this
->parseEntitiesFromText($text);
}
}
}
return $entities;
}
protected function incrementEmbeddedUsage(ContentEntityInterface $entity, $t_type, $uuid) {
$target_entity = $this->entityRepository
->loadEntityByUuid($t_type, $uuid);
if ($target_entity) {
$this->usageService
->add($target_entity
->id(), $t_type, $entity
->id(), $entity
->getEntityTypeId(), $this->pluginId);
}
}
protected function decrementEmbeddedUsage(ContentEntityInterface $entity, $t_type, $uuid) {
$target_entity = $this->entityRepository
->loadEntityByUuid($t_type, $uuid);
if ($target_entity) {
$this->usageService
->delete($target_entity
->id(), $t_type, $entity
->id(), $entity
->getEntityTypeId());
}
}
}