View source
<?php
namespace Drupal\amswap\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
class AmswapConfigForm extends ConfigFormBase {
protected function getEditableConfigNames() {
return [
'amswap.amswapconfig',
];
}
public function getFormId() {
return 'amswap_config_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('amswap.amswapconfig');
$role_menu_pairs = $config
->get('role_menu_pairs');
$num_pairs = $form_state
->get('num_pairs');
$num_pairs = $num_pairs ? $num_pairs : 1;
$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 "owner"'),
'#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 "owner"'),
'#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);
}
public function amswap_add_pair(array $form, FormStateInterface &$form_state) {
$num_pairs = $form_state
->get('num_pairs');
$num_pairs = $num_pairs ? $num_pairs : 1;
$form_state
->set('num_pairs', $num_pairs + 1);
$form_state
->setRebuild(TRUE);
}
public function amswap_delete_pair(array $form, FormStateInterface &$form_state) {
$button = $form_state
->getTriggeringElement();
$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);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$trigger = $form_state
->getTriggeringElement();
$pairs = [];
$pair_index = 0;
$num_pairs = $form_state
->get('num_pairs');
$num_pairs = $num_pairs ? $num_pairs : 1;
for ($i = 0; $i < $num_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);
}
}
$this
->config('amswap.amswapconfig')
->set('role_menu_pairs', $pairs)
->save();
$url = \Drupal\Core\Url::fromRoute('system.performance_settings');
$link = \Drupal\Core\Link::fromTextAndUrl('Clear caches', $url);
$msg = $link
->toString() . ' to see the changes.';
$rendered_msg = \Drupal\Core\Render\Markup::create($msg);
drupal_set_message($rendered_msg, 'status', FALSE);
}
}