You are here

public function LayoutBuilder::getTargetEntities in Entity Usage 8.2

Retrieve the target entity(ies) from a field item value.

Parameters

\Drupal\Core\Field\FieldItemInterface $item: The field item to get the target entity(ies) from.

Return value

string[] An indexed array of strings where each target entity type and ID are concatenated with a "|" character. Will return an empty array if no target entity could be retrieved from the received field item value.

Overrides EntityUsageTrackInterface::getTargetEntities

File

src/Plugin/EntityUsage/Track/LayoutBuilder.php, line 83

Class

LayoutBuilder
Tracks usage of entities related in Layout Builder layouts.

Namespace

Drupal\entity_usage\Plugin\EntityUsage\Track

Code

public function getTargetEntities(FieldItemInterface $item) {
  assert($item instanceof LayoutSectionItem);

  // We support both Content Blocks and Entity Browser Blocks.
  $blockContentRevisionIds = [];
  $ebbContentIds = [];
  $contentDependencyIds = [];

  /** @var \Drupal\layout_builder\Plugin\DataType\SectionData $value */
  foreach ($item as $value) {

    /** @var \Drupal\layout_builder\Section $section */
    $section = $value
      ->getValue();
    foreach ($section
      ->getComponents() as $component) {
      $configuration = $component
        ->toArray()['configuration'];
      try {
        $def = $this->blockManager
          ->getDefinition($component
          ->getPluginId());
      } catch (PluginNotFoundException $e) {

        // Block has since been removed, continue.
        continue;
      }
      if ($def['id'] === 'inline_block') {
        $blockContentRevisionIds[] = $configuration['block_revision_id'];
      }
      elseif ($def['id'] === 'entity_browser_block' && !empty($configuration['entity_ids'])) {
        $ebbContentIds = array_unique(array_merge($ebbContentIds, (array) $configuration['entity_ids']));
      }

      // Check the block plugin's content dependencies.

      /** @var \Drupal\Core\Block\BlockPluginInterface $plugin */
      $plugin = $component
        ->getPlugin();
      $dependencies = $plugin
        ->calculateDependencies();
      if (!empty($dependencies['content'])) {
        $contentDependencyIds = array_merge($contentDependencyIds, $dependencies['content']);
      }
    }
  }
  $target_entities = [];
  if (count($blockContentRevisionIds) > 0) {
    $target_entities = $this
      ->prepareBlockContentIds($blockContentRevisionIds);
  }
  if (count($ebbContentIds) > 0) {
    $target_entities = array_merge($target_entities, $this
      ->prepareEntityBrowserBlockIds($ebbContentIds));
  }
  if (count($contentDependencyIds) > 0) {
    $target_entities = array_merge($target_entities, $this
      ->prepareContentDependencyIds($contentDependencyIds));
  }
  return $target_entities;
}