You are here

public function TermParentCdfAttribute::onPopulateAttributes in Acquia Content Hub 8.2

Reacts to POPULATE_CDF_ATTRIBUTES event.

Parameters

\Drupal\acquia_contenthub\Event\CdfAttributesEvent $event: Event.

Throws

\Exception

File

src/EventSubscriber/CdfAttributes/TermParentCdfAttribute.php, line 37

Class

TermParentCdfAttribute
Creates the term parent cdf attribute.

Namespace

Drupal\acquia_contenthub\EventSubscriber\CdfAttributes

Code

public function onPopulateAttributes(CdfAttributesEvent $event) {

  /** @var \Drupal\taxonomy\Entity\Term $entity */
  $entity = $event
    ->getEntity();
  if (FALSE == $entity instanceof TermInterface) {
    return;
  }
  $term_parents = $this
    ->getStorage()
    ->loadParents($entity
    ->id());
  if (empty($term_parents)) {
    return;
  }
  $parents = array_map(function ($parent) {
    return $parent
      ->uuid();
  }, $term_parents);

  // Exclude current term from parents list.
  $parents = array_filter($parents, function ($uuid) use ($entity) {
    return !empty($uuid) && $uuid !== $entity
      ->uuid();
  });
  $event
    ->getCdf()
    ->addAttribute('parent', CDFAttribute::TYPE_ARRAY_REFERENCE, array_values($parents));
}