You are here

protected function EmbedBase::getEmbeddedEntitiesByField in Entity Usage 8

Finds all entities embedded (<drupal-entity>) by formatted text fields.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: An entity object whose fields to analyze.

bool $omit_field_names: (Optional) Whether the field names should be omitted from the results. Defaults to FALSE.

Return value

array An array of found embedded entities, in the following structure: [ 'field_name' => [ 'uuid1' => 'entity_type1', 'uuid2' => 'entity_type1', 'uuid3' => 'entity_type2', etc. ], ] If the $omit_field_names flag is TRUE, the first level is not present, and the result array is directly an associative array of uuids as keys and entity_types as values.

3 calls to EmbedBase::getEmbeddedEntitiesByField()
EmbedBase::trackOnEntityCreation in src/Plugin/EntityUsage/Track/EmbedBase.php
Track usage updates on the creation of entities.
EmbedBase::trackOnEntityDeletion in src/Plugin/EntityUsage/Track/EmbedBase.php
Track usage updates on the deletion of entities.
EmbedBase::trackOnEntityUpdate in src/Plugin/EntityUsage/Track/EmbedBase.php
Track usage updates on the edition of entities.

File

src/Plugin/EntityUsage/Track/EmbedBase.php, line 190

Class

EmbedBase
Base class for plugins tracking usage in entities embedded in WYSIWYG fields.

Namespace

Drupal\entity_usage\Plugin\EntityUsage\Track

Code

protected function getEmbeddedEntitiesByField(ContentEntityInterface $entity, $omit_field_names = FALSE) {
  $entities = [];
  if ($this->moduleHandler
    ->moduleExists('editor')) {
    $formatted_text_fields = _editor_get_formatted_text_fields($entity);
    foreach ($formatted_text_fields as $formatted_text_field) {
      $text = '';
      $field_items = $entity
        ->get($formatted_text_field);
      foreach ($field_items as $field_item) {
        $text .= $field_item->value;
        if ($field_item
          ->getFieldDefinition()
          ->getType() == 'text_with_summary') {
          $text .= $field_item->summary;
        }
      }
      if ($omit_field_names) {
        $entities += $this
          ->parseEntitiesFromText($text);
      }
      else {
        $entities[$formatted_text_field] = $this
          ->parseEntitiesFromText($text);
      }
    }
  }
  return $entities;
}