SimplesitemapEntitiesForm.php 6.23 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace Drupal\simple_sitemap\Form;

use Drupal\Core\Form\FormStateInterface;

/**
gbyte.co's avatar
gbyte.co committed
8
 * Class SimplesitemapEntitiesForm
gbyte.co's avatar
gbyte.co committed
9
 * @package Drupal\simple_sitemap\Form
10
 */
gbyte.co's avatar
gbyte.co committed
11
class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
12 13 14 15

  /**
   * {@inheritdoc}
   */
gbyte.co's avatar
gbyte.co committed
16
  public function getFormId() {
17 18 19 20 21 22 23 24
    return 'simple_sitemap_entities_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

25
    $form['simple_sitemap_entities']['#prefix'] = $this->getDonationText();
gbyte.co's avatar
gbyte.co committed
26

27
    $form['simple_sitemap_entities']['entities'] = [
28
      '#title' => $this->t('Sitemap entities'),
29
      '#type' => 'fieldset',
gbyte.co's avatar
gbyte.co committed
30
      '#markup' => '<p>' . $this->t('Simple XML sitemap settings will be added only to entity forms of entity types enabled here. For all entity types featuring bundles (e.g. <em>node</em>) sitemap settings have to be set on their bundle pages (e.g. <em>page</em>).') . '</p>',
31
    ];
32

33
    $form['#attached']['library'][] = 'simple_sitemap/sitemapEntities';
34
    $form['#attached']['drupalSettings']['simple_sitemap'] = ['all_entities' => [], 'atomic_entities' => []];
35

36
    $entity_type_labels = [];
gbyte.co's avatar
gbyte.co committed
37
    foreach ($this->entityHelper->getSupportedEntityTypes() as $entity_type_id => $entity_type) {
38 39 40 41
      $entity_type_labels[$entity_type_id] = $entity_type->getLabel() ? : $entity_type_id;
    }
    asort($entity_type_labels);

42
    $this->formHelper->processForm($form_state);
43

44
    $bundle_settings = $this->generator->getBundleSettings();
45

46 47 48 49
    foreach ($entity_type_labels as $entity_type_id => $entity_type_label) {
;
      $enabled_entity_type = $this->generator->entityTypeIsEnabled($entity_type_id);
      $atomic_entity_type = $this->entityHelper->entityTypeIsAtomic($entity_type_id);
50
      $css_entity_type_id = str_replace('_', '-', $entity_type_id);
51

52
      $form['simple_sitemap_entities']['entities'][$entity_type_id] = [
gbyte.co's avatar
gbyte.co committed
53 54
        '#type' => 'details',
        '#title' => $entity_type_label,
55
        '#open' => $enabled_entity_type,
gbyte.co's avatar
gbyte.co committed
56
      ];
57

58 59
      $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled'] = [
        '#type' => 'checkbox',
60
        '#title' => $this->t('Enable @entity_type_label <em>(@entity_type_id)</em> support', ['@entity_type_label' => strtolower($entity_type_label), '@entity_type_id' => $entity_type_id]),
61 62 63
        '#description' => $atomic_entity_type
          ? $this->t('Sitemap settings for the entity type <em>@entity_type_label</em> can be set below and overridden on its entity pages.', ['@entity_type_label' => strtolower($entity_type_label)])
          : $this->t('Sitemap settings for the entity type <em>@entity_type_label</em> can be set on its bundle pages and overridden on its entity pages.', ['@entity_type_label' => strtolower($entity_type_label)]),
64
        '#default_value' => $enabled_entity_type,
65
      ];
66 67

      if ($form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled']['#default_value']) {
68 69 70

        $bundle_info = '';
        $indexed_bundles = isset($bundle_settings[$entity_type_id])
71 72
          ? implode(array_keys(array_filter($bundle_settings[$entity_type_id], function ($val) {return $val['index'];})), ', ')
          : '';
73 74 75

        if (!$atomic_entity_type) {
          $bundle_info .= '<div id="indexed-bundles-' . $css_entity_type_id . '">'
76
            . (!empty($indexed_bundles)
77
              ? $this->t("<em>@entity_type_label</em> bundles set to be indexed:", ['@entity_type_label' => ucfirst(strtolower($entity_type_label))]) . ' ' . '<em>' . $indexed_bundles . '</em>'
78
              : $this->t('No <em>@entity_type_label</em> bundles are set to be indexed yet.', ['@entity_type_label' => strtolower($entity_type_label)]))
79 80 81 82 83 84 85
            . '</div>';
        }

        if (!empty($indexed_bundles)) {
          $bundle_info .= '<div id="warning-' . $css_entity_type_id . '">'
            . ($atomic_entity_type
              ? $this->t("<strong>Warning:</strong> This entity type's sitemap settings including per-entity overrides will be deleted after hitting <em>Save</em>.")
86
              : $this->t("<strong>Warning:</strong> The sitemap settings for <em>@bundles</em> and any per-entity overrides will be deleted after hitting <em>Save</em>.",
87 88 89 90 91
                ['@bundles' => $indexed_bundles]))
            . '</div>';
        }

        $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled']['#suffix'] = $bundle_info;
92 93
      }

94
      $form['#attached']['drupalSettings']['simple_sitemap']['all_entities'][] = $css_entity_type_id;
95

96
      if ($atomic_entity_type) {
97
        $this->formHelper->setEntityCategory('bundle')
98 99 100
          ->setEntityTypeId($entity_type_id)
          ->setBundleName($entity_type_id)
          ->displayEntitySettings($form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_settings'], TRUE);
101
        $form['#attached']['drupalSettings']['simple_sitemap']['atomic_entities'][] = $css_entity_type_id;
102 103
      }
    }
104

105
    $this->formHelper->displayRegenerateNow($form['simple_sitemap_entities']['entities']);
106

107 108 109 110 111 112 113
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
114
    $values = $form_state->getValues();
gbyte.co's avatar
gbyte.co committed
115
    foreach ($values as $field_name => $value) {
gbyte.co's avatar
gbyte.co committed
116
      if (substr($field_name, -strlen('_enabled')) === '_enabled') {
117 118
        $entity_type_id = substr($field_name, 0, -8);
        if ($value) {
119
          $this->generator->enableEntityType($entity_type_id);
120
          if ($this->entityHelper->entityTypeIsAtomic($entity_type_id)) {
121
            $this->generator->setBundleSettings($entity_type_id, $entity_type_id, [
gbyte.co's avatar
gbyte.co committed
122 123
              'index' => TRUE,
              'priority' => $values[$entity_type_id . '_simple_sitemap_priority'],
124
              'changefreq' => $values[$entity_type_id . '_simple_sitemap_changefreq'],
125
              'include_images' => (bool) $values[$entity_type_id . '_simple_sitemap_include_images'],
gbyte.co's avatar
gbyte.co committed
126
            ]);
127 128
          }
        }
gbyte.co's avatar
gbyte.co committed
129
        else {
130
          $this->generator->disableEntityType($entity_type_id);
gbyte.co's avatar
gbyte.co committed
131
        }
132 133 134
      }
    }
    parent::submitForm($form, $form_state);
135 136 137

    // Regenerate sitemaps according to user setting.
    if ($form_state->getValue('simple_sitemap_regenerate_now')) {
138
      $this->generator->generateSitemap();
139
    }
140
  }
gbyte.co's avatar
gbyte.co committed
141

142
}