You are here

class SettingsForm in Responsive and off-canvas menu 8.3

Same name and namespace in other branches
  1. 8.2 src/Form/SettingsForm.php \Drupal\responsive_menu\Form\SettingsForm
  2. 4.4.x src/Form/SettingsForm.php \Drupal\responsive_menu\Form\SettingsForm
  3. 4.0.x src/Form/SettingsForm.php \Drupal\responsive_menu\Form\SettingsForm
  4. 4.1.x src/Form/SettingsForm.php \Drupal\responsive_menu\Form\SettingsForm
  5. 4.3.x src/Form/SettingsForm.php \Drupal\responsive_menu\Form\SettingsForm

Form builder for the responsive_menu admin settings page.

Hierarchy

Expanded class hierarchy of SettingsForm

1 string reference to 'SettingsForm'
responsive_menu.routing.yml in ./responsive_menu.routing.yml
responsive_menu.routing.yml

File

src/Form/SettingsForm.php, line 17

Namespace

Drupal\responsive_menu\Form
View source
class SettingsForm extends ConfigFormBase {

  /**
   * Configuration Factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

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

  /**
   * Stored configuration for the module.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

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

  /**
   * Constructs a SettingsForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($config_factory);
    $this->configFactory = $config_factory;
    $this->entityTypeManager = $entity_type_manager;
    $this->config = $config_factory
      ->get('responsive_menu.settings');
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['responsive_menu'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Responsive menu'),
    ];
    $form['responsive_menu']['horizontal_menu'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Choose which Drupal menu will be rendered as a horizontal menu at the breakpoint width'),
      '#default_value' => $this->config
        ->get('horizontal_menu'),
      '#options' => $this
        ->getMenuOptions(),
      '#description' => $this
        ->t("This menu will only be rendered if you use the included 'Horizontal menu' block. You can safely ignore this setting if you want to use another menu block instead."),
    ];
    $form['responsive_menu']['horizontal_depth'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('A maximum menu depth that the horizontal menu should display'),
      '#default_value' => $this->config
        ->get('horizontal_depth'),
      '#options' => array_combine([
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
      ], [
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
      ]),
      '#description' => $this
        ->t('The mobile menu will always allow all depths to be navigated to. This only controls what menu depth you want to display on the horizontal menu. It can be useful if you want a single row of items because you are handling secondary level and lower in a separate block.'),
    ];
    $form['responsive_menu']['off_canvas'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Off canvas'),
    ];
    $form['responsive_menu']['off_canvas']['off_canvas_menus'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Enter the name(s) of Drupal menus to be rendered in an off-canvas menu'),
      '#description' => $this
        ->t('Enter the names of menus in a comma delimited format. If more than one menu is entered the menu items will be merged together. This is useful if you have a main menu and a utility menu that display separately at wider screen sizes but should be merged into a single menu at smaller screen sizes. Note that the menus will be merged in the entered order.'),
      '#default_value' => $this->config
        ->get('off_canvas_menus'),
    ];
    $form['responsive_menu']['horizontal_wrapping_element'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Choose the HTML element to wrap the menu block in'),
      '#default_value' => $this->config
        ->get('horizontal_wrapping_element'),
      '#options' => [
        'nav' => 'nav',
        'div' => 'div',
      ],
    ];
    $form['responsive_menu']['use_breakpoint'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use a breakpoint'),
      '#description' => $this
        ->t("Unchecking this will disable the breakpoint and your mobile menu icon block will always display (assuming you have placed it on the page). This can be useful if you always want to display the mobile menu icon and don't want a horizontal menu at all, or if you want to control the visibility and breakpoints in your theme's css. Note that the horizontal menu block, if placed, will only be visible if this is checked."),
      '#default_value' => $this->config
        ->get('use_breakpoint'),
    ];

    // Breakpoints.
    $queries = responsive_menu_get_breakpoints();
    $form['responsive_menu']['horizontal_breakpoint'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Choose a breakpoint to trigger the desktop format menu at'),
      '#default_value' => $this->config
        ->get('horizontal_breakpoint'),
      '#options' => $queries,
      '#states' => [
        'visible' => [
          ':input[name="use_breakpoint"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
      '#description' => $this
        ->t('The mobile icon will be hidden at this breakpoint and the horizontal menu will show (if the block has been placed).'),
    ];
    if (empty($queries)) {
      $form['responsive_menu']['horizontal_breakpoint']['#disabled'] = TRUE;
      $form['responsive_menu']['horizontal_breakpoint']['#description'] = '<div class="description">' . $this
        ->t('You must configure at least one @breakpoint to see any options. Until then the select widget above is disabled.', [
        '@breakpoint' => Link::fromTextAndUrl('breakpoint', Url::fromUri('https://www.drupal.org/documentation/modules/breakpoint')),
      ]) . '</div>';
    }

    // Whether to load the base css.
    $form['responsive_menu']['css'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t("Load the responsive_menu module's css"),
      '#description' => $this
        ->t('It might be that you want to override all of the css that comes with the responsive_menu module in which case you can disable the loading of the css here and include it instead in your theme.'),
      '#default_value' => $this->config
        ->get('include_css'),
    ];

    // Whether to allow on admin pages.
    $form['responsive_menu']['allow_admin'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Allow on the admin theme'),
      '#description' => $this
        ->t('By default the mmenu library is not added to admin pages using the admin theme (if different). By checking this option the code which adds the javascript and the wrapping elements to the page will be added to every page including backend admin pages using the admin theme.'),
      '#default_value' => $this->config
        ->get('allow_admin'),
    ];

    // Whether to add a theme wrapper for the admin theme.
    $form['responsive_menu']['wrapper_admin'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Add a page wrapper div to the admin theme'),
      '#description' => $this
        ->t('Many admin themes do not have a wrapping div around all their regions (Seven theme for example) and mmenu requires this div to render properly. Checking this option will add the wrapping div using a preprocess hook.'),
      '#default_value' => $this->config
        ->get('wrapper_admin'),
      '#states' => [
        // Only show this field when the 'allow_admin' checkbox is enabled.
        'visible' => [
          ':input[name="allow_admin"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['responsive_menu']['theme_compatibility'] = [
      '#type' => 'fieldset',
      '#title' => 'Theme compatiblity',
    ];

    // Whether to add a theme wrapper for the front end theme.
    $form['responsive_menu']['theme_compatibility']['wrapper_theme'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Add a page wrapper div to the front end theme'),
      '#description' => $this
        ->t("Some themes don't have a wrapping div around all their regions (Bootstrap theme for example) and mmenu requires this div to render properly. Checking this option will add the wrapping div using a preprocess hook. Alternatively you can do this manually in your theme."),
      '#default_value' => $this->config
        ->get('wrapper_theme'),
    ];

    // Left or right positioned panel.
    $form['responsive_menu']['position'] = [
      '#type' => 'select',
      '#options' => [
        'left' => $this
          ->t('Left'),
        'right' => $this
          ->t('Right'),
      ],
      '#title' => $this
        ->t('Which side the mobile menu panel should slide out from'),
      '#default_value' => $this->config
        ->get('off_canvas_position'),
    ];

    // The theme of the slideout panel.
    $form['responsive_menu']['theme'] = [
      '#type' => 'select',
      '#options' => [
        'theme-light' => $this
          ->t('Light'),
        'theme-dark' => $this
          ->t('Dark'),
        'theme-black' => $this
          ->t('Black'),
        'theme-white' => $this
          ->t('White'),
      ],
      '#title' => $this
        ->t('Which mmenu theme to use'),
      '#default_value' => $this->config
        ->get('off_canvas_theme'),
    ];

    // Whether to dim to the page when the menu slides out.
    $form['responsive_menu']['pagedim'] = [
      '#type' => 'select',
      '#options' => [
        'none' => $this
          ->t('No page dim'),
        'pagedim' => $this
          ->t('Dim to default page colour'),
        'pagedim-white' => $this
          ->t('Dim to white'),
        'pagedim-black' => $this
          ->t('Dim to black'),
      ],
      '#title' => $this
        ->t('The colour to dim the page to when the menu slides out'),
      '#default_value' => $this->config
        ->get('pagedim'),
    ];

    // A javascript enhancements fieldset.
    $form['responsive_menu']['js'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Javascript enhancements'),
    ];
    $form['responsive_menu']['js']['superfish'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Apply Superfish to the horizontal menu'),
      '#description' => $this
        ->t('Adds the <a href="@superfish">Superfish</a> library functionality to the horizontal menu. This enhances the menu with better support for hovering and support for mobiles.', [
        '@superfish' => 'https://github.com/joeldbirch/superfish',
      ]),
      '#default_value' => $this->config
        ->get('horizontal_superfish'),
    ];
    $form['responsive_menu']['js']['superfish_options'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Superfish options'),
      '#states' => [
        'visible' => [
          ':input[name="superfish"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['responsive_menu']['js']['superfish_options']['superfish_delay'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Delay'),
      '#description' => $this
        ->t('The amount of time in milliseconds a menu will remain after the mouse leaves it.'),
      '#default_value' => $this->config
        ->get('horizontal_superfish_delay'),
    ];
    $form['responsive_menu']['js']['superfish_options']['superfish_speed'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Speed'),
      '#description' => $this
        ->t('The amount of time in milliseconds it takes for a menu to reach 100% opacity when it opens.'),
      '#default_value' => $this->config
        ->get('horizontal_superfish_speed'),
    ];
    $form['responsive_menu']['js']['superfish_options']['superfish_speed_out'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Speed out'),
      '#description' => $this
        ->t('The amount of time in milliseconds it takes for a menu to reach 0% opacity when it closes.'),
      '#default_value' => $this->config
        ->get('horizontal_superfish_speed_out'),
    ];
    $form['responsive_menu']['js']['superfish_options']['superfish_hoverintent'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use hoverintent'),
      '#description' => $this
        ->t('Whether to use the <a href="@hoverintent">HoverIntent</a> plugin with superfish. This library is included in the superfish download and doesn\'t require separate installation.', [
        '@hoverintent' => 'http://cherne.net/brian/resources/jquery.hoverIntent.html',
      ]),
      '#default_value' => $this->config
        ->get('horizontal_superfish_hoverintent'),
    ];

    // Whether the optional superfish library is to be used.
    if (!file_exists(DRUPAL_ROOT . '/libraries/superfish/dist/js/superfish.min.js')) {
      $form['responsive_menu']['js']['superfish']['#disabled'] = TRUE;
      $form['responsive_menu']['js']['superfish']['#description'] .= '<br/><span class="warning">' . $this
        ->t('You need to download the <a href="@superfish">Superfish</a> library and place it in a /libraries directory. Until then the superfish option is disabled.', [
        '@superfish' => 'https://github.com/joeldbirch/superfish/archive/master.zip',
      ]) . '</span>';
    }

    // The hammer js library is optional.
    $form['responsive_menu']['js']['hammerjs'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Add swipe gestures'),
      '#description' => $this
        ->t('Adds the hammer.js library to enhance the mobile experience with swipe gestures to open or close the menu.'),
      '#default_value' => $this->config
        ->get('hammerjs'),
    ];

    // If the libraries module isn't installed or if the hammer.min.js
    // file isn't in the correct location then disable the hammer option
    // and display an appropriate message.
    if (!file_exists(DRUPAL_ROOT . '/libraries/hammerjs/hammer.min.js')) {
      $form['responsive_menu']['js']['hammerjs']['#disabled'] = TRUE;
      $form['responsive_menu']['js']['hammerjs']['#description'] .= '<br/><span class="warning">' . $this
        ->t('You must download the <a href="@hammer">HammerJS</a> file and place it in a hammerjs directory in /libraries. Until then the hammerjs option is disabled.', [
        '@hammer' => 'http://hammerjs.github.io/dist/hammer.min.js',
      ]) . '</span>';
    }
    return parent::buildForm($form, $form_state);
  }

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

    // Ensure there are breakpoints configured.
    $values = $form_state
      ->getValues();
    if ($values['use_breakpoint'] && empty($values['horizontal_breakpoint'])) {
      $breakpoint_message = Link::fromTextAndUrl('breakpoint file', Url::fromUri('https://www.drupal.org/node/1803874'))
        ->toRenderable();
      $form_state
        ->setErrorByName('horizontal_breakpoint', $this
        ->t("You have chosen to use a breakpoint but you have not selected one. This may happen if your @breakpoint is not properly set up.", [
        '@breakpoint' => render($breakpoint_message),
      ]));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();

    // Save all the submitted form values into config.
    $this->configFactory
      ->getEditable('responsive_menu.settings')
      ->set('horizontal_menu', $values['horizontal_menu'])
      ->set('horizontal_depth', $values['horizontal_depth'])
      ->set('horizontal_wrapping_element', $values['horizontal_wrapping_element'])
      ->set('use_breakpoint', $values['use_breakpoint'])
      ->set('include_css', $values['css'])
      ->set('allow_admin', $values['allow_admin'])
      ->set('wrapper_admin', $values['wrapper_admin'])
      ->set('wrapper_theme', $values['wrapper_theme'])
      ->set('pagedim', $values['pagedim'])
      ->set('off_canvas_menus', $values['off_canvas_menus'])
      ->set('off_canvas_position', $values['position'])
      ->set('off_canvas_theme', $values['theme'])
      ->set('horizontal_superfish', $values['superfish'])
      ->set('horizontal_superfish_delay', $values['superfish_delay'])
      ->set('horizontal_superfish_speed', $values['superfish_speed'])
      ->set('horizontal_superfish_speed_out', $values['superfish_speed_out'])
      ->set('horizontal_superfish_hoverintent', $values['superfish_hoverintent'])
      ->set('hammerjs', $values['hammerjs'])
      ->save();

    // Handle the breakpoint.
    $queries = responsive_menu_get_breakpoints();

    // Check if the breakpoint exists and the user has chosen
    // to use a breakpoint.
    if ($values['use_breakpoint'] && isset($queries[$values['horizontal_breakpoint']])) {

      // Store the breakpoint for using again in the form.
      $this->configFactory
        ->getEditable('responsive_menu.settings')
        ->set('horizontal_breakpoint', $values['horizontal_breakpoint'])
        ->set('horizontal_media_query', $queries[$values['horizontal_breakpoint']])
        ->save();

      // Generate the breakpoint css file and remove existing one.
      $path = _get_breakpoint_css_filepath();

      // Ensure the directory exists, if not create it.
      if (file_exists($path . RESPONSIVE_MENU_BREAKPOINT_FILENAME)) {
        unlink($path . RESPONSIVE_MENU_BREAKPOINT_FILENAME);
      }
      $breakpoint = $this->config
        ->get('horizontal_media_query');
      responsive_menu_generate_breakpoint_css($breakpoint);
    }

    // Invalidate the block cache for the horizontal menu so these settings get
    // applied when rebuilding the block on view.
    Cache::invalidateTags([
      'config:block.block.horizontalmenu',
    ]);
    parent::submitForm($form, $form_state);
  }

  /**
   * Gets a list of menu names for use as options.
   *
   * @param array $menu_names
   *   (optional) Array of menu names to limit the options, or NULL to load all.
   *
   * @return array
   *   Keys are menu names (ids) values are the menu labels.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  protected function getMenuOptions(array $menu_names = NULL) {
    $menus = $this->entityTypeManager
      ->getStorage('menu')
      ->loadMultiple($menu_names);
    $options = [];

    /** @var \Drupal\system\MenuInterface[] $menus */
    foreach ($menus as $menu) {
      $options[$menu
        ->id()] = $menu
        ->label();
    }
    return $options;
  }

}

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::$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.
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.
SettingsForm::$config protected property Stored configuration for the module.
SettingsForm::$configFactory protected property Configuration Factory. Overrides FormBase::$configFactory
SettingsForm::$entityTypeManager protected property The entity type manager.
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SettingsForm::getMenuOptions protected function Gets a list of menu names for use as options.
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
SettingsForm::__construct public function Constructs a SettingsForm 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.