SimplesitemapEntitiesForm.php 4.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

/**
 * @file
 * Contains \Drupal\simple_sitemap\Form\SimplesitemapEntitiesForm.
 */

namespace Drupal\simple_sitemap\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\simple_sitemap\Simplesitemap;
13
use Drupal\simple_sitemap\Form;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

/**
 * 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) {

    $sitemap = \Drupal::service('simple_sitemap.generator');

41
    $form['simple_sitemap_entities']['entities'] = [
42
      '#title' => t('Sitemap entities'),
43
      '#type' => 'fieldset',
44
      '#markup' => '<p>' . 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>',
45
    ];
46

47
    $form['#attached']['library'][] = 'simple_sitemap/sitemapEntities';
48
    $form['#attached']['drupalSettings']['simple_sitemap'] = ['all_entities' => [], 'atomic_entities' => []];
49

50 51 52 53 54 55
    $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);

56
    $f = new Form();
57

58
    foreach ($entity_type_labels as $entity_type_id => $entity_type_label) {
59
      $form['simple_sitemap_entities']['entities'][$entity_type_id] = [
60
      '#type' => 'details',
61
      '#title' => $entity_type_label,
62
      '#open' => $sitemap->entityTypeIsEnabled($entity_type_id),
63 64 65
    ];
      $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled'] = [
        '#type' => 'checkbox',
66
        '#title' => t('Enable @entity_type_label <em>(@entity_type_id)</em> support', ['@entity_type_label' => strtolower($entity_type_label), '@entity_type_id' => $entity_type_id]),
67
        '#description' => t('Sitemap settings for this entity type can be set on its bundle pages and overridden on its entity pages.'),
68
        '#default_value' => $sitemap->entityTypeIsEnabled($entity_type_id),
69
      ];
70
      $form['#attached']['drupalSettings']['simple_sitemap']['all_entities'][] = str_replace('_', '-', $entity_type_id);
71 72 73 74 75 76
      if (Simplesitemap::entityTypeIsAtomic($entity_type_id)) {
        $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled']['#description'] = t('Sitemap settings for this entity type can be set below and overridden on its entity pages.');
        $f->setEntityCategory('bundle');
        $f->setEntityTypeId($entity_type_id);
        $f->setBundleName($entity_type_id);
        $f->displayEntitySitemapSettings($form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_settings'], TRUE);
77
        $form['#attached']['drupalSettings']['simple_sitemap']['atomic_entities'][] = str_replace('_', '-', $entity_type_id);
78 79 80
      }
    }
    $f->displaySitemapRegenerationSetting($form['simple_sitemap_entities']['entities']);
81 82 83 84 85 86 87 88
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $sitemap = \Drupal::service('simple_sitemap.generator');
89 90 91 92 93
    $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) {
94 95 96 97 98 99
          $sitemap->enableEntityType($entity_type_id);
          if (Simplesitemap::entityTypeIsAtomic($entity_type_id)) {
            $sitemap->setBundleSettings($entity_type_id, $entity_type_id, [
                'index' => TRUE,
                'priority' => $values[$entity_type_id . '_simple_sitemap_priority']
              ]);
100 101
          }
        }
102 103
        else
          $sitemap->disableEntityType($entity_type_id);
104 105 106
      }
    }
    parent::submitForm($form, $form_state);
107 108 109 110 111

    // Regenerate sitemaps according to user setting.
    if ($form_state->getValue('simple_sitemap_regenerate_now')) {
      $sitemap->generateSitemap();
    }
112 113
  }
}