-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalternate_node_fields.module
More file actions
139 lines (123 loc) · 5.34 KB
/
alternate_node_fields.module
File metadata and controls
139 lines (123 loc) · 5.34 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
use Drupal\views\ViewExecutable;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\node\NodeInterface;
use Drupal\node\Entity\Node;
use Drupal\Core\Routing\RouteObjectInterface;
/**
* Implements hook_views_pre_render().
*
* Replaces node title and summary field with Alternate field values, if they exist
*/
function alternate_node_fields_views_pre_render(\Drupal\views\ViewExecutable $view) {
// Check if the current view is a node view and if it has results.
if ($view->storage->get('base_table') === 'node_field_data' && !empty($view->result)) {
foreach ($view->result as $row) {
// Check if the row is a node entity.
if (isset($row->_entity) && $row->_entity instanceof \Drupal\node\NodeInterface) {
$node = $row->_entity;
// alternate title
if ($node->hasField('field_alt_node_fields_title') && !$node->get('field_alt_node_fields_title')->isEmpty()) {
$alternate_title = $node->get('field_alt_node_fields_title')->value;
// Set the node title to the alternate field value.
$node->setTitle($alternate_title);
}
// alternate summary feed
if ($node->hasField('field_alt_node_fields_feed_summa') && !$node->get('field_alt_node_fields_feed_summa')->isEmpty()) {
$alternate_summary_feed = $node->get('field_alt_node_fields_feed_summa')->value;
$node->field_summary_for_feed_1->value = $alternate_summary_feed;
}
}
}
}
}
/**
* Implements hook_tokens_alter().
*
* Replaces node title token for metatags with Alternate Title value, if it exists
*/
function alternate_node_fields_tokens_alter(array &$replacements, array $context, BubbleableMetadata $bubbleable_metadata) {
// Check if we are dealing with a node context.
if (isset($context['data']['node']) && $context['data']['node'] instanceof NodeInterface) {
/** @var \Drupal\node\NodeInterface $node */
$node = $context['data']['node'];
if ($node->hasField('field_alt_node_fields_title') && !$node->get('field_alt_node_fields_title')->isEmpty()) {
$custom_value = $node->get('field_alt_node_fields_title')->value;
$replacements['[node:title]'] = $custom_value;
}
// Add cacheability metadata if your dynamic content relies on it.
$bubbleable_metadata->addCacheableDependency($node);
}
}
/**
* Implements hook_preprocess_node().
*
* Replaces node title and summary field with Alternate field values, if they exist
*/
function alternate_node_fields_preprocess_node(&$variables) {
$node = $variables['node'];
// Check if the node has the alternate field and it's not empty.
if ($node->hasField('field_alt_node_fields_title') && !$node->get('field_alt_node_fields_title')->isEmpty()) {
$alternate_title = $node->get('field_alt_node_fields_title')->value;
$variables['label'] = $alternate_title; // This replaces the node title in node.html.twig.
}
// Check if the node has the alternate summary field and it's not empty.
if ($node->hasField('field_alt_node_fields_feed_summa') && !$node->get('field_alt_node_fields_feed_summa')->isEmpty()) {
$alternate_summary_feed = $node->get('field_alt_node_fields_feed_summa')->value;
$variables['node']->field_summary_for_feed_1->setValue(['value' => $alternate_summary_feed]);
// $variables['content']['field_summary_for_feed_1'] = $alternate_summary_feed; // This replaces the node title in node.html.twig.
}
}
/**
* Implements hook_preprocess_search_result().
*
* Replaces node title with Alternate Title value, if it exists
*/
function alternate_node_fields_preprocess_search_result(&$variables) {
// Get the result entity (e.g., node, user, etc.)
$result_entity = $variables['result']['node']; // Adjust 'node' based on the entity type
// Check if the entity and the field exist
if ($result_entity && $result_entity->hasField('field_alt_node_fields_title') && !$result_entity->get('field_alt_node_fields_title')->isEmpty()) {
$field_value = $result_entity->get('field_alt_node_fields_title')->value;
$variables['title'] = $field_value;
}
}
/**
* Implements hook_preprocess_breadcrumb().
*
* Replaces node title with Alternate Title value, if it exists
* !!!! needs to be fixed to work with easy breadcrumb !!!!!!
*/
function alternate_node_fields_preprocess_breadcrumb(&$variables){
$variables['#cache']['max-age'] = 0;
$request = \Drupal::request();
$title = '';
if ($route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) {
$title = \Drupal::service('title_resolver')->getTitle($request, $route);
// Replaces node title with Alternate Title value, if it exists
// Get the current route match.
$route_match = \Drupal::routeMatch();
if ($node = $route_match->getParameter('node')) {
// Check if the entity and the field exist
if ($node->hasField('field_alt_node_fields_title') && !$node->get('field_alt_node_fields_title')->isEmpty()) {
$field_value = $node->get('field_alt_node_fields_title')->value;
$title = $field_value;
}
}
}
if($variables['breadcrumb']){
foreach ($variables['breadcrumb'] as $key => &$value) {
if($value['text'] == 'Node'){
unset($variables['breadcrumb'][$key]);
}
}
if(!empty($title)){
$variables['breadcrumb'][] = array(
'text' => ''
);
$variables['breadcrumb'][] = array(
'text' => $title
);
}
}
}