Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/dds / src / Form / EnableUtilitiesForm.php
Size: Mime:
<?php


namespace Drupal\dds\Form;


use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class EnableUtilitiesForm extends ConfigFormBase {

  /**
   * Returns a unique string identifying the form.
   *
   * The returned ID should be a unique string that can be a valid PHP function
   * name, since it's used in hook implementation names such as
   * hook_form_FORM_ID_alter().
   *
   * @return string
   *   The unique string identifying the form.
   */
  public function getFormId() {
    return 'enable_utilities_form';
  }

  /**
   * Gets the configuration names that will be editable.
   *
   * @return array
   *   An array of configuration object names that are editable if called in
   *   conjunction with the trait's config() method.
   */
  protected function getEditableConfigNames() {
    return [
      'dds.utilities'
    ];
  }

  /**
   * Form constructor.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array
   *   The form structure.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('dds.utilities');

    $enabled = [];
    foreach ($config->get('enabled') as $key => $value) {
      if ($value === $key) {
        $enabled[] = $key;
      }
    }

    $form['wrapper'] = [
      '#type' => 'details',
      '#open' => FALSE,
      '#title' => $this->t('Enable utilities (@count)', ['@count' => count($enabled)]),
      '#description' => $this->t('Select the utilities you wish to enable.')
    ];

    $form['wrapper']['utilities'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Utilities'),
      '#title_display' => 'invisible',
      '#options' => [
        'anti_autodiscover' => $this->t('Anti Autodiscovery'),
        'nofollow_noindex' => $this->t('Configurable No-follow / No-index')
      ]
    ];

    if (!empty($config->get('enabled'))) {
      $form['wrapper']['utilities']['#default_value'] = $config->get('enabled');
    }

    $form['wrapper']['submit_description'] = [
      '#type' => 'container',
      'desc' => ['#markup' => $this->t('The routing cache will be rebuilt upon saving this form, this is required for it to update any new routes and menu items for the utilities.')],
    ];

    $form['wrapper']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save changes'),
      '#attributes' => [
        'class' => [
          'button--primary'
        ],
        'style' => [
          'margin-left: 0;',
          'margin-top: 1em;'
        ],
      ],
    ];

    return $form;
  }

  /**
   * Form submission handler.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->configFactory->getEditable('dds.utilities')
      ->set('enabled', $form_state->getValue('utilities'))
      ->save();

    // Only rebuild routing cache.
    \Drupal::service('router.builder')->rebuild();

    parent::submitForm($form, $form_state);
  }
}