You are here

class NestedLayoutBuilderEntitiesDetector in Lingotek Translation 4.0.x

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

@RelatedEntitiesDetector ( id = "nested_layout_builder_entities_detector", title =

Plugin annotation


@Translation("Get related nested layout builder content"),
  description = @translation("The default retrieval of nested layout builder content"),
  weight = 5,
)

Hierarchy

Expanded class hierarchy of NestedLayoutBuilderEntitiesDetector

1 file declares its use of NestedLayoutBuilderEntitiesDetector
NestedLayoutBuilderEntitiesDetectorTest.php in tests/src/Unit/Plugin/RelatedEntitiesDetector/NestedLayoutBuilderEntitiesDetectorTest.php

File

src/Plugin/RelatedEntitiesDetector/NestedLayoutBuilderEntitiesDetector.php, line 25

Namespace

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

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

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

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

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Nested layoutBuilderDetector 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\EntityTypeManagerInterface $entityTypeManager
   *   The entity manager
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
   *   The entity field manager.
   * @param \Drupal\lingotek\LingotekConfigurationServiceInterface $lingotekConfiguration
   *   The Lingotek configuration service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, EntityFieldManagerInterface $entityFieldManager, LingotekConfigurationServiceInterface $lingotekConfiguration, ModuleHandlerInterface $moduleHandler) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entityTypeManager;
    $this->entityFieldManager = $entityFieldManager;
    $this->lingotekConfiguration = $lingotekConfiguration;
    $this->moduleHandler = $moduleHandler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('entity_field.manager'), $container
      ->get('lingotek.configuration'), $container
      ->get('module_handler'));
  }

  /**
   * {@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());
      $layoutBuilderAT = $this->moduleHandler
        ->moduleExists('layout_builder_at');
      $layoutBuilderST = $this->moduleHandler
        ->moduleExists('layout_builder_st');
      foreach ($field_definitions as $k => $definition) {
        $field_type = $field_definitions[$k]
          ->getType();
        if ($field_type === 'layout_translation' && $layoutBuilderST || $field_type === 'layout_section' && $layoutBuilderAT) {
          $blockContentRevisionIds = [];
          $block_manager = \Drupal::service('plugin.manager.block');
          $layoutField = $entity
            ->get(OverridesSectionStorage::FIELD_NAME);
          $layout = $layoutField
            ->getValue();

          /** @var \Drupal\layout_builder\Section $sectionObject */
          foreach ($layout as $section) {
            $sectionObject = $section['section'];
            $components = $sectionObject
              ->getComponents();

            /** @var \Drupal\layout_builder\SectionComponent $component */
            foreach ($components as $component) {
              $blockDefinition = $block_manager
                ->getDefinition($component
                ->getPluginId());
              $configuration = $component
                ->toArray()['configuration'];
              if ($blockDefinition['id'] === 'inline_block') {
                $blockContentRevisionIds[] = $configuration['block_revision_id'];
              }
            }
          }
          $target_entity_ids = empty($blockContentRevisionIds) ? $blockContentRevisionIds : $this
            ->prepareBlockContentIds($blockContentRevisionIds);
          $target_entity_type_id = 'block_content';
          foreach ($target_entity_ids as $target_content_entity_id) {
            $target_entity_type = $this->entityTypeManager
              ->getDefinition($target_entity_type_id);
            if ($target_entity_type instanceof ContentEntityType) {
              $relatedEntity = $this->entityTypeManager
                ->getStorage($target_entity_type_id)
                ->load($target_content_entity_id);
              if ($relatedEntity !== NULL) {

                // Avoid entities that were are already visited
                if (!isset($visited[$relatedEntity
                  ->bundle()]) || !in_array($relatedEntity
                  ->id(), $visited[$relatedEntity
                  ->bundle()])) {
                  if ($relatedEntity instanceof ContentEntityInterface && $relatedEntity
                    ->isTranslatable() && $this->lingotekConfiguration
                    ->isEnabled($relatedEntity
                    ->getEntityTypeId(), $relatedEntity
                    ->bundle())) {
                    if (!$this->lingotekConfiguration
                      ->isFieldLingotekEnabled($entity
                      ->getEntityTypeId(), $entity
                      ->bundle(), $k)) {
                      $entities = $this
                        ->extract($relatedEntity, $entities, $related, $depth, $visited);
                    }
                    else {
                      $related[$relatedEntity
                        ->getEntityTypeId()][$relatedEntity
                        ->id()] = $relatedEntity
                        ->getUntranslated();
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    return $entities;
  }

  /**
   * Get block content ids from the revision ids stored in the field definition of parent entity
   *
   * @param array $blockContentRevisionIds
   *   An array of block revision IDs.
   *
   * @return array
   *   An array of block IDs.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  private function prepareBlockContentIds(array $blockContentRevisionIds) {

    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $blockContentStorage */
    $blockContentStorage = $this->entityTypeManager
      ->getStorage('block_content');
    $ids = $blockContentStorage
      ->getQuery()
      ->condition($blockContentStorage
      ->getEntityType()
      ->getKey('revision'), $blockContentRevisionIds, 'IN')
      ->execute();
    return $ids;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NestedLayoutBuilderEntitiesDetector::$entityFieldManager protected property The entity field manager.
NestedLayoutBuilderEntitiesDetector::$entityTypeManager protected property The entity type manager.
NestedLayoutBuilderEntitiesDetector::$lingotekConfiguration protected property The Lingotek configuration service.
NestedLayoutBuilderEntitiesDetector::$moduleHandler protected property The module handler.
NestedLayoutBuilderEntitiesDetector::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
NestedLayoutBuilderEntitiesDetector::extract public function Extract nested and related content. Overrides RelatedEntitiesDetectorInterface::extract
NestedLayoutBuilderEntitiesDetector::prepareBlockContentIds private function Get block content ids from the revision ids stored in the field definition of parent entity
NestedLayoutBuilderEntitiesDetector::__construct public function Nested layoutBuilderDetector constructor. Overrides PluginBase::__construct
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.