class PathFieldSerializer in Acquia Content Hub 8.2
Serializes path fields.
@package Drupal\acquia_contenthub\EventSubscriber\SerializeContentField
Hierarchy
- class \Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\FallbackFieldSerializer implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses ContentFieldMetadataTrait
- class \Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\PathFieldSerializer implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of PathFieldSerializer
1 string reference to 'PathFieldSerializer'
1 service uses PathFieldSerializer
File
- src/
EventSubscriber/ SerializeContentField/ PathFieldSerializer.php, line 16
Namespace
Drupal\acquia_contenthub\EventSubscriber\SerializeContentFieldView source
class PathFieldSerializer extends FallbackFieldSerializer implements EventSubscriberInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $manager;
/**
* PathFieldSerializer constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $manager) {
$this->manager = $manager;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::SERIALIZE_CONTENT_ENTITY_FIELD][] = [
'onSerializeContentField',
11,
];
return $events;
}
/**
* Manipulate the path properties.
*
* @param \Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent $event
* The content entity field serialization event.
*/
public function onSerializeContentField(SerializeCdfEntityFieldEvent $event) {
if ($event
->getEntity()
->getEntityTypeId() === 'path_alias' && $event
->getFieldName() === 'path') {
parent::onSerializeContentField($event);
$values = $event
->getFieldData();
$langcode = $event
->getEntity()
->language()
->getId();
$path = $values['value'][$langcode]['value'];
$params = Url::fromUserInput($path)
->getRouteParameters();
if ($params) {
foreach ($params as $key => $value) {
if ($this->manager
->hasDefinition($key)) {
$entity = $this->manager
->getStorage($key)
->load($value);
if ($path === "/{$entity->toUrl()->getInternalPath()}") {
$values['value'][$langcode]['value'] = $entity
->uuid();
$event
->setFieldData($values);
$event
->stopPropagation();
return;
}
}
}
}
}
if ('path' !== $event
->getField()
->getFieldDefinition()
->getType()) {
return;
}
$event
->stopPropagation();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentFieldMetadataTrait:: |
protected | function | Sets field metadata. | |
PathFieldSerializer:: |
protected | property | The entity type manager. | |
PathFieldSerializer:: |
public static | function |
Returns an array of event names this subscriber wants to listen to. Overrides FallbackFieldSerializer:: |
|
PathFieldSerializer:: |
public | function |
Manipulate the path properties. Overrides FallbackFieldSerializer:: |
|
PathFieldSerializer:: |
public | function | PathFieldSerializer constructor. |