You are here

function context_groups_reaction_ui_display_form_alter in Context groups 8.2

Same name and namespace in other branches
  1. 8 includes/context_groups_ui.inc \context_groups_reaction_ui_display_form_alter()

Alter the reaction table display.

1 call to context_groups_reaction_ui_display_form_alter()
context_groups_process_reactions in ./context_groups.module
Alter the reactions in context_edit_form.

File

includes/context_groups_ui.inc, line 17
Altering display for reaction table.

Code

function context_groups_reaction_ui_display_form_alter(&$form, FormStateInterface $form_state) {

  // Current context.
  $context = $form_state
    ->getFormObject()
    ->getEntity()
    ->getOriginalId();
  $context = \Drupal::entityTypeManager()
    ->getStorage('context')
    ->load($context);

  // Get groups from the context.
  $params = \Drupal::service('context_groups.manager')
    ->getParams($context, $form_state);

  // If there is no groups in context, we don't need to alter the display.
  if (!$params) {
    return;
  }

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

  // Saved context blocks inside context.
  $context_blocks = $context
    ->getReactions()
    ->get('blocks')
    ->getBlocks();
  $context_blocks_keys = array_keys($context_blocks
    ->getConfiguration());
  $form['#context_groups'] = array_keys($params->group_list);
  $table =& $form['reaction-blocks']['options']['blocks']['blocks'];
  $original_table = $form['reaction-blocks']['options']['blocks']['blocks'];
  unset($form['reaction-blocks']['options']['blocks']['blocks']);
  $table = [];
  $table = [
    '#type' => 'field_ui_table',
    '#header' => [
      'Name',
      'Category',
      'Unique',
      'Weight',
      'Region',
      'Parent',
      'Operations',
    ],
    '#attributes' => [
      'id' => 'context-groups-draggable',
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'field-weight',
      ],
      [
        'action' => 'match',
        'relationship' => 'parent',
        'group' => 'field-parent',
      ],
    ],
  ];

  // Add regions to table.
  $table['#regions'] = context_groups_add_regions_to_table($original_table, $regions, $context_blocks_keys);
  $region_keys = array_keys($regions);
  $form['#attached']['drupalSettings']['context_groups_regions'] = $region_keys;

  // Place blocks in region.
  foreach ($context_blocks as $block) {
    $configuration = $block
      ->getConfiguration();

    // Set parent default.
    $parent_default = isset($configuration['parent_wrapper']['parent']) ? $configuration['parent_wrapper']['parent'] : '';
    $operations = [
      'edit' => [
        'title' => t('Edit'),
        'url' => Url::fromRoute('context.reaction.blocks.block_edit', [
          'context' => $context
            ->id(),
          'reaction_id' => 'blocks',
          'block_id' => $configuration['uuid'],
        ], [
          'query' => [
            'theme' => $theme,
          ],
        ]),
        'attributes' => [
          'class' => [
            'use-ajax',
          ],
          'data-dialog-type' => 'modal',
          'data-dialog-options' => Json::encode([
            'width' => 1000,
          ]),
        ],
      ],
      'delete' => [
        'title' => t('Delete'),
        'url' => Url::fromRoute('context.reaction.blocks.block_delete', [
          'context' => $context
            ->id(),
          'block_id' => $configuration['uuid'],
        ]),
        'attributes' => [
          'class' => [
            'use-ajax',
          ],
          'data-dialog-type' => 'modal',
          'data-dialog-options' => Json::encode([
            'width' => 1000,
          ]),
        ],
      ],
    ];
    $table[$configuration['uuid']] = [
      '#attributes' => [
        'class' => [
          'draggable',
          'tabledrag-leaf',
        ],
        'data-region' => 'region-' . $configuration['region'],
      ],
      '#region_callback' => 'context_groups_region_callback',
      'title' => [
        '#markup' => $block
          ->label(),
      ],
      'category' => [
        '#markup' => $block
          ->getPluginDefinition()['category'],
      ],
      'unique' => [
        '#markup' => $configuration['unique'] ? t('Yes') : t('No'),
      ],
      'weight' => [
        '#type' => 'textfield',
        '#default_value' => isset($configuration['weight']) ? $configuration['weight'] : 0,
        '#size' => 3,
        '#title' => 'Weight for row',
        '#title_display' => 'invisible',
        '#attributes' => [
          'class' => [
            'field-weight',
          ],
        ],
      ],
      'region' => [
        '#type' => 'select',
        '#title' => t('Region'),
        '#title_display' => 'invisible',
        '#default_value' => $configuration['region'],
        '#options' => $regions,
        '#attributes' => [
          'class' => [
            'block-region-select',
            'block-region-' . $configuration['region'],
          ],
        ],
      ],
      'parent_wrapper' => [
        'parent' => [
          '#type' => 'select',
          '#attributes' => [
            'class' => [
              'field-parent',
            ],
          ],
          '#options' => $params->group_list,
          '#default_value' => $parent_default,
        ],
      ],
      'operations' => [
        '#type' => 'operations',
        '#links' => $operations,
      ],
    ];
  }
  context_groups_add_groups_to_table($table, $params, $context, $regions);
  $form['reaction-blocks']['options']['blocks']['blocks'] = $table;
}