You are here

function context_groups_form_context_edit_submit in Context groups 8.2

Same name and namespace in other branches
  1. 8 context_groups.module \context_groups_form_context_edit_submit()

Submit callback on context edit form save.

Parameters

array $form: Form array.

FormStateInterface $form_state: FormStateObject.

1 string reference to 'context_groups_form_context_edit_submit'
context_groups_form_context_edit_form_alter in ./context_groups.module
Implements hook_form_FORM_ID_alter().

File

./context_groups.module, line 98
Hooks and helper functions.

Code

function context_groups_form_context_edit_submit(array &$form, FormStateInterface $form_state) {

  // Get list of blocks and context groups.
  $form_values =& $form_state
    ->getValue([
    'reactions',
    'blocks',
    'blocks',
    'blocks',
  ]);

  // Get context groups from this context reaction.
  $complete_form = $form_state
    ->getCompleteForm();

  // If there is no context groups inside context, do nothing.
  if (!isset($complete_form['reactions']['#context_groups'])) {
    return;
  }
  $context_groups = $complete_form['reactions']['#context_groups'];

  // Remove context groups from list of block in context reaction.
  $context_groups_values = [];
  foreach ($context_groups as $group_name) {
    if (isset($form_values[$group_name])) {
      $context_groups_values[$group_name] = $form_values[$group_name];
      unset($form_values[$group_name]);
    }
  }

  // Load context.
  $context = $form_state
    ->getFormObject()
    ->getEntity();

  // Get save context groups.
  $saved_context_groups = $context
    ->getThirdPartySettings('context_groups');

  // Get all context groups parents of a block.
  foreach ($form_values as $key => $block) {
    $form_values[$key]['all_parents'] = \Drupal::service('context_groups.manager')
      ->getAllParentsForGroup($saved_context_groups, $block['parent_wrapper']['parent']);
  }

  // Update context groups in context.
  foreach ($context_groups_values as $name => $value) {
    $group = $saved_context_groups[$name];
    $group['region'] = $value['region'];
    $group['weight'] = $value['weight'];
    $group['parent'] = $value['parent_wrapper']['parent'];
    $group['all_parents'] = \Drupal::service('context_groups.manager')
      ->getAllParentsForGroup($saved_context_groups, $value['parent_wrapper']['parent']);
    $context
      ->setThirdPartySetting('context_groups', $name, $group);
  }
  $context
    ->save();
}