You are here

AmswapConfigForm.php in Admin Menu Swap 7.2

Same filename and directory in other branches
  1. 8 src/Form/AmswapConfigForm.php

Namespace

Drupal\amswap\Form

File

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

namespace Drupal\amswap\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class AmswapConfigForm.
 *
 * @package Drupal\amswap\Form
 */
class AmswapConfigForm extends ConfigFormBase {

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

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

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

    // kint($form, '$form');
    // kint($form_state, '$form_state');
    $config = $this
      ->config('amswap.amswapconfig');

    // @deprecated Old text field
    // $form['role_menu_pairs'] = [
    //   '#type' => 'textarea',
    //   '#title' => $this->t('Role &amp; menu pairs'),
    //   '#description' => $this->t('Roles and their associated menus. Eg &quot;owner:owner-menu; ...&quot;'),
    //   '#default_value' => $config->get('role_menu_pairs'),
    // ];
    $role_menu_pairs = $config
      ->get('role_menu_pairs');

    // kint(['test'], '$role_menu_pairs');
    // var_dump($role_menu_pairs);
    $num_pairs = $form_state
      ->get('num_pairs');
    $num_pairs = $num_pairs ? $num_pairs : 1;

    // var_dump($num_pairs);
    // Use the larger of pairs saved or pairs added using the button
    $num_pairs_in_form = count($role_menu_pairs) > $num_pairs ? count($role_menu_pairs) : $num_pairs;
    $form_state
      ->set('num_pairs', $num_pairs_in_form);
    for ($i = 0; $i < $num_pairs_in_form; $i++) {
      $form['role_menu_pairs'][$i] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Role-Menu Pair ' . ($i + 1)),
      ];
      $role = isset($role_menu_pairs[$i]) ? $role_menu_pairs[$i]['role'] : NULL;
      $form['role_menu_pairs'][$i]['pair-' . $i . '-role'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Role'),
        '#description' => $this
          ->t('The machine name of a role. Eg &quot;owner&quot;'),
        '#default_value' => $role,
      ];
      $menu = isset($role_menu_pairs[$i]) ? $role_menu_pairs[$i]['menu'] : NULL;
      $form['role_menu_pairs'][$i]['pair-' . $i . '-menu'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Menu'),
        '#description' => $this
          ->t('The machine name of menu that should be displayed for that role. Eg &quot;owner&quot;'),
        '#default_value' => $menu,
      ];
      $form['role_menu_pairs'][$i]['pair-' . $i . '-delete'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Remove ' . ($i + 1)),
        '#submit' => [
          '::amswap_delete_pair',
        ],
        '#attributes' => [
          'pair_num' => $i,
        ],
      ];
    }
    $form['add_pair'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Add another role-menu pair'),
      '#submit' => [
        '::amswap_add_pair',
      ],
    ];
    return parent::buildForm($form, $form_state);
  }

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

    // Get the current number of pairs, or 1 if not provided
    $num_pairs = $form_state
      ->get('num_pairs');
    $num_pairs = $num_pairs ? $num_pairs : 1;

    // Add 1 to the number of role-menu pairs that should be displayed
    $form_state
      ->set('num_pairs', $num_pairs + 1);

    // Set the form to be rebuilt.
    $form_state
      ->setRebuild(TRUE);
  }

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

    // kint($form_state, '$form_state');
    $button = $form_state
      ->getTriggeringElement();

    // kint($button, '$button');
    $item = $button['#attributes']['pair_num'];
    $form_state
      ->unsetValue('pair-' . $item . '-role');
    $form_state
      ->unsetValue('pair-' . $item . '-menu');
    $msg = t('Pair ' . ($item + 1) . ' removed. Other pairs saved.');
    drupal_set_message($msg, 'status', FALSE);
    $this
      ->submitForm($form, $form_state);
  }

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

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

    // kint($form, '$form');
    // kint($form_state, '$form_state');
    parent::submitForm($form, $form_state);
    $trigger = $form_state
      ->getTriggeringElement();

    // kint($trigger, '$trigger');
    $pairs = [];
    $pair_index = 0;
    $num_pairs = $form_state
      ->get('num_pairs');
    $num_pairs = $num_pairs ? $num_pairs : 1;

    // kint($num_pairs, '$num_pairs');
    for ($i = 0; $i < $num_pairs; $i++) {

      //   $pairs[$] = $i;
      $role = $form_state
        ->getValue('pair-' . $i . '-role');
      $menu = $form_state
        ->getValue('pair-' . $i . '-menu');
      if ($role && $menu) {
        $pairs[$pair_index]['role'] = $role;
        $pairs[$pair_index]['menu'] = $menu;
        $pair_index++;
      }
      elseif (strpos($trigger['#id'], 'delete') === FALSE) {
        $msg = t('Pair ' . ($i + 1) . ' was missing either a role or a menu value, so was not saved.');
        drupal_set_message($msg, 'warning', FALSE);
      }
    }

    // kint($pairs, '$pairs');
    $this
      ->config('amswap.amswapconfig')
      ->set('role_menu_pairs', $pairs)
      ->save();
    $url = \Drupal\Core\Url::fromRoute('system.performance_settings');

    // kint($url, '$url');
    $link = \Drupal\Core\Link::fromTextAndUrl('Clear caches', $url);

    // kint($link, '$link');
    $msg = $link
      ->toString() . ' to see the changes.';
    $rendered_msg = \Drupal\Core\Render\Markup::create($msg);
    drupal_set_message($rendered_msg, 'status', FALSE);

    // drupal_flush_all_caches();
  }

}

Classes

Namesort descending Description
AmswapConfigForm Class AmswapConfigForm.