You are here

private function AuthorizationProfileForm::buildMappingForm in Authorization 8

Build the mapping form.

Parameters

array $form: An associative array containing the structure of the form.

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

1 call to AuthorizationProfileForm::buildMappingForm()
AuthorizationProfileForm::form in src/Form/AuthorizationProfileForm.php
Gets the actual form array to be built.

File

src/Form/AuthorizationProfileForm.php, line 494

Class

AuthorizationProfileForm
Authorization profile form.

Namespace

Drupal\authorization\Form

Code

private function buildMappingForm(array &$form, FormStateInterface $form_state) : void {
  $authorization_profile = $this
    ->getEntity();
  if (($authorization_profile
    ->hasValidProvider() || $form_state
    ->getValue('provider')) && ($authorization_profile
    ->hasValidConsumer() || $form_state
    ->getValue('consumer'))) {
    $provider = $authorization_profile
      ->getProvider();
    $consumer = $authorization_profile
      ->getConsumer();
    $tokens = [];
    $tokens += $provider
      ->getTokens();
    $tokens += $consumer
      ->getTokens();
    $form['mappings'] = [
      '#type' => 'table',
      '#responsive' => TRUE,
      '#weight' => 100,
      '#title' => $this
        ->t('Configure mapping from @provider_name to @consumer_name', $tokens),
      '#header' => [
        $provider
          ->label(),
        $consumer
          ->label(),
        $this
          ->t('Delete'),
      ],
      '#prefix' => '<div id="authorization-mappings-wrapper">',
      '#suffix' => '</div>',
    ];
    $mappings_fields = $form_state
      ->get('mappings_fields');
    if (empty($mappings_fields)) {
      $count_current_mappings = max(count($authorization_profile
        ->getProviderMappings()), count($authorization_profile
        ->getConsumerMappings()));
      $mappings_fields = $count_current_mappings > 0 ? $count_current_mappings - 1 : 1;
      $form_state
        ->set('mappings_fields', $mappings_fields);
    }
    for ($row_key = 0; $row_key <= $mappings_fields; $row_key++) {
      $form['mappings'][$row_key]['provider_mappings'] = $provider
        ->buildRowForm($form, $form_state, $row_key);
      $form['mappings'][$row_key]['consumer_mappings'] = $consumer
        ->buildRowForm($form, $form_state, $row_key);
      $form['mappings'][$row_key]['delete'] = [
        '#type' => 'checkbox',
        '#default_value' => 0,
      ];
    }
    $form['mappings'][]['mappings_add_another'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Add Another'),
      '#submit' => [
        '::mappingsAddAnother',
      ],
      '#limit_validation_errors' => [],
      '#ajax' => [
        'callback' => '::mappingsAjaxCallback',
        'wrapper' => 'authorization-mappings-wrapper',
      ],
      '#weight' => 103,
      '#wrapper_attributes' => [
        'colspan' => 3,
      ],
    ];
    $form['mappings_provider_help'] = [
      '#type' => 'markup',
      '#markup' => $provider
        ->buildRowDescription($form, $form_state),
      '#weight' => 101,
    ];
    $form['mappings_consumer_help'] = [
      '#type' => 'markup',
      '#markup' => $consumer
        ->buildRowDescription($form, $form_state),
      '#weight' => 102,
    ];
  }
}