SimplesitemapEntitiesForm.php 4.13 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

namespace Drupal\simple_sitemap\Form;

use Drupal\Core\Form\FormStateInterface;

/**
 * SimplesitemapSettingsFrom
 */
gbyte.co's avatar
gbyte.co committed
10
class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
11 12 13 14 15 16 17 18 19 20 21 22 23

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

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

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

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

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

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

41
    $this->form->processForm($form_state);
42

43
    foreach ($entity_type_labels as $entity_type_id => $entity_type_label) {
44
      $form['simple_sitemap_entities']['entities'][$entity_type_id] = [
45
      '#type' => 'details',
46
      '#title' => $entity_type_label,
47
      '#open' => $this->generator->entityTypeIsEnabled($entity_type_id),
48 49 50
    ];
      $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled'] = [
        '#type' => 'checkbox',
51 52
        '#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.'),
53
        '#default_value' => $this->generator->entityTypeIsEnabled($entity_type_id),
54
      ];
55
      $form['#attached']['drupalSettings']['simple_sitemap']['all_entities'][] = str_replace('_', '-', $entity_type_id);
56
      if ($this->generator->entityTypeIsAtomic($entity_type_id)) {
57
        $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.');
58
        $this->form->setEntityCategory('bundle')
59 60 61
          ->setEntityTypeId($entity_type_id)
          ->setBundleName($entity_type_id)
          ->displayEntitySettings($form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_settings'], TRUE);
62
        $form['#attached']['drupalSettings']['simple_sitemap']['atomic_entities'][] = str_replace('_', '-', $entity_type_id);
63 64
      }
    }
65
    $this->form->displayRegenerateNow($form['simple_sitemap_entities']['entities']);
66 67 68 69 70 71 72
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
73 74 75 76 77
    $values = $form_state->getValues();
    foreach($values as $field_name => $value) {
      if (substr($field_name, -strlen('_enabled')) == '_enabled') {
        $entity_type_id = substr($field_name, 0, -8);
        if ($value) {
78 79 80
          $this->generator->enableEntityType($entity_type_id);
          if ($this->generator->entityTypeIsAtomic($entity_type_id)) {
            $this->generator->setBundleSettings($entity_type_id, $entity_type_id, [
81 82 83
                'index' => TRUE,
                'priority' => $values[$entity_type_id . '_simple_sitemap_priority']
              ]);
84 85
          }
        }
86
        else
87
          $this->generator->disableEntityType($entity_type_id);
88 89 90
      }
    }
    parent::submitForm($form, $form_state);
91 92 93

    // Regenerate sitemaps according to user setting.
    if ($form_state->getValue('simple_sitemap_regenerate_now')) {
94
      $this->generator->generateSitemap();
95
    }
96 97
  }
}