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;

/**
Pawel G's avatar
Pawel G committed
8 9
 * Class SimplesitemapEntitiesForm.
 *
Pawel G's avatar
Pawel G committed
10
 * @package Drupal\simple_sitemap\Form
11
 */
Pawel G's avatar
Pawel G 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();
Pawel G's avatar
Pawel G committed
27

Pawel G's avatar
Pawel G committed
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>',
Pawel G's avatar
Pawel G committed
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
      $css_entity_type_id = str_replace('_', '-', $entity_type_id);
47
      $form['simple_sitemap_entities']['entities'][$entity_type_id] = [
Pawel G's avatar
Pawel G committed
48 49 50 51
        '#type' => 'details',
        '#title' => $entity_type_label,
        '#open' => $this->generator->entityTypeIsEnabled($entity_type_id),
      ];
52 53
      $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled'] = [
        '#type' => 'checkbox',
54 55
        '#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.'),
56
        '#default_value' => $this->generator->entityTypeIsEnabled($entity_type_id),
57
      ];
58 59

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

66
      $form['#attached']['drupalSettings']['simple_sitemap']['all_entities'][] = $css_entity_type_id;
67
      if ($this->generator->entityTypeIsAtomic($entity_type_id)) {
68
        $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.');
69
        $this->formHelper->setEntityCategory('bundle')
70 71 72
          ->setEntityTypeId($entity_type_id)
          ->setBundleName($entity_type_id)
          ->displayEntitySettings($form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_settings'], TRUE);
73
        $form['#attached']['drupalSettings']['simple_sitemap']['atomic_entities'][] = $css_entity_type_id;
74 75
      }
    }
76
    $this->formHelper->displayRegenerateNow($form['simple_sitemap_entities']['entities']);
77 78 79 80 81 82 83
    return parent::buildForm($form, $form_state);
  }

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

    // Regenerate sitemaps according to user setting.
    if ($form_state->getValue('simple_sitemap_regenerate_now')) {
106
      $this->generator->generateSitemap();
107
    }
108
  }
Pawel G's avatar
Pawel G committed
109

110
}