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'
1 service uses TermParentSerializer
File
- src/
EventSubscriber/ SerializeContentField/ TermParentSerializer.php, line 15
Namespace
Drupal\acquia_contenthub\EventSubscriber\SerializeContentFieldView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
TermParentSerializer:: |
protected | property | The database connection. | |
TermParentSerializer:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
TermParentSerializer:: |
public | function | Reacts on SERIALIZE_CONTENT_ENTITY_FIELD event. | |
TermParentSerializer:: |
public | function | TermParentSerializer constructor. |