You are here

PopupAdminForm.php in Popup 8

Contains \Drupal\popup\src\Form\PopupAdminForm.

Namespace

Drupal\popup\Form

File

src/Form/PopupAdminForm.php
View source
<?php

namespace Drupal\popup\Form;


/**
 * @file
 * Contains \Drupal\popup\src\Form\PopupAdminForm.
 */
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Messenger\MessengerInterface;

/**
 * Class PopupAdminForm.
 *
 * @package Drupal\popup\Form
 */
class PopupAdminForm extends ConfigFormBase {

  /**
   * Drupal\Core\Session\AccountProxyInterface definition.
   *
   * @var AccountProxyInterface $currentUser
   */
  protected $currentUser;

  /**
   * Drupal\Core\Messenger\MessengerInterface definition.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * Class constructor.
   *
   * @param ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param AccountProxyInterface $current_user
   *   The current user.
   * @param MessengerInterface $messenger
   *   The core messenger service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, AccountProxyInterface $current_user, MessengerInterface $messenger) {
    parent::__construct($config_factory);
    $this->configFactory = $config_factory;
    $this->currentUser = $current_user;
    $this->messenger = $messenger;
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Get current user data.
    $uid = $this->currentUser
      ->id();
    $this->messenger
      ->addMessage('Title: ' . $form_state
      ->getValue('title') . ', User UID: ' . $uid);
    $form['search_by_page_number_of_items'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Number of items to index per cron run'),
      '#options' => [
        10 => '10',
        20 => '20',
        50 => '50',
        100 => '100',
        200 => '200',
        500 => '500',
      ],
      '#default_value' => $this->configFactory
        ->get('search_by_page_number_of_items'),
      '#description' => $this
        ->t('The maximum number of items indexed in each pass of a :reports by Search by Page.', [
        ':reports' => 'cron maintenance task',
      ]),
      '#weight' => 1,
    ];
    $form['search_by_page_env'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Default environment'),
      '#options' => [
        1 => $this
          ->t('One'),
        2 => $this
          ->t('Two'),
        3 => $this
          ->t('Three'),
      ],
      '#default_value' => $this->configFactory
        ->get('search_by_page_env'),
      '#description' => $this
        ->t('The default environment is used for the Search by Page tab when using the core Search page.'),
      '#weight' => 2,
    ];
    $form['search_by_page_tab'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Search tab name'),
      '#default_value' => $this->configFactory
        ->get('search_by_page_tab') ?? $this
        ->t('Pages'),
      '#description' => $this
        ->t('If using Search by Page with the core Search module, the name of the tab where Search by Page results are shown.'),
      '#weight' => 2,
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->config('search_by_page.settings')
      ->set('search_by_page_number_of_items', $form_state
      ->getValue('search_by_page_number_of_items'))
      ->set('search_by_page_env', $form_state
      ->getValue('search_by_page_env'))
      ->set('search_by_page_tab', $form_state
      ->getValue('search_by_page_tab'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Classes

Namesort descending Description
PopupAdminForm Class PopupAdminForm.