You are here

public function LayoutBuilder::getTargetEntities in Entity Usage 8.4

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 EntityUsageBase::getTargetEntities

File

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

Class

LayoutBuilder
Tracks usage of entities related in Layout Builder layouts.

Namespace

Drupal\entity_usage\Plugin\EntityTrack\Track

Code

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

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

  /** @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']));
      }
    }
  }
  $target_entities = [];
  if (count($blockContentRevisionIds) > 0) {
    $target_entities = $this
      ->prepareBlockContentIds($blockContentRevisionIds);
  }
  if (count($ebbContentIds) > 0) {
    $target_entities = array_merge($target_entities, $this
      ->prepareEntityBrowserBlockIds($ebbContentIds));
  }
  return $target_entities;
}