personacontent-8.x-2.x-dev/personacontent.module
personacontent.module
<?php
/**
* @file
* @defgroup personacontent Personacontent: Enables personalized content
*
* The Personacontent module generates fields and paragraphs that allows user
* to have personalized content per segment.
*/
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
/**
* {@inheritdoc}
*/
function personacontent_preprocess_paragraph(&$variables) {
$elements = $variables['elements'];
if ($elements['#paragraph']->getType() != 'persona_component_assignments') {
return;
}
static $i;
$i = isset($i) ? ($i + 1) : 1;
$elements = $variables['elements']['#paragraph']->field_persona_comp_item->getValue();
$elements_json = personacontent_get_elements_json_paragraph($elements);
$variables['#attached']['drupalSettings']['personacontent_paragraph'][$i] = $elements_json;
$variables['attributes']['class'][] = 'personacontent--region--paragraph';
$variables['personacontent_region_paragraph_id'] = $i;
$variables['#attached']['library'][] = 'personacontent/region';
}
/**
* {@inheritdoc}
*/
function personacontent_preprocess_html(&$variables) {
$config = \Drupal::config('personacontent.settings');
$screen_debug = $config->get('debug');
$screen_debug_html = $config->get('debug_html');
// Get all Segments Defined.
$entityTypeManager = \Drupal::service('entity_type.manager');
$es_storage = $entityTypeManager->getStorage('entity_subqueue');
$entity_subqueue = $es_storage->load('personacontent_segments');
if ($entity_subqueue) {
$items = $entity_subqueue->get('items')->getValue();
$elements_json = personacontent_get_elements_json_node($items);
$variables['#attached']['drupalSettings']['personacontent']['segments'] = $elements_json;
$variables['#attached']['drupalSettings']['personacontent']['screen_debug'] = $screen_debug;
$variables['#attached']['drupalSettings']['personacontent']['screen_debug_html'] = $screen_debug_html;
$variables['#attached']['library'][] = 'personacontent/html';
}
}
/**
* {@inheritdoc}
*/
function personacontent_preprocess_block(&$variables) {
if (!isset($variables['elements']['content']['#block_content'])) {
return;
}
$bundle = $variables['elements']['content']['#block_content']->bundle();
$valid = [
'segmented_paragraph' => 'field_content_block',
'region_segmented' => 'field_segmented_element',
];
if (!isset($valid[$bundle])) {
return;
}
$field = $valid[$bundle];
if ($field == 'field_segmented_element') {
if (!isset($variables['content']['#block_content']->field_segmented_element)) {
return;
}
}
elseif ($field == 'field_content_block') {
if (!isset($variables['content']['#block_content']->field_content_block)) {
return;
}
}
static $i;
$i = isset($i) ? ($i + 1) : 1;
if ($field == 'field_segmented_element') {
personacontent_preprocess_block_region_segmented($variables, $i);
}
elseif ($field == 'field_content_block') {
personacontent_preprocess_block_content_block($variables, $i);
}
}
/**
* {@inheritdoc}
*/
function personacontent_preprocess_block_region_segmented(&$variables, $i) {
$elements = $variables['content']['#block_content']->field_segmented_element->getValue();
$elements_json = personacontent_get_elements_json($elements);
$variables['#attached']['drupalSettings']['personacontent'][$i] = $elements_json;
$variables['attributes']['class'][] = 'personacontent--region';
$variables['personacontent_region_id'] = $i;
$variables['#attached']['library'][] = 'personacontent/region';
}
/**
* {@inheritdoc}
*/
function personacontent_preprocess_block_content_block(&$variables, $i) {
$content_block_raw = $variables['content']['#block_content']->field_content_block->getValue();
if (!($content_block = Paragraph::load($content_block_raw[0]['target_id']))) {
return;
}
$elements = $content_block->field_persona_comp_item->getValue();
$elements_json = personacontent_get_elements_json_paragraph($elements);
$variables['#attached']['drupalSettings']['personacontent'][$i] = $elements_json;
$variables['attributes']['class'][] = 'personacontent--region';
$variables['personacontent_region_id'] = $i;
$variables['#attached']['library'][] = 'personacontent/region';
}
/**
* {@inheritdoc}
*/
function personacontent_theme_suggestions_paragraph(array $variables) {
$elements = $variables['elements'];
if ($elements['#paragraph']->getType() != 'persona_component_assignments') {
return;
}
$suggestions = [];
$suggestions[] = 'paragraph__personacontent';
return $suggestions;
}
/**
* {@inheritdoc}
*/
function personacontent_theme_suggestions_block(array $variables) {
if (!isset($variables['elements']['content']['#block_content'])) {
return;
}
$bundle = $variables['elements']['content']['#block_content']->bundle();
if ($bundle != 'region_segmented') {
return;
}
$suggestions = [];
$suggestions[] = 'block__personacontent';
return $suggestions;
}
/**
* {@inheritdoc}
*/
function personacontent_theme_suggestions_node(array $variables) {
if (!isset($variables['elements']['#node'])) {
return;
}
$bundle = $variables['elements']['#node']->bundle();
if ($bundle != 'element') {
return;
}
$suggestions = [];
$suggestions[] = 'node__personacontent';
return $suggestions;
}
/**
* {@inheritdoc}
*/
function personacontent_theme() {
return [
'block__personacontent' => [
'template' => 'block--personacontent',
'base hook' => 'block',
],
'node__personacontent' => [
'template' => 'node--personacontent',
'base hook' => 'node',
],
'paragraph__personacontent' => [
'template' => 'paragraph--personacontent',
'base hook' => 'paragraph',
],
];
}
/**
* {@inheritdoc}
*/
function personacontent_get_elements_json_node($nodes) {
$items = [];
foreach ($nodes as $node_raw) {
if (($segment = Node::load($node_raw['target_id']))) {
$items[] = personacontent_get_elements_json_node_item($segment);
}
}
return $items;
}
/**
* {@inheritdoc}
*/
function personacontent_get_elements_json_node_item($segment) {
// Get Segment.
$item = [];
$item['id'] = $segment->id();
$item['title'] = $segment->label();
// Get Rules.
$rules_matches = $segment->field_rules_matches->getValue();
$item['rules_matches'] = $rules_matches[0]['value'];
$item['rules'] = [];
// Rules.
$rules = $segment->field_rules->getValue();
$item['rules'] = personacontent_get_elements_json_item_rules($rules);
return $item;
}
/**
* {@inheritdoc}
*/
function personacontent_get_elements_json_paragraph($paragraphs) {
$items = [];
foreach ($paragraphs as $paragraph_raw) {
if (($paragraph = Paragraph::load($paragraph_raw['target_id']))) {
$items[] = personacontent_get_elements_json_paragraph_item($paragraph);
}
}
return $items;
}
/**
* {@inheritdoc}
*/
function personacontent_get_elements_json_paragraph_item($paragraph) {
// Get Segment.
$item = [];
$segment_raw = $paragraph->field_segment->getValue();
$segment = Node::load($segment_raw[0]['target_id']);
$item['title'] = $segment->label();
// Get Rules.
$rules_matches = $segment->field_rules_matches->getValue();
$item['rules_matches'] = $rules_matches[0]['value'];
$item['rules'] = [];
// Rules.
$rules = $segment->field_rules->getValue();
$item['rules'] = personacontent_get_elements_json_item_rules($rules);
// Get Element.
$element = $paragraph->field_row->getValue();
$item['element'] = [];
foreach ($element as $element_item) {
$item['element'][] = personacontent_get_elements_json_paragraph_item_element($element_item);
}
return $item;
}
/**
* {@inheritdoc}
*/
function personacontent_get_elements_json_paragraph_item_element($element_raw) {
$view_mode = 'default';
$entity = \Drupal::entityTypeManager()->getStorage('paragraph')->load($element_raw['target_id']);
$builder = \Drupal::getContainer()->get('entity_type.manager')->getViewBuilder('paragraph');
$view = $builder->view($entity, $view_mode);
$render = \Drupal::service('renderer')->render($view);
return $render;
}
/**
* {@inheritdoc}
*/
function personacontent_get_elements_json($paragraphs) {
$items = [];
foreach ($paragraphs as $paragraph_raw) {
$paragraph = Paragraph::load($paragraph_raw['target_id']);
$items[] = personacontent_get_elements_json_item($paragraph);
}
return $items;
}
/**
* {@inheritdoc}
*/
function personacontent_get_elements_json_item($paragraph) {
// Get Segment.
$item = [];
$segment_raw = $paragraph->field_segment->getValue();
$segment = Node::load($segment_raw[0]['target_id']);
$item['title'] = $segment->label();
// Get Rules.
$rules_matches = $segment->field_rules_matches->getValue();
$item['rules_matches'] = $rules_matches[0]['value'];
$item['rules'] = [];
// Rules.
$rules = $segment->field_rules->getValue();
$item['rules'] = personacontent_get_elements_json_item_rules($rules);
// Get Element.
$element = $paragraph->field_element->getValue();
$item['element'] = personacontent_get_elements_json_item_element($element[0]);
return $item;
}
/**
* {@inheritdoc}
*/
function personacontent_get_elements_json_item_element($element_raw) {
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
$node = Node::load($element_raw['target_id']);
$node_view = $view_builder->view($node, 'default');
return \Drupal::service('renderer')->renderRoot($node_view)->__toString();
}
/**
* {@inheritdoc}
*/
function personacontent_get_elements_json_item_rules($rules_raw) {
$rules = [];
foreach ($rules_raw as $rule_raw) {
$rule_p = Paragraph::load($rule_raw['target_id']);
$rule = [];
// Type.
$rule['type'] = $rule_p->field_type->getValue();
$rule['type'] = $rule['type'][0]['value'];
// Operator.
$rule['operator'] = $rule_p->field_operator->getValue();
$rule['operator'] = $rule['operator'][0]['value'];
// Values.
$rule['values'] = $rule_p->field_values->getValue();
$rule['values'] = explode("\n", $rule['values'][0]['value']);
// Options.
$options = $rule_p->field_options->getValue();
foreach ($options as $option) {
$rule['options'][] = $option;
}
$rules[] = $rule;
}
return $rules;
}
