You are here

class EditorDetectorBase in Lingotek Translation 3.8.x

Same name and namespace in other branches
  1. 4.0.x src/Plugin/RelatedEntitiesDetector/EditorDetectorBase.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\EditorDetectorBase
  2. 3.6.x src/Plugin/RelatedEntitiesDetector/EditorDetectorBase.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\EditorDetectorBase
  3. 3.7.x src/Plugin/RelatedEntitiesDetector/EditorDetectorBase.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\EditorDetectorBase

Hierarchy

Expanded class hierarchy of EditorDetectorBase

File

src/Plugin/RelatedEntitiesDetector/EditorDetectorBase.php, line 15

Namespace

Drupal\lingotek\Plugin\RelatedEntitiesDetector
View source
class EditorDetectorBase extends PluginBase implements RelatedEntitiesDetectorInterface, ContainerFactoryPluginInterface {

  /**
   * The EntityRepository service.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * The Lingotek configuration service.
   *
   * @var \Drupal\lingotek\LingotekConfigurationServiceInterface
   */
  protected $lingotekConfiguration;

  /**
   * The field types this detector applies to.
   *
   * @var string[]
   */
  protected $fieldTypes;

  /**
   * The XPath to find embedded content.
   *
   * @var string
   */
  protected $xpath;

  /**
   * NestedEntityReferences 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 array $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The EntityRepositoryInterface service.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
   *   The entity field manager.
   * @param \Drupal\lingotek\LingotekConfigurationServiceInterface $lingotekConfiguration
   *   The Lingotek configuration service.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Extract entities referenced by a given text.
   *
   * @param string $text
   *   The text to extract the entities from.
   *
   * @return array
   *   Array of entities in the form of (uuid => entity_type_id).
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EditorDetectorBase::$entityFieldManager protected property The entity field manager.
EditorDetectorBase::$entityRepository protected property The EntityRepository service.
EditorDetectorBase::$fieldTypes protected property The field types this detector applies to. 3
EditorDetectorBase::$lingotekConfiguration protected property The Lingotek configuration service.
EditorDetectorBase::$xpath protected property The XPath to find embedded content. 2
EditorDetectorBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
EditorDetectorBase::extract public function Extract nested and related content. Overrides RelatedEntitiesDetectorInterface::extract
EditorDetectorBase::extractEntitiesFromText protected function Extract entities referenced by a given text. 1
EditorDetectorBase::__construct public function NestedEntityReferences constructor. Overrides PluginBase::__construct 1
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.