You are here

public function NestedLayoutBuilderEntitiesDetector::extract in Lingotek Translation 3.8.x

Same name and namespace in other branches
  1. 4.0.x src/Plugin/RelatedEntitiesDetector/NestedLayoutBuilderEntitiesDetector.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\NestedLayoutBuilderEntitiesDetector::extract()
  2. 3.6.x src/Plugin/RelatedEntitiesDetector/NestedLayoutBuilderEntitiesDetector.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\NestedLayoutBuilderEntitiesDetector::extract()
  3. 3.7.x src/Plugin/RelatedEntitiesDetector/NestedLayoutBuilderEntitiesDetector.php \Drupal\lingotek\Plugin\RelatedEntitiesDetector\NestedLayoutBuilderEntitiesDetector::extract()

Extract nested and related content.

Parameters

Drupal\Core\Entity\ContentEntityInterface $entity: Entity node to parse for related entities.

array $entities: Entities found.

array $related: Related entities.

int $depth: Recursion depth of entity node.

array $visited: Array of visited field definitions.

Return value

array An array of the nested content

Overrides RelatedEntitiesDetectorInterface::extract

File

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

Class

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…

Namespace

Drupal\lingotek\Plugin\RelatedEntitiesDetector

Code

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;
}