public function EntityRenderHandler::onCreateCdf in Acquia Lift Connector 8.4
Actions on create CDF.
Parameters
\Drupal\acquia_contenthub\Event\CreateCdfEntityEvent $event: The create CDF entity event.
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
File
- modules/
acquia_lift_publisher/ src/ EventSubscriber/ Cdf/ EntityRenderHandler.php, line 161
Class
- EntityRenderHandler
- Class EntityRenderHandler.
Namespace
Drupal\acquia_lift_publisher\EventSubscriber\CdfCode
public function onCreateCdf(CreateCdfEntityEvent $event) {
$entity = $event
->getEntity();
if (!$entity instanceof ContentEntityInterface) {
// @todo we should support config entity rendering too.
return;
}
if ($view_modes = $this
->getEntityViewModesSettingValue($entity)) {
$client = $this->clientFactory
->getClient();
if (!is_object($client)) {
return;
}
$document = $client
->getEntities([
$entity
->uuid(),
]);
$remote_entity = $document
->hasEntity($entity
->uuid()) ? $document
->getCDFEntity($entity
->uuid()) : FALSE;
$default_lang = $this->languageDefault
->get();
foreach (array_keys($view_modes) as $view_mode) {
// The preview image field setting is saved along side the view modes.
// Don't process it as one.
if ($view_mode == 'acquia_lift_preview_image') {
continue;
}
foreach ($entity
->getTranslationLanguages() as $language) {
$translation = $entity
->getTranslation($language
->getId());
if ($remote_entity instanceof CDFObject) {
$uuid = $this
->getRenderUuid($remote_entity, $view_mode, $language
->getId());
}
else {
$uuid = $this->uuidGenerator
->generate();
}
$cdf = new CDFObject('rendered_entity', $uuid, date('c'), date('c'), $this->origin);
$this->languageDefault
->set($language);
$this->translationManager
->setDefaultLangcode($language
->getId());
$standard_languages = LanguageManager::getStandardLanguageList();
if (isset($standard_languages[$language
->getId()][1])) {
$language_native_label = $standard_languages[$language
->getId()][1];
}
else {
$language_native_label = $language
->getName();
}
// Getting fake user account to give as context to the normalization.
$account = $this
->getRenderUser();
// Checking for entity access permission to this particular account.
$entity_access = $translation
->access('view', $account, TRUE);
// Switch to render account.
$this->accountSwitcher
->switchTo($account);
$elements = [];
if ($entity_access
->isAllowed()) {
$elements = $this
->getViewModeMinimalHtml($translation, $view_mode);
}
$html = $this->renderer
->renderPlain($elements);
// Restore user account.
$this->accountSwitcher
->switchBack();
$metadata['data'] = base64_encode($html);
$cdf
->addAttribute('content', CDFAttribute::TYPE_STRING, trim(preg_replace('/\\s+/', ' ', str_replace("\n", ' ', strip_tags($html)))));
$cdf
->addAttribute('source_entity', CDFAttribute::TYPE_STRING, $translation
->uuid());
$cdf
->addAttribute('label', CDFAttribute::TYPE_ARRAY_STRING, $translation
->label(), $translation
->language()
->getId());
$cdf
->addAttribute('language', CDFAttribute::TYPE_STRING, $language
->getId());
$cdf
->addAttribute('language_label', CDFAttribute::TYPE_STRING, $language_native_label);
$cdf
->addAttribute('view_mode', CDFAttribute::TYPE_STRING, $view_mode);
$cdf
->addAttribute('view_mode_label', CDFAttribute::TYPE_STRING, $view_mode);
$preview_src = $this
->buildPreviewImageAttributeSource($view_modes, 'acquia_lift_preview_image', 'acquia_lift_publisher_preview_image', $translation);
if (!empty($preview_src)) {
$cdf
->addAttribute('preview_image', CDFAttribute::TYPE_STRING, $preview_src);
}
$cdf
->setMetadata($metadata);
$event
->addCdf($cdf);
}
}
$this->languageDefault
->set($default_lang);
$this->translationManager
->setDefaultLangcode($default_lang
->getId());
}
}