SimplesitemapEntitiesForm.php 8.7 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\simple_sitemap\Form;

5
use Symfony\Component\DependencyInjection\ContainerInterface;
6
use Drupal\Core\Form\FormStateInterface;
7 8
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\simple_sitemap\EntityHelper;
9 10

/**
gbyte.co's avatar
gbyte.co committed
11
 * Class SimplesitemapEntitiesForm
gbyte.co's avatar
gbyte.co committed
12
 * @package Drupal\simple_sitemap\Form
13
 */
gbyte.co's avatar
gbyte.co committed
14
class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
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
  /**
   * @var \Drupal\simple_sitemap\EntityHelper
   */
  protected $entityHelper;

  /**
   * SimplesitemapEntitiesForm constructor.
   * @param \Drupal\simple_sitemap\Simplesitemap $generator
   * @param \Drupal\simple_sitemap\Form\FormHelper $form_helper
   * @param \Drupal\simple_sitemap\EntityHelper $entity_helper
   */
  public function __construct(
    Simplesitemap $generator,
    FormHelper $form_helper,
    EntityHelper $entity_helper
  ) {
    parent::__construct(
      $generator,
      $form_helper
    );
    $this->entityHelper = $entity_helper;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('simple_sitemap.generator'),
      $container->get('simple_sitemap.form_helper'),
      $container->get('simple_sitemap.entity_helper')
    );
  }

50 51 52
  /**
   * {@inheritdoc}
   */
gbyte.co's avatar
gbyte.co committed
53
  public function getFormId() {
54 55 56 57 58 59 60 61
    return 'simple_sitemap_entities_form';
  }

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

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

64
    $form['simple_sitemap_entities']['entities'] = [
65
      '#title' => $this->t('Sitemap entities'),
66
      '#type' => 'fieldset',
67
      '#markup' => '<div class="description">' . $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>).') . '</div>',
68
    ];
69

70
    $form['#attached']['library'][] = 'simple_sitemap/sitemapEntities';
71
    $form['#attached']['drupalSettings']['simple_sitemap'] = ['all_entities' => [], 'atomic_entities' => []];
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86
    $variants = $this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE);
    $all_bundle_settings = $this->generator->setVariants(TRUE)->getBundleSettings(NULL, NULL, TRUE, TRUE);
    $indexed_bundles = [];
    foreach ($all_bundle_settings as $variant => $entity_types) {
      foreach ($entity_types as $entity_type_name => $bundles) {
        foreach ($bundles as $bundle_name => $bundle_settings) {
          if (!empty($bundle_settings['index'])) {
            $indexed_bundles[$entity_type_name][$bundle_name]['variants'][] = $this->t($variants[$variant]['label']);
            $indexed_bundles[$entity_type_name][$bundle_name]['bundle_label'] = $this->entityHelper->getBundleLabel($entity_type_name, $bundle_name);
          }
        }
      }
    }

87
    $entity_type_labels = [];
gbyte.co's avatar
gbyte.co committed
88
    foreach ($this->entityHelper->getSupportedEntityTypes() as $entity_type_id => $entity_type) {
89 90 91 92
      $entity_type_labels[$entity_type_id] = $entity_type->getLabel() ? : $entity_type_id;
    }
    asort($entity_type_labels);

93 94 95
    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);
96
      $css_entity_type_id = str_replace('_', '-', $entity_type_id);
97

98
      $form['simple_sitemap_entities']['entities'][$entity_type_id] = [
gbyte.co's avatar
gbyte.co committed
99 100
        '#type' => 'details',
        '#title' => $entity_type_label,
101
        '#open' => $enabled_entity_type,
gbyte.co's avatar
gbyte.co committed
102
      ];
103

104 105
      $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled'] = [
        '#type' => 'checkbox',
106
        '#title' => $this->t('Enable @entity_type_label <em>(@entity_type_id)</em> support', ['@entity_type_label' => $entity_type_label, '@entity_type_id' => $entity_type_id]),
107
        '#description' => $atomic_entity_type
108 109
          ? $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' => $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' => $entity_type_label]),
110
        '#default_value' => $enabled_entity_type,
111
      ];
112 113

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

115 116 117 118 119 120
        $indexed_bundles_string = '';
        if (isset($indexed_bundles[$entity_type_id])) {
          foreach ($indexed_bundles[$entity_type_id] as $bundle => $bundle_data) {
            $indexed_bundles_string .= '<br><em>' . $bundle_data['bundle_label'] . '</em> <span class="description">(' . $this->t('sitemap variants') . ': <em>' . implode($bundle_data['variants'], ', ') . '</em>)</span>';
          }
        }
121

122
        $bundle_info = '';
123 124
        if (!$atomic_entity_type) {
          $bundle_info .= '<div id="indexed-bundles-' . $css_entity_type_id . '">'
125 126 127
            . (!empty($indexed_bundles_string)
              ? $this->t("<em>@entity_type_label</em> bundles set to be indexed:", ['@entity_type_label' => $entity_type_label]) . ' ' . $indexed_bundles_string
              : $this->t('No <em>@entity_type_label</em> bundles are set to be indexed yet.', ['@entity_type_label' => $entity_type_label]))
128 129 130
            . '</div>';
        }

131
        if (!empty($indexed_bundles_string)) {
132 133 134
          $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>.")
135
              : $this->t("<strong>Warning:</strong> The sitemap settings and any per-entity overrides will be deleted for the following bundles:" . $indexed_bundles_string))
136 137 138 139
            . '</div>';
        }

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

142
      $form['#attached']['drupalSettings']['simple_sitemap']['all_entities'][] = $css_entity_type_id;
143

144
      if ($atomic_entity_type) {
145 146 147
        $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_settings']['#prefix'] = '<div id="indexed-bundles-' . $css_entity_type_id . '">';
        $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_settings']['#suffix'] = '</div>';

148 149 150
        $this->formHelper
          ->cleanUpFormInfo()
          ->setEntityCategory('bundle')
151 152
          ->setEntityTypeId($entity_type_id)
          ->setBundleName($entity_type_id)
153
          ->negotiateSettings()
154
          ->displayEntitySettings(
155
            $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_settings']
156
          );
157 158
      }
    }
159

160
    $this->formHelper->displayRegenerateNow($form['simple_sitemap_entities']['entities']);
161

162 163 164 165 166 167 168
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
169
    $values = $form_state->getValues();
gbyte.co's avatar
gbyte.co committed
170
    foreach ($values as $field_name => $value) {
gbyte.co's avatar
gbyte.co committed
171
      if (substr($field_name, -strlen('_enabled')) === '_enabled') {
172 173
        $entity_type_id = substr($field_name, 0, -8);
        if ($value) {
174
          $this->generator->enableEntityType($entity_type_id);
175
          if ($this->entityHelper->entityTypeIsAtomic($entity_type_id)) {
176 177 178 179 180 181 182 183 184 185
            foreach ($this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE) as $variant => $definition) {
              if (isset($values['index_' . $variant . '_' . $entity_type_id . '_settings'])) {
                $this->generator
                  ->setVariants($variant)
                  ->setBundleSettings($entity_type_id, $entity_type_id, [
                    'index' => (bool) $values['index_' . $variant . '_' . $entity_type_id . '_settings'],
                    'priority' => $values['priority_' . $variant . '_' . $entity_type_id . '_settings'],
                    'changefreq' => $values['changefreq_' . $variant . '_' . $entity_type_id . '_settings'],
                    'include_images' => (bool) $values['include_images_' . $variant . '_' . $entity_type_id . '_settings'],
                    ]);
186 187
              }
            }
188 189
          }
        }
gbyte.co's avatar
gbyte.co committed
190
        else {
191
          $this->generator->disableEntityType($entity_type_id);
gbyte.co's avatar
gbyte.co committed
192
        }
193 194 195
      }
    }
    parent::submitForm($form, $form_state);
196 197 198

    // Regenerate sitemaps according to user setting.
    if ($form_state->getValue('simple_sitemap_regenerate_now')) {
gbyte.co's avatar
gbyte.co committed
199
      $this->generator->generateSitemap();
200
    }
201
  }
gbyte.co's avatar
gbyte.co committed
202

203
}