1+ <?php
2+ use Drupal\views\ViewExecutable;
3+ use Drupal\Core\Render\BubbleableMetadata;
4+ use Drupal\node\NodeInterface;
5+ use Drupal\node\Entity\Node;
6+ use Drupal\Core\Routing\RouteObjectInterface;
7+
8+ /**
9+ * Implements hook_views_pre_render().
10+ *
11+ * Replaces node title and summary field with Alternate field values, if they exist
12+ */
13+ function alternate_node_fields_views_pre_render(\Drupal\views\ViewExecutable $view) {
14+ // Check if the current view is a node view and if it has results.
15+ if ($view->storage->get('base_table') === 'node_field_data' && !empty($view->result)) {
16+ foreach ($view->result as $row) {
17+ // Check if the row is a node entity.
18+ if (isset($row->_entity) && $row->_entity instanceof \Drupal\node\NodeInterface) {
19+ $node = $row->_entity;
20+
21+ // alternate title
22+ if ($node->hasField('field_alt_node_fields_title') && !$node->get('field_alt_node_fields_title')->isEmpty()) {
23+ $alternate_title = $node->get('field_alt_node_fields_title')->value;
24+ // Set the node title to the alternate field value.
25+ $node->setTitle($alternate_title);
26+ }
27+
28+ // alternate summary feed
29+ if ($node->hasField('field_alt_node_fields_feed_summa') && !$node->get('field_alt_node_fields_feed_summa')->isEmpty()) {
30+ $alternate_summary_feed = $node->get('field_alt_node_fields_feed_summa')->value;
31+ $node->field_summary_for_feed_1->value = $alternate_summary_feed;
32+ }
33+ }
34+ }
35+ }
36+ }
37+
38+ /**
39+ * Implements hook_tokens_alter().
40+ *
41+ * Replaces node title token for metatags with Alternate Title value, if it exists
42+ */
43+ function alternate_node_fields_tokens_alter(array &$replacements, array $context, BubbleableMetadata $bubbleable_metadata) {
44+ // Check if we are dealing with a node context.
45+ if (isset($context['data']['node']) && $context['data']['node'] instanceof NodeInterface) {
46+ /** @var \Drupal\node\NodeInterface $node */
47+ $node = $context['data']['node'];
48+
49+ if ($node->hasField('field_alt_node_fields_title') && !$node->get('field_alt_node_fields_title')->isEmpty()) {
50+ $custom_value = $node->get('field_alt_node_fields_title')->value;
51+ $replacements['[node:title]'] = $custom_value;
52+ }
53+
54+ // Add cacheability metadata if your dynamic content relies on it.
55+ $bubbleable_metadata->addCacheableDependency($node);
56+ }
57+ }
58+
59+ /**
60+ * Implements hook_preprocess_node().
61+ *
62+ * Replaces node title and summary field with Alternate field values, if they exist
63+ */
64+ function alternate_node_fields_preprocess_node(&$variables) {
65+ $node = $variables['node'];
66+
67+ // Check if the node has the alternate field and it's not empty.
68+ if ($node->hasField('field_alt_node_fields_title') && !$node->get('field_alt_node_fields_title')->isEmpty()) {
69+ $alternate_title = $node->get('field_alt_node_fields_title')->value;
70+ $variables['label'] = $alternate_title; // This replaces the node title in node.html.twig.
71+ }
72+
73+ // Check if the node has the alternate summary field and it's not empty.
74+ if ($node->hasField('field_alt_node_fields_feed_summa') && !$node->get('field_alt_node_fields_feed_summa')->isEmpty()) {
75+ $alternate_summary_feed = $node->get('field_alt_node_fields_feed_summa')->value;
76+ $variables['node']->field_summary_for_feed_1->setValue(['value' => $alternate_summary_feed]);
77+ // $variables['content']['field_summary_for_feed_1'] = $alternate_summary_feed; // This replaces the node title in node.html.twig.
78+ }
79+ }
80+
81+ /**
82+ * Implements hook_preprocess_search_result().
83+ *
84+ * Replaces node title with Alternate Title value, if it exists
85+ */
86+ function alternate_node_fields_preprocess_search_result(&$variables) {
87+ // Get the result entity (e.g., node, user, etc.)
88+ $result_entity = $variables['result']['node']; // Adjust 'node' based on the entity type
89+
90+ // Check if the entity and the field exist
91+ if ($result_entity && $result_entity->hasField('field_alt_node_fields_title') && !$result_entity->get('field_alt_node_fields_title')->isEmpty()) {
92+ $field_value = $result_entity->get('field_alt_node_fields_title')->value;
93+ $variables['title'] = $field_value;
94+ }
95+ }
96+
97+ /**
98+ * Implements hook_preprocess_breadcrumb().
99+ *
100+ * Replaces node title with Alternate Title value, if it exists
101+ * !!!! needs to be fixed to work with easy breadcrumb !!!!!!
102+ */
103+ function alternate_node_fields_preprocess_breadcrumb(&$variables){
104+ $variables['#cache']['max-age'] = 0;
105+
106+ $request = \Drupal::request();
107+ $title = '';
108+ if ($route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) {
109+ $title = \Drupal::service('title_resolver')->getTitle($request, $route);
110+
111+ // Replaces node title with Alternate Title value, if it exists
112+ // Get the current route match.
113+ $route_match = \Drupal::routeMatch();
114+ if ($node = $route_match->getParameter('node')) {
115+
116+ // Check if the entity and the field exist
117+ if ($node->hasField('field_alt_node_fields_title') && !$node->get('field_alt_node_fields_title')->isEmpty()) {
118+ $field_value = $node->get('field_alt_node_fields_title')->value;
119+ $title = $field_value;
120+ }
121+ }
122+ }
123+
124+ if($variables['breadcrumb']){
125+ foreach ($variables['breadcrumb'] as $key => &$value) {
126+ if($value['text'] == 'Node'){
127+ unset($variables['breadcrumb'][$key]);
128+ }
129+ }
130+ if(!empty($title)){
131+ $variables['breadcrumb'][] = array(
132+ 'text' => ''
133+ );
134+ $variables['breadcrumb'][] = array(
135+ 'text' => $title
136+ );
137+ }
138+ }
139+ }
0 commit comments