SimplesitemapEntitiesForm.php 4.32 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace Drupal\simple_sitemap\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\simple_sitemap\Simplesitemap;
8
use Drupal\simple_sitemap\Form;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

/**
 * SimplesitemapSettingsFrom
 */
class SimplesitemapEntitiesForm extends ConfigFormBase {

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

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['simple_sitemap.settings'];
  }

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

Pawel G's avatar
Pawel G committed
34
    $generator = \Drupal::service('simple_sitemap.generator');
35

Pawel G's avatar
Pawel G committed
36
    $form['simple_sitemap_entities']['entities'] = [
37
      '#title' => $this->t('Sitemap entities'),
38
      '#type' => 'fieldset',
39
      '#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
40
    ];
41

42
    $form['#attached']['library'][] = 'simple_sitemap/sitemapEntities';
43
    $form['#attached']['drupalSettings']['simple_sitemap'] = ['all_entities' => [], 'atomic_entities' => []];
44

45 46 47 48 49 50
    $entity_type_labels = [];
    foreach (Simplesitemap::getSitemapEntityTypes() as $entity_type_id => $entity_type) {
      $entity_type_labels[$entity_type_id] = $entity_type->getLabel() ? : $entity_type_id;
    }
    asort($entity_type_labels);

51
    $f = new Form();
52

53
    foreach ($entity_type_labels as $entity_type_id => $entity_type_label) {
54
      $form['simple_sitemap_entities']['entities'][$entity_type_id] = [
55
      '#type' => 'details',
56
      '#title' => $entity_type_label,
Pawel G's avatar
Pawel G committed
57
      '#open' => $generator->entityTypeIsEnabled($entity_type_id),
58 59 60
    ];
      $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled'] = [
        '#type' => 'checkbox',
61 62
        '#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.'),
Pawel G's avatar
Pawel G committed
63
        '#default_value' => $generator->entityTypeIsEnabled($entity_type_id),
64
      ];
65
      $form['#attached']['drupalSettings']['simple_sitemap']['all_entities'][] = str_replace('_', '-', $entity_type_id);
66
      if (Simplesitemap::entityTypeIsAtomic($entity_type_id)) {
67
        $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.');
68 69 70
        $f->setEntityCategory('bundle');
        $f->setEntityTypeId($entity_type_id);
        $f->setBundleName($entity_type_id);
Pawel G's avatar
Pawel G committed
71
        $f->displayEntitySettings($form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_settings'], TRUE);
72
        $form['#attached']['drupalSettings']['simple_sitemap']['atomic_entities'][] = str_replace('_', '-', $entity_type_id);
73 74
      }
    }
Pawel G's avatar
Pawel G committed
75
    $f->displayRegenerateNow($form['simple_sitemap_entities']['entities']);
76 77 78 79 80 81 82
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
Pawel G's avatar
Pawel G committed
83
    $generator = \Drupal::service('simple_sitemap.generator');
84 85 86 87 88
    $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) {
Pawel G's avatar
Pawel G committed
89
          $generator->enableEntityType($entity_type_id);
90
          if (Simplesitemap::entityTypeIsAtomic($entity_type_id)) {
Pawel G's avatar
Pawel G committed
91
            $generator->setBundleSettings($entity_type_id, $entity_type_id, [
92 93 94
                'index' => TRUE,
                'priority' => $values[$entity_type_id . '_simple_sitemap_priority']
              ]);
95 96
          }
        }
97
        else
Pawel G's avatar
Pawel G committed
98
          $generator->disableEntityType($entity_type_id);
99 100 101
      }
    }
    parent::submitForm($form, $form_state);
102 103 104

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