SimplesitemapEntitiesForm.php 4.8 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 16 17 18 19 20 21 22 23 24

  /**
   * {@inheritdoc}
   */
  public function getFormID() {
    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',
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
    foreach ($entity_type_labels as $entity_type_id => $entity_type_label) {
45

46
      $css_entity_type_id = str_replace('_', '-', $entity_type_id);
47

48
      $form['simple_sitemap_entities']['entities'][$entity_type_id] = [
gbyte.co's avatar
gbyte.co committed
49 50 51 52
        '#type' => 'details',
        '#title' => $entity_type_label,
        '#open' => $this->generator->entityTypeIsEnabled($entity_type_id),
      ];
53

54 55
      $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled'] = [
        '#type' => 'checkbox',
56 57
        '#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]),
        '#description' => $this->t('Sitemap settings for this entity type can be set on its bundle pages and overridden on its entity pages.'),
58
        '#default_value' => $this->generator->entityTypeIsEnabled($entity_type_id),
59
      ];
60 61

      if ($form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled']['#default_value']) {
62 63 64 65
        $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled']['#suffix']
          = "<div id='warning-$css_entity_type_id'>"
          . $this->t("<strong>Warning:</strong> This entity type's sitemap settings including per-entity overrides will be deleted after hitting <em>Save</em>.")
          . "</div>";
66 67
      }

68
      $form['#attached']['drupalSettings']['simple_sitemap']['all_entities'][] = $css_entity_type_id;
69

70
      if ($this->entityHelper->entityTypeIsAtomic($entity_type_id)) {
71
        $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled']['#description'] = $this->t('Sitemap settings for this entity type can be set below and overridden on its entity pages.');
72
        $this->formHelper->setEntityCategory('bundle')
73 74 75
          ->setEntityTypeId($entity_type_id)
          ->setBundleName($entity_type_id)
          ->displayEntitySettings($form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_settings'], TRUE);
76
        $form['#attached']['drupalSettings']['simple_sitemap']['atomic_entities'][] = $css_entity_type_id;
77 78
      }
    }
79

80
    $this->formHelper->displayRegenerateNow($form['simple_sitemap_entities']['entities']);
81

82 83 84 85 86 87 88
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
89
    $values = $form_state->getValues();
gbyte.co's avatar
gbyte.co committed
90
    foreach ($values as $field_name => $value) {
91 92 93
      if (substr($field_name, -strlen('_enabled')) == '_enabled') {
        $entity_type_id = substr($field_name, 0, -8);
        if ($value) {
94
          $this->generator->enableEntityType($entity_type_id);
95
          if ($this->entityHelper->entityTypeIsAtomic($entity_type_id)) {
96
            $this->generator->setBundleSettings($entity_type_id, $entity_type_id, [
gbyte.co's avatar
gbyte.co committed
97 98
              'index' => TRUE,
              'priority' => $values[$entity_type_id . '_simple_sitemap_priority'],
99
              'changefreq' => $values[$entity_type_id . '_simple_sitemap_changefreq'],
gbyte.co's avatar
gbyte.co committed
100
            ]);
101 102
          }
        }
gbyte.co's avatar
gbyte.co committed
103
        else {
104
          $this->generator->disableEntityType($entity_type_id);
gbyte.co's avatar
gbyte.co committed
105
        }
106 107 108
      }
    }
    parent::submitForm($form, $form_state);
109 110 111

    // Regenerate sitemaps according to user setting.
    if ($form_state->getValue('simple_sitemap_regenerate_now')) {
112
      $this->generator->generateSitemap();
113
    }
114
  }
gbyte.co's avatar
gbyte.co committed
115

116
}