You are here

class TermParentSerializer in Acquia Content Hub 8.2

Serializes taxonomy term field parents.

@package Drupal\acquia_contenthub\EventSubscriber\SerializeContentField

Hierarchy

  • class \Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\TermParentSerializer implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of TermParentSerializer

1 string reference to 'TermParentSerializer'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses TermParentSerializer
term.parent.serializer in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\TermParentSerializer

File

src/EventSubscriber/SerializeContentField/TermParentSerializer.php, line 15

Namespace

Drupal\acquia_contenthub\EventSubscriber\SerializeContentField
View source
class TermParentSerializer implements EventSubscriberInterface {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * TermParentSerializer constructor.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   Database connection.
   */
  public function __construct(Connection $database) {
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::SERIALIZE_CONTENT_ENTITY_FIELD][] = [
      'onSerializeContentField',
      102,
    ];
    return $events;
  }

  /**
   * Reacts on SERIALIZE_CONTENT_ENTITY_FIELD event.
   *
   * Manages taxonomy terms relationships.
   *
   * @param \Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent $event
   *   Event.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function onSerializeContentField(SerializeCdfEntityFieldEvent $event) {
    if ($event
      ->getEntity()
      ->getEntityTypeId() == 'taxonomy_term' && $event
      ->getFieldName() == 'parent') {

      /** @var \Drupal\taxonomy\TermInterface $term */
      $term = $event
        ->getEntity();

      /** @var \Drupal\taxonomy\TermStorage $storage */
      $storage = \Drupal::entityTypeManager()
        ->getStorage('taxonomy_term');
      $parents = $storage
        ->loadParents($term
        ->id());

      // Set the value of the parent for other subscribers to handle.
      if (!empty($parents)) {
        $term
          ->set('parent', $parents);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TermParentSerializer::$database protected property The database connection.
TermParentSerializer::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
TermParentSerializer::onSerializeContentField public function Reacts on SERIALIZE_CONTENT_ENTITY_FIELD event.
TermParentSerializer::__construct public function TermParentSerializer constructor.