You are here

function context_groups_process_reactions in Context groups 8.2

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

Alter the reactions in context_edit_form.

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

File

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

Code

function context_groups_process_reactions(&$form, FormStateInterface $form_state) {
  $context = $form_state
    ->getFormObject()
    ->getEntity();

  // Show button to add group only if any reaction is set.
  if (!$context
    ->getReactions()
    ->has('blocks')) {
    return $form;
  }

  // Get all regions from selected theme.
  $theme = \Drupal::service('context_groups.manager')
    ->getCurrentTheme($form_state);

  // If user has permission, show him add group button.
  if (Drupal::currentUser()
    ->hasPermission('create context groups')) {
    $form['reaction-blocks']['options']['blocks']['group_add'] = [
      '#type' => 'link',
      '#title' => t('Add group'),
      '#attributes' => [
        'class' => [
          'use-ajax',
          'button',
          'button--small',
          'button-action',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => 500,
        ]),
      ],
      '#url' => Url::fromRoute('context_groups.group_add', [
        'context' => $context
          ->getOriginalId(),
        'theme' => $theme,
      ]),
      '#weight' => -1,
    ];
  }

  // Set weight to Place block button to be the first link.
  $form['reaction-blocks']['options']['blocks']['block_add']['#weight'] = -2;
  $form_state
    ->loadInclude('context_groups', 'inc', 'includes/context_groups_ui');
  context_groups_reaction_ui_display_form_alter($form, $form_state);
  return $form;
}