public function EntityUrlCdfAttribute::onPopulateAttributes in Acquia Content Hub 8.2
Method called on the POPULATE_CDF_ATTRIBUTES event.
Adds a CDF attribute that contains the Entity URL.
Parameters
\Drupal\acquia_contenthub\Event\CdfAttributesEvent $event: The CdfAttributesEvent object.
Throws
\Drupal\Core\Entity\EntityMalformedException
File
- src/
EventSubscriber/ CdfAttributes/ EntityUrlCdfAttribute.php, line 36
Class
- EntityUrlCdfAttribute
- Adds a CDF attribute that contains the Entity URL.
Namespace
Drupal\acquia_contenthub\EventSubscriber\CdfAttributesCode
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);
}