Skip to content
Snippets Groups Projects

ISTWCMS-4866: moving the heading selector for ec outside the individual ec...

1 file
+ 65
5
Compare changes
  • Side-by-side
  • Inline
@@ -177,6 +177,7 @@ class UwCblExpandCollapse extends BlockBase implements ContainerFactoryPluginInt
$this->t('Section'),
$this->t('Heading selector'),
$this->t('Heading text'),
'',
$this->t('Weight'),
],
'#empty' => $this->t('No items.'),
@@ -215,9 +216,10 @@ class UwCblExpandCollapse extends BlockBase implements ContainerFactoryPluginInt
// The first column of the table, that will house the arrows
// for rearranging e/c sections.
$settings[''] = [
$settings['arrow'] = [
'#type' => 'markup',
'#markup' => '',
'#title_display' => 'invisible',
];
// The section element, which is an autocomplete using the nid
@@ -251,6 +253,21 @@ class UwCblExpandCollapse extends BlockBase implements ContainerFactoryPluginInt
'#default_value' => isset($ecs[$i]['heading_text']) ? $ecs[$i]['heading_text'] : '',
];
// The add expand/collapse remove section button.
$settings['button'] = [
'#type' => 'submit',
'#value' => $this->t('Remove'),
'#submit' => [[$this, 'expandCollapseRemoveOne']],
'#ajax' => [
'callback' => [$this, 'expandCollapseCallback'],
'wrapper' => 'items-wrapper',
'element_id' => $weight,
],
'#attributes' => [
'class' => ['button--small'],
],
];
// The weight element.
$settings['weight'] = [
'#type' => 'weight',
@@ -270,10 +287,12 @@ class UwCblExpandCollapse extends BlockBase implements ContainerFactoryPluginInt
'#value' => $this->t('Add expand/collapse section'),
'#submit' => [[$this, 'expandCollapseAddOne']],
'#ajax' => [
'callback' => [$this, 'expandCollapseAddOneCallback'],
'callback' => [$this, 'expandCollapseCallback'],
'wrapper' => 'items-wrapper',
],
'#attributes' => [
'class' => ['button--large'],
],
];
return $form;
@@ -311,7 +330,48 @@ class UwCblExpandCollapse extends BlockBase implements ContainerFactoryPluginInt
}
/**
* The ajax call back for expand/collapse add one.
* Remove one e/c section from the form.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
public function expandCollapseRemoveOne(array &$form, FormStateInterface $form_state) {
// Get the triggering element, so that we can get
// which element we need to remove.
$triggering_element = $form_state->getTriggeringElement();
// Get the element id from the triggering element.
$element_id = $triggering_element['#ajax']['element_id'];
// Get the current values of the form_state.
$values = $form_state->getValues();
$ecs = $values['settings']['items_fieldset']['items'];
unset($ecs[$element_id]);
// Set the ecs from the items in the form_state, so that we
// will load newly added values.
$form_state->set('ecs', $ecs);
// Decrement the num_of_rows, since we are removing one.
$num_of_rows = $form_state->get('num_of_rows');
$num_of_rows--;
$form_state->set('num_of_rows', $num_of_rows);
// Set the add_more_called flag, so that we will load
// the items by form_state, rather than block config.
$form_state->set('add_more_called', TRUE);
// Rebuild form with 1 extra row.
$form_state->setRebuild();
}
/**
* The ajax call back for expand/collapse remove one.
*
* @param array $form
* The form array.
@@ -321,7 +381,7 @@ class UwCblExpandCollapse extends BlockBase implements ContainerFactoryPluginInt
* @return mixed
* The form element to be overwritten.
*/
public function expandCollapseAddOneCallback(array &$form, FormStateInterface $form_state) {
public function expandCollapseCallback(array &$form, FormStateInterface $form_state) {
return $form['settings']['items_fieldset'];
}
Loading