View source
<?php
namespace Drupal\drop_down_login\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class DropDownLoginAdminForm extends ConfigFormBase {
public function getFormId() {
return 'o365_admin_settings';
}
protected function getEditableConfigNames() {
return [
'drop_down_login.admin.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('drop_down_login.admin.settings');
$form['#tree'] = TRUE;
$form['#attached']['library'][] = 'drop_down_login/drop_down_login_settings';
$drop_down_login_want_myaccount = $config
->get('drop_down_login_want_myaccount');
$form['drop_down_login_setting']['drop_down_login_want_myaccount'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable My Account drop-down after login.'),
'#default_value' => isset($drop_down_login_want_myaccount['drop_down_login_want_myaccount']) ? $drop_down_login_want_myaccount['drop_down_login_want_myaccount'] : NULL,
'#description' => $this
->t('If you want a drop-down menu to appear with "View Profile"
and "Logout" (as well as optional links you specify below between these two
options) instead of just the logout button, check this box.'),
];
$form['drop_down_login_myaccount_links'] = [
'#type' => 'details',
'#weight' => 80,
'#tree' => TRUE,
'#title' => $this
->t('Additional Links'),
'#open' => TRUE,
'#prefix' => '<div id="js-ajax-elements-wrapper">',
'#suffix' => '</div>',
'#description' => $this
->t('If you chose to enable the "My Account" drop-down after login,
you can include additional links by completing the fields below, one set
for each link.'),
];
$num_rows_field = $form_state
->get('num_rows');
$mapping_rows = empty($form_state
->get('empty_rows')) ? $config
->get('num_rows') : $form_state
->get('empty_rows');
if (!empty($num_rows_field)) {
$num_rows = $form_state
->get('num_rows');
}
elseif (!empty($mapping_rows)) {
$form_state
->set('num_rows', $mapping_rows);
$num_rows = $form_state
->get('num_rows');
}
elseif (empty($num_rows_field)) {
$form_state
->set('num_rows', []);
$num_rows = $form_state
->get('num_rows');
}
$form['drop_down_login_myaccount_links']['table'] = [
'#type' => 'table',
'#header' => [
$this
->t('Url Name'),
$this
->t('Url Path'),
[
'data' => $this
->t('Operations'),
'colspan' => 3,
],
],
'#empty' => $this
->t('There are no items yet. Add an item.', [
'@add-url' => Url::fromRoute('drop_down_login.drop_down_login_admin_settings'),
]),
'#tableselect' => FALSE,
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'drop-down-login-item-weight',
],
],
];
$drop_down_login_myaccount_links = $config
->get('drop_down_login_myaccount_links');
foreach ($num_rows as $delta) {
$form['drop_down_login_myaccount_links']['table'][$delta]['#attributes']['class'][] = 'draggable';
$form['drop_down_login_myaccount_links']['table'][$delta]['menu_name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Menu Name'),
'#size' => 60,
'#required' => TRUE,
'#default_value' => !empty($drop_down_login_myaccount_links['table'][$delta]) ? $drop_down_login_myaccount_links['table'][$delta]['menu_name'] : NULL,
'#description' => $this
->t('The text to be used for this link in the My Account drop down.'),
];
$form['drop_down_login_myaccount_links']['table'][$delta]['menu_url'] = [
'#type' => 'url',
'#title' => $this
->t('Menu URL'),
'#size' => 60,
'#required' => TRUE,
'#max_length' => 512,
'#default_value' => !empty($drop_down_login_myaccount_links['table'][$delta]) ? $drop_down_login_myaccount_links['table'][$delta]['menu_url'] : NULL,
'#description' => $this
->t('The path for this menu link. This can be an internal Drupal path such as node/add or an external URL such as http://drupal.org. Enter :front to link to the front page.', [
':front' => "<front>",
]),
];
$form['drop_down_login_myaccount_links']['table'][$delta]['remove_name'] = [
'#type' => 'submit',
'#value' => $this
->t('Remove'),
'#limit_validation_errors' => [],
'#submit' => [
'::removeCallback',
],
'#ajax' => [
'callback' => '::addmoreCallback',
'wrapper' => 'js-ajax-elements-wrapper',
],
'#attributes' => [
'class' => [
'button-small',
],
],
'#name' => 'remove_name_' . $delta,
];
$form['drop_down_login_myaccount_links']['table'][$delta]['menu']['#weight'] = $delta;
$form['drop_down_login_myaccount_links']['table'][$delta]['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight'),
'#default_value' => $delta,
'#title-display' => 'invisible',
'#attributes' => [
'class' => [
'drop-down-login-item-weight',
],
],
];
}
$form['drop_down_login_myaccount_links']['add_name'] = [
'#type' => 'submit',
'#value' => $this
->t('Add a link'),
'#submit' => [
'::addOne',
],
'#limit_validation_errors' => [],
'#ajax' => [
'callback' => '::addmoreCallback',
'wrapper' => 'js-ajax-elements-wrapper',
],
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$config = $this
->config('drop_down_login.admin.settings');
$config
->set('drop_down_login_want_myaccount', $form_state
->getValue('drop_down_login_setting'));
$config
->set('drop_down_login_myaccount_links', $form_state
->getValue('drop_down_login_myaccount_links'));
$drop_down_login_myaccount_links = $form_state
->getValue('drop_down_login_myaccount_links');
$weight = [];
foreach ($drop_down_login_myaccount_links['table'] as $key => $value) {
$weight[] = $key;
}
$config
->set('num_rows', $weight);
$config
->save();
}
public function addmoreCallback(array &$form, FormStateInterface $form_state) {
return $form['drop_down_login_myaccount_links'];
}
public function addOne(array &$form, FormStateInterface $form_state) {
$num_rows = $form_state
->get('num_rows');
$num_rows[] = count($num_rows) > 0 ? max($num_rows) + 1 : 0;
$form_state
->set('num_rows', $num_rows);
$form_state
->setRebuild();
}
public function removeCallback(array &$form, FormStateInterface $form_state) {
$triggerdElement = $form_state
->getTriggeringElement();
if (!empty($triggerdElement['#name'])) {
$name = explode('_', $triggerdElement['#name']);
$num_rows = $form_state
->get('num_rows');
$pointer = array_search($name['2'], $num_rows);
unset($num_rows[$pointer]);
$form_state
->set('num_rows', $num_rows);
if (empty($num_rows)) {
$form_state
->set('empty_rows', 1);
}
}
$form_state
->setRebuild();
}
}