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

namespace Drupal\simple_sitemap_engines\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
10
use Drupal\simple_sitemap\Form\FormHelper;
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 83 84 85
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Form for managing search engine submission settings.
 */
class SimplesitemapEnginesForm extends ConfigFormBase {

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatter
   */
  protected $dateFormatter;

  /**
   * SimplesitemapEnginesForm constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\Core\Datetime\DateFormatter $date_formatter
   *   The date formatter service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, DateFormatter $date_formatter) {
    parent::__construct($config_factory);
    $this->entityTypeManager = $entity_type_manager;
    $this->dateFormatter = $date_formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('entity_type.manager'),
      $container->get('date.formatter')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'simple_sitemap_engines_settings_form';
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('simple_sitemap_engines.settings');
    $engines = $this->entityTypeManager->getStorage('simple_sitemap_engine')->loadMultiple();
    // Construct a non-associative array containing the enabled search engines.
    $engine_statuses = array_column($engines, 'status', 'id');
    $enabled_engines = array_keys(array_filter($engine_statuses));

    $form['engine_group'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Search engines'),
86
      '#prefix' => FormHelper::getDonationText(),
87 88 89 90 91 92 93 94 95 96 97 98 99 100
    ];
    $form['engine_group']['engines'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Submit the sitemap to the following engines'),
      '#options' => $engines,
      '#default_value' => $enabled_engines,
    ];

    $form['settings'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Submission settings'),
    ];
    $form['settings']['enabled'] = [
      '#type' => 'checkbox',
101
      '#title' => $this->t('Submit the sitemap to search engines'),
102 103
      '#default_value' => $config->get('enabled'),
    ];
104
    $form['settings']['submission_interval'] = [
105
      '#type' => 'select',
106 107 108
      '#title' => $this->t('Submission interval'),
      '#options' => FormHelper::getCronIntervalOptions(),
      '#default_value' => $config->get('submission_interval'),
109
      '#states' => [
110
        'visible' => [':input[name="enabled"]' => ['checked' => TRUE]],
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
      ],
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $engines = $this->entityTypeManager->getStorage('simple_sitemap_engine')->loadMultiple();
    foreach ($form_state->getValue('engines') as $engine => $enabled) {
      $engines[$engine]->status = (bool) $enabled;
      $engines[$engine]->save();
    }
    $config = $this->config('simple_sitemap_engines.settings');
    $config->set('enabled', $form_state->getValue('enabled'));
128
    $config->set('submission_interval', $form_state->getValue('submission_interval'));
129 130 131 132
    $config->save();
  }

}