-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopeny_clover.theme
91 lines (78 loc) · 2.63 KB
/
openy_clover.theme
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
<?php
/**
* @file
* OpenY Clover Theme File.
*/
use Drupal\Core\Url;
use Drupal\Core\Database\Database;
/**
* Implements hook_preprocess_node() for the Blog Content Type.
*/
function openy_clover_preprocess_node(&$variables) {
$variables['is_case_study'] = openy_clover_is_case_study($variables['node']);
if ($variables['node']->getType() == 'blog') {
$back_link_title = t('Back to News & Events');
$variables['back_link_title'] = $back_link_title;
// @todo move this url out of the theme.
$variables['back_link_path'] = Url::fromUri('internal:/news_events')->toString();
$current_nid = $variables['node']->nid->value;
$connection = Database::getConnection();
$query = $connection->select('node', 'n');
$query->addField('n', 'nid');
$query->condition('n.type', 'blog');
$blog_nids = $query->execute()->fetchAll(PDO::FETCH_ASSOC);
$array_nid = [];
foreach ($blog_nids as $blog_nid) {
$array_nid[] = $blog_nid['nid'];
}
sort($array_nid);
$current_key = array_search($current_nid, $array_nid);
if (isset($array_nid[$current_key - 1])) {
$prev = t('< Previous Post');
$variables['prev'] = $prev;
$variables['prev_link'] = Url::fromRoute('entity.node.canonical', ['node' => $array_nid[$current_key - 1]])->toString();
}
if (isset($array_nid[$current_key + 1])) {
$next = t('Next Post >');
$variables['next'] = $next;
$variables['next_link'] = Url::fromRoute('entity.node.canonical', ['node' => $array_nid[$current_key + 1]])->toString();
}
}
}
function openy_clover_preprocess_views_view_unformatted(&$variables) {
$variables['back_link_path'] = Url::fromUri('internal:/news_events')->toString();
}
/**
* Checks if node is case study.
*
* @param \Drupal\node\NodeInterface $node
* Node.
* @return bool
* True if node is case study.
*/
function openy_clover_is_case_study($node) {
$bundle = $node->bundle();
if ($bundle !== 'landing_page') {
return FALSE;
}
if (!$node->hasField('field_content')) {
return FALSE;
}
$field_content = $node->get('field_content');
if ($field_content->isEmpty()){
return FALSE;
}
$manager = Drupal::service('entity_type.manager');
$paragraph_storage = $manager->getStorage('paragraph');
foreach ($field_content as $field_item) {
$value = $field_item->getValue();
$revision_id = $value['target_revision_id'];
/** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
$paragraph = $paragraph_storage->loadRevision($revision_id);
$paragraph_bundle = $paragraph->bundle();
if ($paragraph_bundle === 'back_to') {
return TRUE;
}
}
return FALSE;
}