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

      if ($form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled']['#default_value']) {
        $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled']['#suffix'] = "<div id='warning-$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>";
      }

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

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

    // Regenerate sitemaps according to user setting.
    if ($form_state->getValue('simple_sitemap_regenerate_now')) {
102
      $this->generator->generateSitemap();
103
    }
104
  }
Pawel G's avatar
Pawel G committed
105

106
}