class EntityUrlCdfAttribute in Acquia Content Hub 8.2
Adds a CDF attribute that contains the Entity URL.
Hierarchy
- class \Drupal\acquia_contenthub\EventSubscriber\CdfAttributes\EntityUrlCdfAttribute implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of EntityUrlCdfAttribute
1 string reference to 'EntityUrlCdfAttribute'
1 service uses EntityUrlCdfAttribute
File
- src/
EventSubscriber/ CdfAttributes/ EntityUrlCdfAttribute.php, line 15
Namespace
Drupal\acquia_contenthub\EventSubscriber\CdfAttributesView source
class EntityUrlCdfAttribute implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::POPULATE_CDF_ATTRIBUTES][] = [
'onPopulateAttributes',
100,
];
return $events;
}
/**
* Method called on the POPULATE_CDF_ATTRIBUTES event.
*
* Adds a CDF attribute that contains the Entity URL.
*
* @param \Drupal\acquia_contenthub\Event\CdfAttributesEvent $event
* The CdfAttributesEvent object.
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
public function onPopulateAttributes(CdfAttributesEvent $event) {
$entity = $event
->getEntity();
if (!$entity instanceof ContentEntityInterface || !$entity
->hasLinkTemplate('canonical')) {
return;
}
try {
$base_path = Url::fromUserInput("/", [
'absolute' => TRUE,
])
->toString();
$entity_url = $base_path . $entity
->toUrl('canonical')
->getInternalPath();
} catch (\UnexpectedValueException $ex) {
// We could not obtain the internal path for this entity.
\Drupal::logger('acquia_contenthub')
->error(sprintf("We could not obtain the Internal URL for Entity (%s:%s). The URL attribute was not added to the CDF. Error code: %s, Error message: \"%s\"", $entity
->getEntityTypeId(), $entity
->id(), $ex
->getCode(), $ex
->getMessage()));
return;
}
$cdf = $event
->getCdf();
$cdf
->addAttribute('url', CDFAttribute::TYPE_STRING, $entity_url);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityUrlCdfAttribute:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
EntityUrlCdfAttribute:: |
public | function | Method called on the POPULATE_CDF_ATTRIBUTES event. |