You are here

class AjaxLoaderSettingsForm in Ajax loader 8

Class AjaxLoaderSettingsForm.

@package Drupal\ajax_throbber\Form

Hierarchy

Expanded class hierarchy of AjaxLoaderSettingsForm

1 string reference to 'AjaxLoaderSettingsForm'
ajax_loader.routing.yml in ./ajax_loader.routing.yml
ajax_loader.routing.yml

File

src/Form/AjaxLoaderSettingsForm.php, line 16

Namespace

Drupal\ajax_loader\Form
View source
class AjaxLoaderSettingsForm extends ConfigFormBase {
  protected $throbberManager;

  /**
   * Function to construct.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ThrobberManagerInterface $throbber_manager) {
    parent::__construct($config_factory);
    $this->throbberManager = $throbber_manager;
  }

  /**
   * Function to create.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The container value.
   *
   * @return static
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('ajax_loader.throbber_manager'));
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $settings = $this
      ->config('ajax_loader.settings');
    $form['wrapper'] = [
      '#prefix' => '<div id="throbber-wrapper">',
      '#suffix' => '</div>',
    ];
    $form['wrapper']['throbber'] = [
      '#type' => 'select',
      '#title' => t('Throbber'),
      '#description' => t('Choose your throbber'),
      '#required' => TRUE,
      '#options' => $this->throbberManager
        ->getThrobberOptionList(),
      '#default_value' => $settings
        ->get('throbber'),
      '#ajax' => [
        'wrapper' => 'throbber-wrapper',
        'callback' => [
          $this,
          'ajaxThrobberChange',
        ],
      ],
    ];
    if (!empty($form_state
      ->getValue('throbber')) || !empty($settings
      ->get('throbber'))) {
      $plugin_id = !empty($form_state
        ->getValue('throbber')) ? $form_state
        ->getValue('throbber') : $settings
        ->get('throbber');
      if ($this->throbberManager
        ->getDefinition($plugin_id, FALSE)) {

        // Show preview of throbber.
        if (!empty($form_state
          ->getValue('throbber'))) {
          $throbber = $this->throbberManager
            ->loadThrobberInstance($form_state
            ->getValue('throbber'));
        }
        else {
          $throbber = $this->throbberManager
            ->loadThrobberInstance($settings
            ->get('throbber'));
        }
        $form['wrapper']['throbber']['#attached']['library'] = [
          'ajax_loader/ajax_loader.admin',
        ];
        $form['wrapper']['throbber']['#suffix'] = '<span class="throbber-example">' . $throbber
          ->getMarkup() . '</span>';
      }
    }
    $form['hide_ajax_message'] = [
      '#type' => 'checkbox',
      '#title' => t('Never show ajax loading message'),
      '#description' => t('Choose whether you want to hide the loading ajax message even when it is set.'),
      '#default_value' => $settings
        ->get('hide_ajax_message') ?: 0,
    ];
    $form['always_fullscreen'] = [
      '#type' => 'checkbox',
      '#title' => t('Always show loader as overlay (fullscreen)'),
      '#description' => t('Choose whether you want to show the loader as an overlay, no matter what the settings of the loader are.'),
      '#default_value' => $settings
        ->get('always_fullscreen') ?: 0,
    ];
    $form['show_admin_paths'] = [
      '#type' => 'checkbox',
      '#title' => t('Use ajax loader on admin pages'),
      '#description' => t('Choose whether you also want to show the loader on admin pages or still like to use the default core loader.'),
      '#default_value' => $settings
        ->get('show_admin_paths') ?: 0,
    ];
    $form['throbber_position'] = [
      '#type' => 'textfield',
      '#title' => t('Throbber position'),
      '#required' => TRUE,
      '#description' => t('Allows you to change the position where the throbber is inserted. A valid css selector must be used here. The default value is: body'),
      '#default_value' => $settings
        ->get('throbber_position') ?: 'body',
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * Ajax callback when throbber is changed.
   *
   * @param array $form
   *   The form array.
   * @param array $form_state
   *   The form state array.
   */
  public function ajaxThrobberChange(array $form, array &$form_state) {
    return $form['wrapper'];
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->config('ajax_loader.settings')
      ->set('throbber', $form_state
      ->getValue('throbber'))
      ->set('hide_ajax_message', $form_state
      ->getValue('hide_ajax_message'))
      ->set('always_fullscreen', $form_state
      ->getValue('always_fullscreen'))
      ->set('show_admin_paths', $form_state
      ->getValue('show_admin_paths'))
      ->set('throbber_position', $form_state
      ->getValue('throbber_position'))
      ->save();

    // Clear cache, so that library is picked up.
    drupal_flush_all_caches();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AjaxLoaderSettingsForm::$throbberManager protected property
AjaxLoaderSettingsForm::ajaxThrobberChange public function Ajax callback when throbber is changed.
AjaxLoaderSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
AjaxLoaderSettingsForm::create public static function Function to create. Overrides ConfigFormBase::create
AjaxLoaderSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
AjaxLoaderSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AjaxLoaderSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
AjaxLoaderSettingsForm::__construct public function Function to construct. Overrides ConfigFormBase::__construct
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.