You are here

class ResponsiveMenusAdminForm in Responsive Menus 8

Class ResponsiveMenusAdminForm.

@package Drupal\responsive_menus\Form

Hierarchy

Expanded class hierarchy of ResponsiveMenusAdminForm

1 string reference to 'ResponsiveMenusAdminForm'
responsive_menus.routing.yml in ./responsive_menus.routing.yml
responsive_menus.routing.yml

File

src/Form/ResponsiveMenusAdminForm.php, line 16

Namespace

Drupal\responsive_menus\Form
View source
class ResponsiveMenusAdminForm extends ConfigFormBase {

  /**
   * The Responsive Menus plugin manager.
   *
   * @var \Drupal\responsive_menus\ResponsiveMenusPluginManager
   */
  protected $pluginManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(ConfigFactoryInterface $config_factory, ResponsiveMenusPluginManager $plugin_manager) {
    parent::__construct($config_factory);
    $this->pluginManager = $plugin_manager;
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('responsive_menus.configuration');

    // Gather enabled styles.
    $styles = $this->pluginManager
      ->getDefinitions();
    $style_options = [];
    foreach ($styles as $style => $values) {
      $style_options[$style] = $values['label'];
    }

    // Get style settings form elements from ajax or the currently enabled
    // style.
    $current_style = $config
      ->get('style');
    if (!empty($form_state
      ->getValue('responsive_menus_style'))) {
      $current_style = $form_state
        ->getValue('responsive_menus_style');
    }

    // Reminders about jQuery requirements if applicable.
    //    $form['responsive_menus_no_jquery_update'] = [
    //      '#type'          => 'checkbox',
    //      '#title'         => t('I will provide my own jQuery library.'),
    //      '#description'   => t("If the style you want requires newer jQuery version and you don't want to use jquery_update module."),
    //      '#default_value' => $config->get('no_jquery_update'),
    //    ];
    // Ignore admin pages option.
    $form['responsive_menus_ignore_admin'] = [
      '#type' => 'checkbox',
      '#title' => 'Ignore admin pages?',
      '#default_value' => $config
        ->get('ignore_admin'),
    ];

    //    $jq_update_ignore = $form['responsive_menus_no_jquery_update']['#default_value'];
    //    $style_info = responsive_menus_style_load($current_style, $jq_update_ignore);
    $style_plugin = $this->pluginManager
      ->createInstance($current_style, [
      'settings' => $config
        ->get('style_settings'),
    ]);
    $form['responsive_menus_style'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Responsive menu style'),
      '#options' => $style_options,
      '#default_value' => $current_style,
      '#ajax' => [
        'callback' => '::ajax',
        'wrapper' => 'rm-style-options',
        'method' => 'replace',
        'effect' => 'fade',
      ],
    ];
    $form['responsive_menus_style_settings'] = [
      '#title' => $this
        ->t('Style settings'),
      '#description' => $this
        ->t('Settings for chosen menu style.'),
      '#prefix' => '<div id="rm-style-options">',
      '#suffix' => '</div>',
      '#type' => 'detail',
      '#tree' => TRUE,
    ];

    // Which selector to use info.
    if (!empty($style_plugin
      ->getSelectorInfo())) {
      $form['responsive_menus_style_settings']['selector_info'] = [
        '#type' => 'item',
        '#title' => $this
          ->t('Selector(s) to use for this style:'),
        '#markup' => '<div class="messages status">' . $style_plugin
          ->getSelectorInfo() . '</div>',
      ];
    }

    // Build additional style settings from style plugins.
    foreach ($style_plugin
      ->settingsForm([], $form_state) as $name => $element) {
      $form['responsive_menus_style_settings'][$name] = $element;
    }
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save configuration'),
    ];

    //    if (!empty($_POST) && form_get_errors()) {
    //      drupal_set_message(t('The settings have not been saved because of the errors.'), 'error');
    //    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * Ajax callback.
   *
   * @param array $form
   *   The settings form array.
   * @param FormStateInterface $form_state
   *   The settings form state object.
   */
  public function ajax(array &$form, FormStateInterface $form_state) {
    return $form['responsive_menus_style_settings'];
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $this
      ->config('responsive_menus.configuration')
      ->set('style', $values['responsive_menus_style'])
      ->set('ignore_admin', $values['responsive_menus_ignore_admin'])
      ->set('style_settings', $values['responsive_menus_style_settings'])
      ->save();

    //  // Clear libraries cache if Sidr style in use to allow theme to be updated.
    //  if ($form_state['values']['responsive_menus_style'] == 'sidr') {
    //    cache_clear_all('*', 'cache_libraries', TRUE);
    //  }
    $this
      ->messenger()
      ->addMessage($this
      ->t('The configuration options have been saved.'));
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
ResponsiveMenusAdminForm::$pluginManager protected property The Responsive Menus plugin manager.
ResponsiveMenusAdminForm::ajax public function Ajax callback.
ResponsiveMenusAdminForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
ResponsiveMenusAdminForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
ResponsiveMenusAdminForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
ResponsiveMenusAdminForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ResponsiveMenusAdminForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
ResponsiveMenusAdminForm::__construct public function Constructs a \Drupal\system\ConfigFormBase object. Overrides ConfigFormBase::__construct
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.