View source
<?php
namespace Drupal\lingotek\Plugin\RelatedEntitiesDetector;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\lingotek\LingotekConfigurationServiceInterface;
use Drupal\lingotek\RelatedEntities\RelatedEntitiesDetectorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EditorDetectorBase extends PluginBase implements RelatedEntitiesDetectorInterface, ContainerFactoryPluginInterface {
protected $entityRepository;
protected $entityFieldManager;
protected $lingotekConfiguration;
protected $fieldTypes;
protected $xpath;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityRepositoryInterface $entity_repository, EntityFieldManagerInterface $entityFieldManager, LingotekConfigurationServiceInterface $lingotekConfiguration) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityRepository = $entity_repository;
$this->entityFieldManager = $entityFieldManager;
$this->lingotekConfiguration = $lingotekConfiguration;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity.repository'), $container
->get('entity_field.manager'), $container
->get('lingotek.configuration'));
}
public function extract(ContentEntityInterface &$entity, array &$entities, array &$related, $depth, array $visited) {
$visited[$entity
->bundle()][] = $entity
->id();
$entities[$entity
->getEntityTypeId()][$entity
->id()] = $entity
->getUntranslated();
if ($depth > 0) {
--$depth;
$field_definitions = $this->entityFieldManager
->getFieldDefinitions($entity
->getEntityTypeId(), $entity
->bundle());
foreach ($field_definitions as $k => $definition) {
$field_type = $field_definitions[$k]
->getType();
if (in_array($field_type, $this->fieldTypes)) {
foreach ($entity
->get($k) as $delta => $fieldData) {
$text = $fieldData->value;
if ($field_type === 'text_with_summary') {
$text .= $fieldData->summary;
}
$fieldTextEntities = $this
->extractEntitiesFromText($text);
foreach ($fieldTextEntities as $fieldTextEntityTypeUuid => $fieldTextEntityTypeId) {
$relatedEntity = $this->entityRepository
->loadEntityByUuid($fieldTextEntityTypeId, $fieldTextEntityTypeUuid);
if ($relatedEntity instanceof ContentEntityInterface && $relatedEntity
->isTranslatable() && $this->lingotekConfiguration
->isEnabled($relatedEntity
->getEntityTypeId(), $relatedEntity
->bundle())) {
$entities[$relatedEntity
->getEntityTypeId()][$relatedEntity
->id()] = $relatedEntity
->getUntranslated();
$entities = $this
->extract($relatedEntity, $entities, $related, $depth, $visited);
}
}
}
}
}
}
return $entities;
}
protected function extractEntitiesFromText($text) {
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
$entities = [];
foreach ($xpath
->query($this->xpath) as $node) {
$entities[$node
->getAttribute('data-entity-uuid')] = $node
->getAttribute('data-entity-type');
}
return $entities;
}
}