You are here

class MmenuSettingsForm in Mobile sliding menu 8

Class MmenuSettingsForm.

@package Drupal\mmenu\Form

Hierarchy

Expanded class hierarchy of MmenuSettingsForm

1 string reference to 'MmenuSettingsForm'
mmenu.routing.yml in ./mmenu.routing.yml
mmenu.routing.yml

File

src/Form/MmenuSettingsForm.php, line 18

Namespace

Drupal\mmenu\Form
View source
class MmenuSettingsForm extends FormBase {

  /**
   * Returns a unique string identifying the form.
   *
   * @return string
   *   The unique string identifying the form.
   */
  public function getFormId() {
    return 'mmenu_settings';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $mmenu_name = '') {
    $mmenu = mmenu_list($mmenu_name);
    $form['general'] = array(
      '#tree' => TRUE,
      '#type' => 'details',
      '#title' => t('General'),
      '#weight' => -5,
      '#open' => TRUE,
    );
    $form['general']['enabled'] = array(
      '#title' => t('Enabled?'),
      '#type' => 'select',
      '#options' => array(
        1 => t('Yes'),
        0 => t('No'),
      ),
      '#default_value' => $mmenu['enabled'] ? 1 : 0,
      '#required' => TRUE,
      '#weight' => -3,
      '#description' => t('Enable or disable the mmenu.'),
    );
    $form['general']['name'] = array(
      '#type' => 'hidden',
      '#value' => $mmenu_name,
    );
    $block_options = mmenu_get_blocks();

    //    dpm($block_options);
    //    $block_options = array();
    //    $block_options[] = t('--- Please select a block ---');
    //    foreach ($drupal_blocks as $module => $drupal_block) {
    //      foreach ($drupal_block as $id => $block) {
    //        $block_options[$module . '|' . $block->getPluginId() . '|' . $id] = Unicode::ucfirst($module) . ' - ' . $block->label();
    //      }
    //    }
    $form['blocks'] = array(
      '#tree' => TRUE,
      '#type' => 'details',
      '#title' => t('Blocks'),
      '#weight' => 0,
      '#open' => TRUE,
    );
    $blocks = array();
    foreach ($mmenu['blocks'] as $k => $block) {
      $blocks[] = $block;
    }
    $allowed_blocks_nums = \Drupal::config('mmenu.settings')
      ->get('allowed_blocks_nums');
    dpm($allowed_blocks_nums, '$allowed_blocks_nums');
    for ($i = count($blocks); $i < $allowed_blocks_nums; $i++) {
      $blocks[$i]['title'] = '';
      $blocks[$i]['plugin_id'] = '';
      $blocks[$i]['collapsed'] = TRUE;
      $blocks[$i]['wrap'] = FALSE;
    }
    foreach ($blocks as $k => $block) {
      $form['blocks'][$k] = array(
        '#tree' => TRUE,
        '#type' => 'details',
        '#title' => t('Block'),
        '#open' => !empty($block['plugin_id']),
      );
      $form['blocks'][$k]['plugin_id'] = array(
        '#title' => t('Select a block'),
        '#type' => 'select',
        '#options' => $block_options,
        '#default_value' => !empty($block['plugin_id']) ? $block['plugin_id'] : '',
        '#description' => t('Select a block to display on the mmenu.'),
      );
      $form['blocks'][$k]['menu_parameters'] = array(
        '#tree' => TRUE,
        '#type' => 'details',
        '#title' => t('Menu parameters'),
        '#open' => FALSE,
      );
      $options = array(
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
      );
      $options = array_combine($options, $options);
      $form['blocks'][$k]['menu_parameters']['min_depth'] = array(
        '#title' => t('Min depth'),
        '#type' => 'select',
        '#options' => $options,
        '#default_value' => isset($block['menu_parameters']) && isset($block['menu_parameters']['min_depth']) ? $block['menu_parameters']['min_depth'] : 1,
        '#description' => t('The minimum depth of menu links in the resulting tree. Defaults to 1, which is the default to build a whole tree for a menu (excluding menu container itself).'),
      );
      $form['blocks'][$k]['title'] = array(
        '#title' => t('Title'),
        '#type' => 'textfield',
        '#default_value' => $block['title'],
        '#description' => t('Override the default title for the block. Use <em>:placeholder</em> to display no title, or leave blank to use the default block title.', array(
          ':placeholder' => '<none>',
        )),
      );
      $form['blocks'][$k]['collapsed'] = array(
        '#title' => t('Collapsed'),
        '#type' => 'select',
        '#options' => array(
          1 => t('Yes'),
          0 => t('No'),
        ),
        '#default_value' => $block['collapsed'] ? 1 : 0,
        '#description' => t('Collapse or expand the block content by default.'),
      );
      $form['blocks'][$k]['wrap'] = array(
        '#title' => t('Wrap'),
        '#type' => 'select',
        '#options' => array(
          1 => t('Yes'),
          0 => t('No'),
        ),
        '#default_value' => $block['wrap'] ? 1 : 0,
        '#description' => t('Determine if needs to wrap the block content. Usually to set it to true if the block is not a system menu.'),
      );
    }
    $form['mmenu_options'] = array(
      '#tree' => TRUE,
      '#type' => 'details',
      '#title' => t('Mmenu options'),
      '#weight' => 1,
      '#open' => FALSE,
    );
    $form['mmenu_options']['yaml'] = array(
      '#title' => t('Enter YAML format settings'),
      '#type' => 'textarea',
      '#rows' => 20,
      '#required' => FALSE,
      '#default_value' => isset($mmenu['options']) ? Yaml::encode($mmenu['options']) : Yaml::encode(Json::encode(mmenu_get_default_options())),
      '#weight' => 0,
      '#description' => t('For more information about the options, please visit the page <a href=":link" target="_blank">:link</a>.', array(
        ':link' => 'https://mmenu.frebsite.nl/documentation/core/options.html',
      )),
    );
    $form['mmenu_configurations'] = array(
      '#tree' => TRUE,
      '#type' => 'details',
      '#title' => t('Mmenu configurations'),
      '#weight' => 2,
      '#open' => FALSE,
    );
    $form['mmenu_configurations']['yaml'] = array(
      '#title' => t('Enter YAML format settings'),
      '#type' => 'textarea',
      '#rows' => 20,
      '#required' => FALSE,
      '#default_value' => isset($mmenu['configurations']) ? Yaml::encode($mmenu['configurations']) : Yaml::encode(Json::encode(mmenu_get_default_configurations())),
      '#weight' => 0,
      '#description' => t('For more information about the configurations, please visit the page <a href=":link" target="_blank">:link</a>.', array(
        ':link' => 'https://mmenu.frebsite.nl/documentation/core/configuration.html',
      )),
    );
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
      '#weight' => 0,
    );
    $form['actions']['reset'] = array(
      '#type' => 'submit',
      '#value' => t('Reset'),
      '#weight' => 1,
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    switch ($values['op']
      ->__toString()) {
      case t('Save'):

        //        $blocks = array();
        //
        //        // Updates the blocks.
        //        foreach ($values['blocks'] as $k => $block) {
        //          if (!empty($block['module_delta'])) {
        //            list($module, , $id) = explode('|', $block['module_delta']);
        //            $blocks[$k] = $block;
        //            $blocks[$k] += array(
        //              'module' => $module,
        //              'delta' => $id,
        //            );
        //          }
        //        }
        $mmenu = array(
          'enabled' => $values['general']['enabled'],
          'name' => $values['general']['name'],
          'blocks' => $values['blocks'],
          'options' => Yaml::decode($values['mmenu_options']['yaml']),
          'configurations' => Yaml::decode($values['mmenu_configurations']['yaml']),
        );
        $config = \Drupal::configFactory()
          ->getEditable('mmenu.settings');
        $config
          ->set('mmenu_item_' . $values['general']['name'], $mmenu);
        $config
          ->save();

        // Clears mmenus cache.
        \Drupal::cache()
          ->delete('mmenus:cache');
        drupal_set_message(t('The settings have been saved.'));
        break;
      case t('Reset'):

        // Deletes the mmenu settings from database.
        $config = \Drupal::configFactory()
          ->getEditable('mmenu.settings');
        $config
          ->delete('mmenu_item_' . $values['general']['name']);

        // Clears mmenus cache.
        \Drupal::cache()
          ->delete('mmenus:cache');
        drupal_set_message(t('The settings have been reset.'));
        break;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
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.
MmenuSettingsForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
MmenuSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
MmenuSettingsForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
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.