-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain_xmlsitemap.module
122 lines (107 loc) · 3.24 KB
/
domain_xmlsitemap.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
120
121
122
<?php
/**
* @file
* Contains domain_xmlsitemap.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Database\Query\AlterableInterface;
/**
* Implements hook_help().
*/
function domain_xmlsitemap_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the domain_xmlsitemap module.
case 'help.page.domain_xmlsitemap':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Provides domain contexts for XML sitemaps.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_xmlsitemap_context_info().
*/
function domain_xmlsitemap_xmlsitemap_context_info() {
$info['domain'] = [
'label' => t('Domain'),
'default' => 0,
];
return $info;
}
/**
* Implements hook_xmlsitemap_context().
*/
function domain_xmlsitemap_xmlsitemap_context() {
$context = [];
$active_id = \Drupal::service('domain.negotiator')->getActiveId();
// Ensure it's a valid domain, and cache it.
$domain = \Drupal::service('domain.loader')->load($active_id);
if ($domain instanceof EntityInterface) {
$context['domain'] = $active_id;
}
return $context;
}
/**
* Implements hook_xmlsitemap_context_url_options().
*/
function domain_xmlsitemap_xmlsitemap_context_url_options(array $context) {
$options = [];
if (isset($context['domain'])) {
$domain = \Drupal::service('domain.loader')->load($context['domain']);
if ($domain instanceof EntityInterface) {
$options['base_url'] = trim($domain->getPath(), '/');
}
}
return $options;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function domain_xmlsitemap_form_xmlsitemap_sitemap_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$domains = \Drupal::service('domain.loader')->loadMultipleSorted();
$options = [];
foreach ($domains as $domain) {
$options[$domain->Id()] = $domain->getPath();
}
if ($entity = $form_state->getFormObject()->getEntity()) {
$default = $entity->getContext()['domain'];
}
else {
$default = 0;
}
$form['context']['domain'] = [
'#type' => 'select',
'#title' => t('Domain'),
'#options' => $options,
'#default_value' => $default,
'#access' => !empty($options),
];
}
/**
* Implements hook_query_TAG_alter().
*/
function domain_xmlsitemap_query_xmlsitemap_generate_alter(AlterableInterface $query) {
$sitemap = $query->getMetaData('sitemap');
if (!isset($sitemap->context['domain'])) {
return;
}
// Get the domain from the context.
$domain = \Drupal::service('domain.loader')->load($sitemap->context['domain']);
if (!($domain instanceof EntityInterface)) {
return;
}
// Let domain_access, via node_access control whether nodes can be seen.
$query->leftJoin('node_access', 'na', "x.type = 'node' AND x.id = na.nid");
// Remove the existing x.access condition.
$where =& $query->conditions();
unset($where[0]);
// Restrict results to the domain in the context.
$node_access = new Condition('AND');
$node_access->condition('na.gid', $domain->getDomainId())
->condition('na.realm', 'domain_id');
$query->condition($node_access);
}