SimplesitemapEntitiesForm.php 2.23 KB
Newer Older
1 2 3 4 5 6 7 8 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
<?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;

/**
 * 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(
      '#title' => t('Supported entities'),
      '#type' => 'fieldset',
      '#markup' => '<p>' . t('XML sitemap settings will be added only to entity forms of entities enabled here. Disabling an entity on this page will irreversibly delete its sitemap settings.') . '</p>',
    );

    $options = [];
    $entity_types = Simplesitemap::getSitemapEntityTypes();
    foreach ($entity_types as $entity_type_id => $entity_type) {
      $options[$entity_type_id] = $entity_type->getLabel() ? : $entity_type_id;
    }

    $form['simple_sitemap_entities']['entities']['entities'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Enable sitemap settings'),
      '#description' => t(''),
      '#options' => $options,
      '#default_value' => array_keys($sitemap->getConfig('entity_types')),
    );

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

    foreach($form_state->getValue('entities') as $entity_type_name => $enable) {
      if (!$enable) {
        unset($entity_types[$entity_type_name]);
      }
      elseif (empty($entity_types[$entity_type_name])) {
        $entity_types[$entity_type_name] = [];
      }
    }

    $sitemap->saveConfig('entity_types', $entity_types);
    parent::submitForm($form, $form_state);
  }
}