You are here

class TermParentCdfAttribute in Acquia Content Hub 8.2

Creates the term parent cdf attribute.

@package Drupal\acquia_contenthub\EventSubscriber\CdfAttributes

Hierarchy

  • class \Drupal\acquia_contenthub\EventSubscriber\CdfAttributes\TermParentCdfAttribute implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of TermParentCdfAttribute

1 file declares its use of TermParentCdfAttribute
TermParentCdfAttributeTest.php in tests/src/Kernel/EventSubscriber/CdfAttributes/TermParentCdfAttributeTest.php
1 string reference to 'TermParentCdfAttribute'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses TermParentCdfAttribute
entity.term_parent.cdf.attribute in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\CdfAttributes\TermParentCdfAttribute

File

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

Namespace

Drupal\acquia_contenthub\EventSubscriber\CdfAttributes
View source
class TermParentCdfAttribute implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      AcquiaContentHubEvents::POPULATE_CDF_ATTRIBUTES => [
        [
          'onPopulateAttributes',
          100,
        ],
      ],
    ];
  }

  /**
   * Reacts to POPULATE_CDF_ATTRIBUTES event.
   *
   * @param \Drupal\acquia_contenthub\Event\CdfAttributesEvent $event
   *   Event.
   *
   * @throws \Exception
   */
  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));
  }

  /**
   * Gets the Taxonomy Term Storage.
   */
  protected function getStorage() {
    return $this
      ->getEntityTypeManager()
      ->getStorage('taxonomy_term');
  }

  /**
   * Gets the Entity Type Manager Service.
   *
   * @return \Drupal\Core\Entity\EntityTypeManagerInterface
   *   The EntityTypeManager.
   */
  protected function getEntityTypeManager() {
    return \Drupal::entityTypeManager();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TermParentCdfAttribute::getEntityTypeManager protected function Gets the Entity Type Manager Service.
TermParentCdfAttribute::getStorage protected function Gets the Taxonomy Term Storage.
TermParentCdfAttribute::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
TermParentCdfAttribute::onPopulateAttributes public function Reacts to POPULATE_CDF_ATTRIBUTES event.