SimplesitemapEntitiesForm.php 4.71 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 9
 * Class SimplesitemapEntitiesForm.
 *
gbyte.co's avatar
gbyte.co committed
10
 * @package Drupal\simple_sitemap\Form
11
 */
gbyte.co's avatar
gbyte.co committed
12
class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
13 14 15 16 17 18 19 20 21 22 23 24 25

  /**
   * {@inheritdoc}
   */
  public function getFormID() {
    return 'simple_sitemap_entities_form';
  }

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

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

28
    $form['simple_sitemap_entities']['entities'] = [
29
      '#title' => $this->t('Sitemap entities'),
30
      '#type' => 'fieldset',
31
      '#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>',
32
    ];
33

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

37
    $entity_type_labels = [];
38
    foreach ($this->generator->getSitemapEntityTypes() as $entity_type_id => $entity_type) {
39 40 41 42
      $entity_type_labels[$entity_type_id] = $entity_type->getLabel() ? : $entity_type_id;
    }
    asort($entity_type_labels);

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

45
    foreach ($entity_type_labels as $entity_type_id => $entity_type_label) {
46

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

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

55 56
      $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled'] = [
        '#type' => 'checkbox',
57 58
        '#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.'),
59
        '#default_value' => $this->generator->entityTypeIsEnabled($entity_type_id),
60
      ];
61 62

      if ($form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled']['#default_value']) {
63 64 65 66
        $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>";
67 68
      }

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

71
      if ($this->generator->entityTypeIsAtomic($entity_type_id)) {
72
        $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.');
73
        $this->formHelper->setEntityCategory('bundle')
74 75 76
          ->setEntityTypeId($entity_type_id)
          ->setBundleName($entity_type_id)
          ->displayEntitySettings($form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_settings'], TRUE);
77
        $form['#attached']['drupalSettings']['simple_sitemap']['atomic_entities'][] = $css_entity_type_id;
78 79
      }
    }
80

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

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
90
    $values = $form_state->getValues();
gbyte.co's avatar
gbyte.co committed
91
    foreach ($values as $field_name => $value) {
92 93 94
      if (substr($field_name, -strlen('_enabled')) == '_enabled') {
        $entity_type_id = substr($field_name, 0, -8);
        if ($value) {
95 96 97
          $this->generator->enableEntityType($entity_type_id);
          if ($this->generator->entityTypeIsAtomic($entity_type_id)) {
            $this->generator->setBundleSettings($entity_type_id, $entity_type_id, [
gbyte.co's avatar
gbyte.co committed
98 99 100
              'index' => TRUE,
              'priority' => $values[$entity_type_id . '_simple_sitemap_priority'],
            ]);
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
}