PageContext.php in Acquia Lift Connector 8
File
src/Service/Context/PageContext.php
View source
<?php
namespace Drupal\acquia_lift\Service\Context;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\image\Entity\ImageStyle;
class PageContext {
const ENGAGEMENT_SCORE_DEFAULT = 1;
private $fieldMappings;
private $thumbnailConfig;
private $taxonomyTermStorage;
private $pageContext = [
'content_title' => 'Untitled',
'content_type' => 'page',
'page_type' => 'content page',
'content_section' => '',
'content_keywords' => '',
'post_id' => '',
'published_date' => '',
'thumbnail_url' => '',
'persona' => '',
'engagement_score' => SELF::ENGAGEMENT_SCORE_DEFAULT,
'author' => '',
'evalSegments' => TRUE,
'trackingId' => '',
];
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
$settings = $config_factory
->get('acquia_lift.settings');
$this->fieldMappings = $settings
->get('field_mappings');
$this->thumbnailConfig = $settings
->get('thumbnail');
$this->taxonomyTermStorage = $entity_type_manager
->getStorage('taxonomy_term');
}
public function setByNode(EntityInterface $node) {
$this
->setNodeData($node);
$this
->setThumbnailUrl($node);
$this
->setFields($node);
}
private function setNodeData(EntityInterface $node) {
$this->pageContext['content_type'] = $node
->getType();
$this->pageContext['content_title'] = $node
->getTitle();
$this->pageContext['published_date'] = $node
->getCreatedTime();
$this->pageContext['post_id'] = $node
->id();
$this->pageContext['author'] = $node
->getOwner()
->getUsername();
$this->pageContext['page_type'] = 'node page';
}
private function setThumbnailUrl(EntityInterface $node) {
$node_type = $node
->getType();
if (!isset($this->thumbnailConfig[$node_type])) {
return;
}
$node_type_thumbnail_config = $this->thumbnailConfig[$node_type];
$thumbnail_config_array = explode('->', $node_type_thumbnail_config['field']);
$entity = $node;
foreach ($thumbnail_config_array as $field_key) {
if (empty($entity->{$field_key}->entity) || method_exists($entity->{$field_key}, 'isEmpty') && $entity->{$field_key}
->isEmpty()) {
return;
}
$entity = $entity->{$field_key}->entity;
}
if ($entity
->bundle() !== 'file') {
return;
}
$fileUri = $entity
->getFileUri();
$image_style = ImageStyle::load($node_type_thumbnail_config['style']);
if (empty($image_style)) {
return;
}
$thumbnail_uri = $image_style
->buildUrl($fileUri);
$this->pageContext['thumbnail_url'] = file_create_url($thumbnail_uri);
}
private function setFields(EntityInterface $node) {
$available_field_vocabulary_names = $this
->getAvailableFieldVocabularyNames($node);
$vocabulary_term_names = $this
->getVocabularyTermNames($node);
foreach ($available_field_vocabulary_names as $page_context_name => $vocabulary_names) {
$field_term_names = $this
->getFieldTermNames($vocabulary_names, $vocabulary_term_names);
$this->pageContext[$page_context_name] = implode(',', $field_term_names);
}
}
private function getAvailableFieldVocabularyNames(EntityInterface $node) {
$available_field_vocabulary_names = [];
foreach ($this->fieldMappings as $page_context_name => $field_name) {
if (!isset($node->{$field_name})) {
continue;
}
$vocabulary_names = $node->{$field_name}
->getSetting('handler_settings')['target_bundles'];
$available_field_vocabulary_names[$page_context_name] = $vocabulary_names;
}
return $available_field_vocabulary_names;
}
private function getVocabularyTermNames(EntityInterface $node) {
$terms = $this->taxonomyTermStorage
->getNodeTerms([
$node
->id(),
]);
$node_terms = isset($terms[$node
->id()]) ? $terms[$node
->id()] : [];
$vocabulary_term_names = [];
foreach ($node_terms as $term) {
$vocabulary_id = $term
->getVocabularyId();
$term_name = $term
->getName();
$vocabulary_term_names[$vocabulary_id][] = $term_name;
}
return $vocabulary_term_names;
}
private function getFieldTermNames($vocabulary_names, $vocabulary_term_names) {
$field_term_names = [];
foreach ($vocabulary_names as $vocabulary_name) {
if (!isset($vocabulary_term_names[$vocabulary_name])) {
continue;
}
$field_term_names = array_merge($field_term_names, $vocabulary_term_names[$vocabulary_name]);
}
return array_unique($field_term_names);
}
public function getAll() {
return $this->pageContext;
}
public function setPageContextTitle($title) {
if (is_array($title) && isset($title['#markup']) && isset($title['#allowed_tags'])) {
$allowed_tags = empty($title['#allowed_tags']) ? '' : '<' . implode('><', $title['#allowed_tags']) . '>';
$title = strip_tags($title['#markup'], $allowed_tags);
}
if (is_array($title) || empty($title)) {
$this->pageContext['content_title'] = '';
return;
}
$this->pageContext['content_title'] = $title;
}
}