XmlSitemapEntitiesSettingsForm.php 6.65 KB
Newer Older
1
2
3
4
<?php

namespace Drupal\xmlsitemap\Form;

5
use Drupal\Component\Utility\SafeMarkup;
6
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
7
8
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Config\ConfigFactoryInterface;
9
use Drupal\Core\Entity\EntityTypeManagerInterface;
10
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
11
use Drupal\Core\Session\AnonymousUserSession;
12
use Drupal\Core\Entity\ContentEntityTypeInterface;
13
use Drupal\Core\Form\FormStateInterface;
14
use Drupal\Core\Url;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17

/**
18
 * Configure what entities will be included in sitemap.
19
 */
20
class XmlSitemapEntitiesSettingsForm extends ConfigFormBase implements ContainerInjectionInterface {
21

22
  /**
23
   * The entity type manager.
24
   *
25
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
26
   */
27
28
29
30
31
32
33
34
  protected $entityTypeManager;

  /**
   * The entity type bundle info.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;
35

36
37
38
39
40
41
42
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'xmlsitemap_config_entities_settings_form';
  }

43
  /**
44
   * Constructs a XmlSitemapEntitiesSettingsForm object.
45
46
47
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
48
49
50
51
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle info.
52
   */
53
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
54
    parent::__construct($config_factory);
55
56
57

    $this->entityTypeManager = $entity_type_manager;
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
58
59
60
61
62
63
64
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
65
66
67
      $container->get('config.factory'),
      $container->get('entity_type.manager'),
      $container->get('entity_type.bundle.info')
68
    );
69
70
  }

71
72
73
74
75
76
77
  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['xmlsitemap.settings'];
  }

78
79
80
  /**
   * {@inheritdoc}
   */
81
  public function buildForm(array $form, FormStateInterface $form_state) {
82
    $config = $this->config('xmlsitemap.settings');
83
    $entity_types = $this->entityTypeManager->getDefinitions();
84
85
    $labels = [];
    $default = [];
86
    $anonymous_user = new AnonymousUserSession();
87
    $bundles = $this->entityTypeBundleInfo->getAllBundleInfo();
88
89

    foreach ($entity_types as $entity_type_id => $entity_type) {
amateescu's avatar
amateescu committed
90
      if (!$entity_type instanceof ContentEntityTypeInterface || !isset($bundles[$entity_type_id])) {
91
92
93
94
        continue;
      }

      $labels[$entity_type_id] = $entity_type->getLabel() ? : $entity_type_id;
95
    }
96
97
98

    asort($labels);

99
    $form = [
100
      '#labels' => $labels,
101
    ];
102

103
    $form['entity_types'] = [
104
      '#title' => $this->t('Custom sitemap entities settings'),
105
      '#type' => 'checkboxes',
106
107
      '#options' => $labels,
      '#default_value' => $default,
108
    ];
109

110
    $form['settings'] = ['#tree' => TRUE];
111
112
113
114

    foreach ($labels as $entity_type_id => $label) {
      $entity_type = $entity_types[$entity_type_id];

115
      $form['settings'][$entity_type_id] = [
116
117
        '#type' => 'container',
        '#entity_type' => $entity_type_id,
118
119
        '#bundle_label' => $entity_type->getBundleLabel() ? $entity_type->getBundleLabel() : $label,
        '#title' => $entity_type->getBundleLabel() ? $entity_type->getBundleLabel() : $label,
120
121
122
123
124
        '#states' => [
          'visible' => [
            ':input[name="entity_types[' . $entity_type_id . ']"]' => ['checked' => TRUE],
          ],
        ],
125

126
        'types' => [
127
          '#type' => 'table',
128
          '#tableselect' => TRUE,
129
130
131
          '#default_value' => [],
          '#header' => [
            [
132
              'data' => $entity_type->getBundleLabel() ? $entity_type->getBundleLabel() : $label,
133
134
135
              'class' => ['bundle'],
            ],
            [
136
              'data' => $this->t('Sitemap settings'),
137
138
139
              'class' => ['operations'],
            ],
          ],
140
          '#empty' => $this->t('No content available.'),
141
142
        ],
      ];
143

144
      foreach ($bundles[$entity_type_id] as $bundle => $bundle_info) {
145
        $form['settings'][$entity_type_id][$bundle]['settings'] = [
146
147
          '#type' => 'item',
          '#label' => $bundle_info['label'],
148
        ];
149

150
151
        $form['settings'][$entity_type_id]['types'][$bundle] = [
          'bundle' => [
152
            '#markup' => $bundle_info['label'],
153
          ],
154
          'operations' => [
155
156
157
158
            '#type' => 'operations',
            '#links' => [
              'configure' => [
                'title' => $this->t('Configure'),
159
                'url' => Url::fromRoute('xmlsitemap.admin_settings_bundle', [
160
161
162
                  'entity' => $entity_type_id,
                  'bundle' => $bundle,
                  'query' => drupal_get_destination(),
163
164
165
                ]),
              ],
            ],
166
          ],
167
        ];
168
169
        $form['settings'][$entity_type_id]['types']['#default_value'][$bundle] = xmlsitemap_link_bundle_check_enabled($entity_type_id, $bundle);

170
171
172
        if (xmlsitemap_link_bundle_check_enabled($entity_type_id, $bundle)) {
          $default[$entity_type_id] = $entity_type_id;
        }
173
174
      }
    }
175
    $form['entity_types']['#default_value'] = $default;
176
    $form = parent::buildForm($form, $form_state);
177
    $form['actions']['submit']['#value'] = $this->t('Save');
178
179
180
181

    return $form;
  }

182
183
184
  /**
   * {@inheritdoc}
   */
185
  public function submitForm(array &$form, FormStateInterface $form_state) {
186
    $bundles = $this->entityTypeBundleInfo->getAllBundleInfo();
187
188
    $values = $form_state->getValues();
    $entity_values = $values['entity_types'];
189
    $config = $this->config('xmlsitemap.settings');
190
    $settings = $form_state->getValue('settings');
191
    foreach ($entity_values as $key => $value) {
192
193
      if ($value) {
        foreach ($bundles[$key] as $bundle_key => $bundle_value) {
194
          if (!$values['settings'][$key]['types'][$bundle_key]) {
195
196
            xmlsitemap_link_bundle_delete($key, $bundle_key, TRUE);
          }
197
198
199
200
201
          else {
            if (!xmlsitemap_link_bundle_check_enabled($key, $bundle_key)) {
              xmlsitemap_link_bundle_enable($key, $bundle_key);
            }
          }
202
203
204
205
        }
      }
      else {
        foreach ($bundles[$key] as $bundle_key => $bundle_value) {
206
          xmlsitemap_link_bundle_delete($key, $bundle_key, TRUE);
207
        }
208
      }
209
    }
210
    \Drupal::state()->set('xmlsitemap_regenerate_needed', TRUE);
211
212
    parent::submitForm($form, $form_state);
  }
amateescu's avatar
amateescu committed
213

214
}