Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / simple_sitemap / simple_sitemap.module
1 <?php
2
3 /**
4  * @file
5  * Main module file containing hooks.
6  */
7
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Entity\EntityInterface;
10 use Drupal\Core\Routing\RouteMatchInterface;
11 use Drupal\system\MenuInterface;
12
13 /**
14  * Implements hook_help.
15  */
16 function simple_sitemap_help($route_name, RouteMatchInterface $route_match) {
17   return $route_name === 'help.page.simple_sitemap' ?
18     check_markup(file_get_contents(dirname(__FILE__) . "/README.md")) : NULL;
19 }
20
21 /**
22  * Implements hook_form_alter.
23  *
24  * Adds sitemap settings to entity types that are supported via plugins.
25  */
26 function simple_sitemap_form_alter(&$form, FormStateInterface $form_state, $form_id) {
27
28   /**
29    * @var Drupal\simple_sitemap\Form\FormHelper $f
30    */
31   $f = \Drupal::service('simple_sitemap.form_helper')->processForm($form_state);
32   if (!$f->alteringForm()) {
33     return;
34   }
35
36   $form['simple_sitemap'] = [
37     '#type' => 'details',
38     '#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
39     '#title' => t('Simple XML sitemap'),
40     '#description' => $f->getEntityCategory() == 'instance' ? t('Settings for this entity can be overridden here.') : '',
41   ];
42
43   // Attach some js magic to forms.
44   // todo: JS not working on comment entity form, hence disabling.
45   if ($f->getEntityTypeId() != 'comment' || $f->getEntityCategory() != 'instance') {
46     $form['#attached']['library'][] = 'simple_sitemap/form';
47   }
48
49   // Only attach fieldset summary js to 'additional settings' vertical tabs.
50   if (isset($form['additional_settings'])) {
51     $form['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
52   }
53
54   $f->displayEntitySettings($form['simple_sitemap'])
55   // todo: do not show setting when creating new bundle.
56     ->displayRegenerateNow($form['simple_sitemap']);
57
58   // Add submission handler.
59   if (isset($form['actions']['submit']['#submit'])) {
60     foreach (array_keys($form['actions']) as $action) {
61       if ($action != 'preview'
62         && isset($form['actions'][$action]['#type'])
63         && $form['actions'][$action]['#type'] === 'submit') {
64         $form['actions'][$action]['#submit'][] = 'simple_sitemap_entity_form_submit';
65       }
66     }
67   }
68   // Fix for account page rendering other submit handlers not usable.
69   else {
70     $form['#submit'][] = 'simple_sitemap_entity_form_submit';
71   }
72 }
73
74 /**
75  * Form submission handler called in hook_form_alter.
76  */
77 function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_state) {
78
79   /**
80    * @var Drupal\simple_sitemap\Form\FormHelper $f
81    */
82   $f = \Drupal::service('simple_sitemap.form_helper')->processForm($form_state);
83
84   $values = $form_state->getValues();
85
86   // Fix for values appearing in a sub array on a commerce product entity.
87   $values = isset($values['simple_sitemap']) ? $values['simple_sitemap'] : $values;
88
89   // Only make changes in DB if sitemap settings actually changed.
90   if ($f->valuesChanged($form, $values)) {
91
92     /**
93      * @var \Drupal\simple_sitemap\Simplesitemap $generator
94      */
95     $generator = \Drupal::service('simple_sitemap.generator');
96
97     switch ($f->getEntityCategory()) {
98
99       case 'bundle':
100         $generator->setBundleSettings(
101           $f->getEntityTypeId(),
102           !empty($f->getBundleName()) ? $f->getBundleName() : $f->getFormEntityId(),
103           [
104             'index' => $values['simple_sitemap_index_content'],
105             'priority' => $values['simple_sitemap_priority']
106           ]
107         );
108         break;
109
110       case 'instance':
111         $generator->setEntityInstanceSettings(
112           $f->getEntityTypeId(),
113           !empty($f->getInstanceId()) ? $f->getInstanceId() : $f->getFormEntityId(),
114           [
115             'index' => $values['simple_sitemap_index_content'],
116             'priority' => $values['simple_sitemap_priority']
117           ]
118         );
119         break;
120     }
121
122     // Regenerate sitemaps according to user setting.
123     if ($values['simple_sitemap_regenerate_now']) {
124       $generator->generateSitemap();
125     }
126   }
127 }
128
129 /**
130  * Implements hook_cron.
131  */
132 function simple_sitemap_cron() {
133
134   /**
135    * @var \Drupal\simple_sitemap\Simplesitemap $generator
136    */
137   $generator = \Drupal::service('simple_sitemap.generator');
138   if ($generator->getSetting('cron_generate')) {
139     $generator->generateSitemap('backend');
140   }
141 }
142
143 /**
144  * Implements hook_entity_delete().
145  *
146  * Removes settings of the removed entity.
147  *
148  * @param \Drupal\Core\Entity\EntityInterface $entity
149  */
150 function simple_sitemap_entity_delete(EntityInterface $entity) {
151
152   /**
153    * @var \Drupal\simple_sitemap\Simplesitemap $generator
154    */
155   $generator = \Drupal::service('simple_sitemap.generator');
156   $generator->removeEntityInstanceSettings(
157     $entity->getEntityTypeId(), $entity->id()
158   );
159 }
160
161 /**
162  * Implements hook_entity_bundle_delete().
163  *
164  * Removes settings of the removed bundle.
165  */
166 function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
167   simple_sitemap_delete_bundle_config($entity_type_id, $bundle);
168 }
169
170 /**
171  * Implements hook_menu_delete().
172  *
173  * Removes settings for the removed menu.
174  *
175  * @param \Drupal\system\MenuInterface $menu
176  */
177 function simple_sitemap_menu_delete(MenuInterface $menu) {
178   simple_sitemap_delete_bundle_config('menu_link_content', $menu->id());
179 }
180
181 /**
182  * Helper function used by simple_sitemap_entity_bundle_delete() and
183  * simple_sitemap_menu_delete() hooks. This is needed, as menus are technically
184  * not bundles.
185  *
186  * @param string $entity_type_id
187  * @param string $bundle
188  */
189 function simple_sitemap_delete_bundle_config($entity_type_id, $bundle) {
190
191   /**
192    * @var \Drupal\simple_sitemap\Simplesitemap $generator
193    */
194   $generator = \Drupal::service('simple_sitemap.generator');
195   $deleted_bundle_settings = $generator->getBundleSettings($entity_type_id, $bundle);
196   if ($deleted_bundle_settings !== FALSE) {
197
198     // Delete bundle settings.
199     \Drupal::service('config.factory')->getEditable("simple_sitemap.bundle_settings.$entity_type_id.$bundle")->delete();
200
201     $message = "You may want to <a href='@url'>regenerate</a> your XML sitemap now.";
202     if ($generator->getSetting('cron_generate')) {
203       $message .= ' Otherwise the sitemap will be regenerated on the next cron run.';
204     }
205     drupal_set_message(t($message, ['@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap']));
206   }
207 }
208
209 /**
210  * Implements hook_robotstxt().
211  */
212 function simple_sitemap_robotstxt() {
213   return [
214     '# XML sitemap',
215     'Sitemap: ' . $GLOBALS['base_url'] . '/sitemap.xml',
216   ];
217 }