-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhtml_title.module
119 lines (112 loc) · 4.37 KB
/
html_title.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* @file
* This module enables limited HTML to be used in node titles. It strips title
* markup from RSS feeds to eliminate unsightly markup in feed readers.
*/
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Render\Element;
use Drupal\node\Entity\Node;
use Drupal\Component\Utility\SafeMarkup;
/**
* Implementation of hook_theme_registry_alter()
*/
function html_title_theme_registry_alter(&$theme_registry) {
// Re-order search result pre-processing so ours always runs last
if ( \Drupal::moduleHandler()->moduleExists('search') && is_array($theme_registry['search_result']['preprocess functions'])) {
foreach($theme_registry['search_result']['preprocess functions'] as $value) {
if ($value != 'html_title_preprocess_search_result') {
$callbacks[] = $value;
}
}
$callbacks[] = 'html_title_preprocess_search_result';
$theme_registry['search_result']['preprocess functions'] = $callbacks;
}
}
/**
* Implementation of hook_preprocess_page_title()
*/
function html_title_preprocess_page_title(&$variables) {
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$variables['node'] = $node;
/** @var \Drupal\node\NodeInterface $node */
$node = $variables['node'];
$config = \Drupal::config('html_title.settings');
$elements = $config->get('html_title_allowed_elements');
$elements = array_unique($elements);
$variables['title'] = SafeMarkup::format($node->getTitle(), $elements);
}
}
/**
* Implementation of template_preprocess_node()
*/
function html_title_preprocess_node(&$variables) {
$variables['node'] = $variables['elements']['#node'];
/** @var \Drupal\node\NodeInterface $node */
$node = $variables['node'];
switch ($variables['view_mode']) {
case 'search_index':
case 'rss':
return;
default:
$config = \Drupal::config('html_title.settings');
$elements = $config->get('html_title_allowed_elements');
if ($variables['view_mode'] == 'full' && isset($variables['content']['title'])) {
// @todo, find a way to deal with titles on full page views now that
// the title is in a block.
// $variables['content']['title']['#printed'] = FALSE;
}
$elements = array_unique($elements);
$variables['label'] = SafeMarkup::format($node->getTitle(), $elements);
unset($variables['elements']['title']);
}
}
/**
* Implementation of hook_preprocess_search_result()
*/
function html_title_preprocess_search_result(&$vars) {
$config = \Drupal::config('html_title.settings');
$elements = $config->get('html_title_allowed_elements');
$elements = array_values(array_unique($elements));
if (isset($vars['result']['title'])) {
$vars['title'] = SafeMarkup::format($vars['result']['title'], $elements);
}
if (isset($vars['result']['snippet'])) {
$vars['snippet'] = SafeMarkup::format($vars['result']['snippet']['#markup'], $elements);
}
}
/**
* Implements hook_ENTITY_TYPE_view().
*/
function html_title_node_view(array &$build, \Drupal\Node\Entity\Node $node, EntityViewDisplayInterface $display, $view_mode) {
switch ($view_mode) {
case 'rss':
$title = strip_tags($node->getTitle());
// @todo: Change title of node when there is RSS display type.
break;
}
}
/**
* Implements hook_entity_prepare_view().
*/
function html_title_entity_prepare_view($entity_type_id, array $entities, array $displays, $view_mode) {
if ($entity_type_id !== 'node') {
return;
}
// @todo, this is only required because template_preprocess_node assumes there
// is a key inside the renderable array inside the elements array:
// $variables['label'] = $variables['elements']['title'];
// Since it can be display configured to be removed, it disappaers from the
// elements list. We must reinstate if it has been hidden from display.
// The alternative is a core patch or an alt implementation, perhaps involving
// a new extra field?
foreach ($entities as $entity) {
$display = $displays[$entity->bundle()];
if (!$display->getComponent('title')) {
$fields = \Drupal::entityManager()->getFieldDefinitions($entity_type_id, $entity->bundle());
$options = $fields['title']->getDisplayOptions('view');
$built_title = \Drupal::service('plugin.manager.field.formatter')->prepareConfiguration($fields['title']->getType(), $options);
$display->setComponent('title', $built_title);
}
}
}