SimplesitemapEntitiesForm.php 4.2 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 41

/**
 * 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');

    $form['simple_sitemap_entities']['entities'] = array(
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. node) inclusion settings have to be set on their bundle pages (e.g. 'page'). Disabling an entity type on this page will irreversibly delete its sitemap settings including per-entity overrides.") . '</p>',
45 46
    );

47 48 49
    $sitemap_entity_types = Simplesitemap::getSitemapEntityTypes();
    $entity_types = $sitemap->getConfig('entity_types');
    $f = new Form();
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    foreach ($sitemap_entity_types as $entity_type_id => $entity_type) {
      $entity_type_label = $entity_type->getLabel() ? : $entity_type_id;
      $entity_type_enabled = isset($entity_types[$entity_type_id]);
      $form['simple_sitemap_entities']['entities'][$entity_type_id] = [
      '#type' => 'fieldset',
      '#title' => $entity_type_label,
    ];
      $form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled'] = [
        '#type' => 'checkbox',
        '#title' => t('Enable @entity_type_label support', array('@entity_type_label' => strtolower($entity_type_label))),
        '#description' => t('Sitemap settings for this entity type can be set on its bundle pages and overridden on its entity pages.'),
        '#default_value' => $entity_type_enabled,
      ];
      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);
      }
    }
    $f->displaySitemapRegenerationSetting($form['simple_sitemap_entities']['entities']);
73 74 75 76 77 78 79 80 81
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $sitemap = \Drupal::service('simple_sitemap.generator');
    $entity_types = $sitemap->getConfig('entity_types');
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    $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) {
          if (empty($entity_types[$entity_type_id])) {
            if (Simplesitemap::entityTypeIsAtomic($entity_type_id))
              // As entity type has no bundles, making it index by default with set priority.
              $entity_types[$entity_type_id][$entity_type_id] = ['index' => 1, 'priority' => $values[$entity_type_id . '_simple_sitemap_priority']];
            else // As entity has bundles, enabling settings on its bundle pages.
              $entity_types[$entity_type_id] = [];
          }
        }
        else {
          unset($entity_types[$entity_type_id]);
        }
98 99 100 101
      }
    }
    $sitemap->saveConfig('entity_types', $entity_types);
    parent::submitForm($form, $form_state);
102 103 104 105 106

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