Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / simple_sitemap / src / Form / SimplesitemapCustomLinksForm.php
index 0db0388c395fa52166b1dabb96b398e95062775c..da6a091041b629d5aece452dd7c1bbd71ad774fa 100644 (file)
@@ -13,7 +13,7 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
   /**
    * {@inheritdoc}
    */
-  public function getFormID() {
+  public function getFormId() {
     return 'simple_sitemap_custom_links_form';
   }
 
@@ -32,8 +32,16 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
     $form['simple_sitemap_custom']['custom_links'] = [
       '#type' => 'textarea',
       '#title' => $this->t('Relative Drupal paths'),
-      '#default_value' => $this->customLinksToString($this->generator->getCustomLinks()),
-      '#description' => $this->t("Please specify drupal internal (relative) paths, one per line. Do not forget to prepend the paths with a '/'. You can optionally add a priority (0.0 - 1.0) by appending it to the path after a space. The home page with the highest priority would be <em>/ 1.0</em>, the contact page with the default priority would be <em>/contact 0.5</em>."),
+      '#default_value' => $this->customLinksToString($this->generator->getCustomLinks(FALSE)),
+      '#description' => $this->t("Please specify drupal internal (relative) paths, one per line. Do not forget to prepend the paths with a '/'.<br/>Optionally link priority <em>(0.0 - 1.0)</em> can be added by appending it after a space.<br/> Optionally link change frequency <em>(always / hourly / daily / weekly / monthly / yearly / never)</em> can be added by appending it after a space.<br/><br/><strong>Examples:</strong><br/><em>/ 1.0 daily</em> -> home page with the highest priority and daily change frequency<br/><em>/contact</em> -> contact page with the default priority and no change frequency information"),
+    ];
+
+    $form['simple_sitemap_custom']['include_images'] = [
+      '#type' => 'select',
+      '#title' => $this->t('Include images'),
+      '#description' => $this->t('If a custom link points to an entity, include its referenced images in the sitemap.'),
+      '#default_value' => $this->generator->getSetting('custom_links_include_images', 0),
+      '#options' => [0 => $this->t('No'), 1 => $this->t('Yes')],
     ];
 
     $this->formHelper->displayRegenerateNow($form['simple_sitemap_custom']);
@@ -46,24 +54,35 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
    */
   public function validateForm(array &$form, FormStateInterface $form_state) {
     foreach ($this->stringToCustomLinks($form_state->getValue('custom_links')) as $i => $link_config) {
-      $placeholders = ['@line' => ++$i, '@path' => $link_config['path'], '@priority' => isset($link_config['priority']) ? $link_config['priority'] : ''];
+      $placeholders = [
+        '@line' => ++$i,
+        '@path' => $link_config['path'],
+        '@priority' => isset($link_config['priority']) ? $link_config['priority'] : '',
+        '@changefreq' => isset($link_config['changefreq']) ? $link_config['changefreq'] : '',
+        '@changefreq_options' => implode(', ', FormHelper::getChangefreqOptions()),
+      ];
 
       // Checking if internal path exists.
       if (!$this->pathValidator->isValid($link_config['path'])
 //      if (!$this->pathValidator->getUrlIfValidWithoutAccessCheck($link_config['path']) //todo
       // Path validator does not see a double slash as an error. Catching this to prevent breaking path generation.
        || strpos($link_config['path'], '//') !== FALSE) {
-        $form_state->setErrorByName('', $this->t("<strong>Line @line</strong>: The path <em>@path</em> does not exist.", $placeholders));
+        $form_state->setErrorByName('', $this->t('<strong>Line @line</strong>: The path <em>@path</em> does not exist.', $placeholders));
       }
 
       // Making sure the paths start with a slash.
-      if ($link_config['path'][0] != '/') {
+      if ($link_config['path'][0] !== '/') {
         $form_state->setErrorByName('', $this->t("<strong>Line @line</strong>: The path <em>@path</em> needs to start with a '/'.", $placeholders));
       }
 
       // Making sure the priority is formatted correctly.
       if (isset($link_config['priority']) && !FormHelper::isValidPriority($link_config['priority'])) {
-        $form_state->setErrorByName('', $this->t("<strong>Line @line</strong>: The priority setting <em>@priority</em> for path <em>@path</em> is incorrect. Set the priority from 0.0 to 1.0.", $placeholders));
+        $form_state->setErrorByName('', $this->t('<strong>Line @line</strong>: The priority setting <em>@priority</em> for path <em>@path</em> is incorrect. Set the priority from 0.0 to 1.0.', $placeholders));
+      }
+
+      // Making sure changefreq is formatted correctly.
+      if (isset($link_config['changefreq']) && !FormHelper::isValidChangefreq($link_config['changefreq'])) {
+        $form_state->setErrorByName('', $this->t('<strong>Line @line</strong>: The changefreq setting <em>@changefreq</em> for path <em>@path</em> is incorrect. The following are the correct values: <em>@changefreq_options</em>.', $placeholders));
       }
     }
   }
@@ -77,6 +96,7 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
     foreach ($custom_links as $link_config) {
       $this->generator->addCustomLink($link_config['path'], $link_config);
     }
+    $this->generator->saveSetting('custom_links_include_images', $form_state->getValue('include_images'));
     parent::submitForm($form, $form_state);
 
     // Regenerate sitemaps according to user setting.
@@ -99,10 +119,26 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
 
     $custom_links = [];
     foreach ($custom_links_string_lines as $i => &$line) {
-      $link_settings = explode(' ', $line, 2);
+      $link_settings = explode(' ', $line);
       $custom_links[$i]['path'] = $link_settings[0];
-      if (isset($link_settings[1]) && $link_settings[1] != '') {
+
+      // If two arguments are provided for a link, assume the first to be
+      // priority, the second to be changefreq.
+      if (!empty($link_settings[1]) && !empty($link_settings[2])) {
         $custom_links[$i]['priority'] = $link_settings[1];
+        $custom_links[$i]['changefreq'] = $link_settings[2];
+      }
+      else {
+        // If one argument is provided for a link, guess if it is priority or
+        // changefreq.
+        if (!empty($link_settings[1])) {
+          if (is_numeric($link_settings[1])) {
+            $custom_links[$i]['priority'] = $link_settings[1];
+          }
+          else {
+            $custom_links[$i]['changefreq'] = $link_settings[1];
+          }
+        }
       }
     }
     return $custom_links;
@@ -115,9 +151,13 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
   protected function customLinksToString(array $links) {
     $setting_string = '';
     foreach ($links as $custom_link) {
+      $setting_string .= $custom_link['path'];
       $setting_string .= isset($custom_link['priority'])
-        ? $custom_link['path'] . ' ' . $this->formHelper->formatPriority($custom_link['priority'])
-        : $custom_link['path'];
+        ? ' ' . $this->formHelper->formatPriority($custom_link['priority'])
+        : '';
+      $setting_string .= isset($custom_link['changefreq'])
+        ? ' ' . $custom_link['changefreq']
+        : '';
       $setting_string .= "\r\n";
     }
     return $setting_string;