You are here

protected function RemoteForm::addAuthOptions in Entity Share 8.3

Helper function to build the authorization options in the form.

Parameters

array $form: The form.

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

Throws

\Drupal\Component\Plugin\Exception\PluginException

1 call to RemoteForm::addAuthOptions()
RemoteForm::form in modules/entity_share_client/src/Form/RemoteForm.php
Gets the actual form array to be built.

File

modules/entity_share_client/src/Form/RemoteForm.php, line 152

Class

RemoteForm
Entity form of the remote entity.

Namespace

Drupal\entity_share_client\Form

Code

protected function addAuthOptions(array &$form, FormStateInterface $form_state) {
  $options = [];
  $plugins = [];
  $commonUuid = '';
  if ($this
    ->hasAuthPlugin()) {
    $options[$this->authPlugin
      ->getPluginId()] = $this->authPlugin
      ->getLabel();
    $plugins[$this->authPlugin
      ->getPluginId()] = $this->authPlugin;

    // Ensure all plugins will have the same uuid in the configuration to
    // avoid duplication of entries in the key value store.
    $existing_plugin_configuration = $this->authPlugin
      ->getConfiguration();
    $commonUuid = $existing_plugin_configuration['uuid'];
  }
  $availablePlugins = $this->authPluginManager
    ->getAvailablePlugins($commonUuid);
  foreach ($availablePlugins as $id => $plugin) {
    if (empty($options[$id])) {

      // This plugin type was not previously set as an option.
      $options[$id] = $plugin
        ->getLabel();
      $plugins[$id] = $plugin;
    }
  }

  // Do we have a value?
  $selected = $form_state
    ->getValue('pid');
  if (!empty($selected)) {
    $selectedPlugin = $plugins[$selected];
  }
  elseif (!empty($this->authPlugin)) {

    // Is a plugin previously stored?
    $selectedPlugin = $this->authPlugin;
  }
  else {

    // Fallback: take the first option.
    $selectedPlugin = reset($plugins);
  }
  $form['auth'] = [
    '#type' => 'container',
    '#plugins' => $plugins,
    'pid' => [
      '#type' => 'radios',
      '#title' => $this
        ->t('Authorization methods'),
      '#options' => $options,
      '#default_value' => $selectedPlugin
        ->getPluginId(),
      '#ajax' => [
        'wrapper' => 'plugin-form-ajax-container',
        'callback' => [
          get_class($this),
          'ajaxPluginForm',
        ],
      ],
    ],
    'data' => [],
  ];
  $subformState = SubformState::createForSubform($form['auth']['data'], $form, $form_state);
  $form['auth']['data'] = $selectedPlugin
    ->buildConfigurationForm($form['auth']['data'], $subformState);
  $form['auth']['data']['#tree'] = TRUE;
  $form['auth']['data']['#prefix'] = '<div id="plugin-form-ajax-container">';
  $form['auth']['data']['#suffix'] = '</div>';
}