Skip to content

Instantly share code, notes, and snippets.

@swappyp20
Forked from patilvishalvs/AddMoreRemoveForm.php
Created January 9, 2018 07:02
Show Gist options
  • Save swappyp20/43f02bd66f8f117d613e59be2b368fd6 to your computer and use it in GitHub Desktop.
Save swappyp20/43f02bd66f8f117d613e59be2b368fd6 to your computer and use it in GitHub Desktop.
Add remove form elements with tabledrag and ajax drupal 8.x
<?php
namespace Drupal\experiments\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\SortArray;
/**
* Class AddMoreForm.
*
* @package Drupal\experiments\Form
*/
class AddMoreForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'add_more_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['accordions'] = [
'#type' => 'table',
'#tree' => true,
'#prefix' => '<div id="accordions-wrapper">',
'#suffix' => '</div>',
'#header' => array(t('Item'), t('Weight'), t('Accordion content'), t('Actions')),
'#tabledrag' => array(
array(
'action' => 'order',
'relationship' => 'sibling',
'group' => 'mytable-order-weight',
),
),
];
$weights = $form_state->getValue('accordions');
if (empty($weights)) {
$weights[] = [
'weight' => 0,
'accordion' => [],
];
}
uasort($weights, 'Drupal\Component\Utility\SortArray::sortByWeightElement');
foreach ($weights as $delta => $weight) {
// TableDrag: Weight column element.
$form['accordions'][$delta]['#attributes']['class'][] = 'draggable';
$form['accordions'][$delta]['#weight'] = isset($weight['weight'])?$weight['weight']:0;
$form['accordions'][$delta]['item'] = array(
'#plain_text' => isset($weight['weight'])?$weight['weight']:0
);
$form['accordions'][$delta]['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight for @title', array('@title' => 'accordion')),
'#title_display' => 'invisible',
'#attributes' => array('class' => array('mytable-order-weight')),
'#default_value' => isset($weight['weight'])?$weight['weight']:0
);
$form['accordions'][$delta]['accordion'] = [
'#type' => 'details',
'#tree' => true,
'#title' => t('Accordion @counter', ['@counter' => ($delta+1)]),
'#collapsible' => true,
];
$form['accordions'][$delta]['accordion']['title'] = [
'#type' => 'textfield',
'#title' => t('Title'),
];
$form['accordions'][$delta]['accordion']['text'] = [
'#type' => 'text_format',
'#title' => t('Name'),
];
if (count($weights) > 1) {
$form['accordions'][$delta]['delete'] = [
'#type' => 'submit',
'#title' => t('Remove'),
'#name' => 'delete_' . $delta,
'#value' => 'Remove',
'#submit' => ['::ajaxSubmit'],
'#ajax' => [
'callback' => '::addMoreSet',
'wrapper' => 'accordions-wrapper',
]
];
}else{
$form['accordions'][$delta]['delete'] = [];
}
}
$form['add'] = [
'#type' => 'submit',
'#title' => t('Add accordion'),
'#value' => t('Add more'),
'#submit' => ['::ajaxSubmit'],
'#ajax' => [
'callback' => '::addMoreSet',
'wrapper' => 'accordions-wrapper',
]
];
return $form;
}
public function addMoreSet(array &$form, FormStateInterface $form_state) {
return $form['accordions'];
}
/**
* {@inheritdoc}
*/
public function ajaxSubmit(array &$form, FormStateInterface $form_state) {
$weights = $form_state->getValue('accordions');
$parents = $form_state->getTriggeringElement()['#parents'];
if (isset($parents[0]) && $parents[0] == 'add') {
$weights[] = [
'weight' => 0,
'accordion' => [],
];
}
if (isset($parents[2]) && $parents[2] == 'delete') {
unset($weights[$parents[1]]);
}
$form_state->setValue('accordions', $weights);
$form_state->setRebuild(TRUE);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment