You are here

public function TermParentSerializer::onSerializeContentField in Acquia Content Hub 8.2

Reacts on SERIALIZE_CONTENT_ENTITY_FIELD event.

Manages taxonomy terms relationships.

Parameters

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

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

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

Class

TermParentSerializer
Serializes taxonomy term field parents.

Namespace

Drupal\acquia_contenthub\EventSubscriber\SerializeContentField

Code

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);
    }
  }
}