You are here

public function ContextEditForm::processReactions in Context 8.4

Same name and namespace in other branches
  1. 8 modules/context_ui/src/Form/ContextEditForm.php \Drupal\context_ui\Form\ContextEditForm::processReactions()
  2. 8.0 modules/context_ui/src/Form/ContextEditForm.php \Drupal\context_ui\Form\ContextEditForm::processReactions()

Process function for the reactions.

Parameters

array $element: The element to process.

\Drupal\Core\Form\FormStateInterface $form_state: The current form state.

Return value

array An array with the reaction element.

File

modules/context_ui/src/Form/ContextEditForm.php, line 161

Class

ContextEditForm
Provides a form to edit context.

Namespace

Drupal\context_ui\Form

Code

public function processReactions(array &$element, FormStateInterface $form_state) {
  $reactions = $this->entity
    ->getReactions();
  $element['add_reaction'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Add reaction'),
    '#url' => Url::fromRoute('context.reactions_library', [
      'context' => $this->entity
        ->id(),
    ]),
    '#attributes' => [
      'class' => [
        'use-ajax',
        'button',
        'button--small',
      ],
      'data-dialog-type' => 'modal',
      'data-dialog-options' => Json::encode([
        'width' => 700,
      ]),
    ],
  ];
  if (!count($reactions)) {
    $element['empty'] = [
      '#type' => 'container',
      '#markup' => $this
        ->t('No reactions has been added.'),
    ];
  }
  $element['reaction_tabs'] = [
    '#type' => 'vertical_tabs',
    '#parents' => [
      'reaction_tabs',
    ],
  ];
  foreach ($reactions as $reaction_id => $reaction) {
    $element['reaction-' . $reaction_id] = [
      '#type' => 'details',
      '#title' => $reaction
        ->getPluginDefinition()['label'],
      '#group' => 'reaction_tabs',
    ];
    $reaction_values = $form_state
      ->getValue([
      'reactions',
      $reaction_id,
    ], []);
    $reaction_state = (new FormState())
      ->setValues($reaction_values);
    $element['reaction-' . $reaction_id]['options'] = $reaction
      ->buildConfigurationForm([], $reaction_state, $this->entity);
    $element['reaction-' . $reaction_id]['options']['#parents'] = [
      'reactions',
      $reaction_id,
    ];
    $element['reaction-' . $reaction_id]['remove'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Remove reaction'),
      '#url' => Url::fromRoute('context.reaction_delete', [
        'context' => $this->entity
          ->id(),
        'reaction_id' => $reaction_id,
      ]),
      '#attributes' => [
        'class' => [
          'use-ajax',
          'button',
          'button--small',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => 700,
        ]),
      ],
    ];
  }
  return $element;
}