You are here

public function AuthorizationProfileForm::buildEntityForm in Authorization 8

Builds the form for the basic server properties.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: Form state.

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

File

src/Form/AuthorizationProfileForm.php, line 149

Class

AuthorizationProfileForm
Authorization profile form.

Namespace

Drupal\authorization\Form

Code

public function buildEntityForm(array &$form, FormStateInterface $form_state) : void {
  $authorization_profile = $this
    ->getEntity();
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Profile name'),
    '#maxlength' => 255,
    '#default_value' => $authorization_profile
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $authorization_profile
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\authorization\\Entity\\AuthorizationProfile::load',
    ],
    '#disabled' => !$authorization_profile
      ->isNew(),
  ];

  /* You will need additional form elements for your custom properties. */
  $form['status'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#default_value' => $authorization_profile
      ->get('status'),
  ];
  $provider_options = $this
    ->getProviderOptions();
  if ($provider_options) {
    if (count($provider_options) === 1) {
      $authorization_profile
        ->set('provider', key($provider_options));
    }
    $form['provider'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Provider'),
      '#options' => $provider_options,
      '#default_value' => $authorization_profile
        ->getProviderId(),
      '#required' => TRUE,
      '#ajax' => [
        'callback' => [
          get_class($this),
          'buildAjaxProviderConfigForm',
        ],
        'wrapper' => 'authorization-profile-form',
        'method' => 'replace',
        'effect' => 'fade',
      ],
    ];
  }
  else {
    $this
      ->messenger()
      ->addError($this
      ->t('There are no provider plugins available. You will need to install and enable something like the LDAP Authorization Provider module that ships with LDAP.'));
    $form['#access'] = FALSE;
    $form['#markup'] = $this
      ->t('Authorization profile cannot be created.');
    $form['#cache'] = [
      'tags' => [],
      'contexts' => [],
      'max-age' => 0,
    ];
  }
  $consumer_options = $this
    ->getConsumerOptions();
  if ($consumer_options) {
    if (count($consumer_options) == 1) {
      $authorization_profile
        ->set('consumer', key($consumer_options));
    }
    $form['consumer'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Consumer'),
      '#options' => $consumer_options,
      '#default_value' => $authorization_profile
        ->getConsumerId(),
      '#required' => TRUE,
      '#ajax' => [
        'callback' => [
          get_class($this),
          'buildAjaxConsumerConfigForm',
        ],
        'wrapper' => 'authorization-profile-form',
        'method' => 'replace',
        'effect' => 'fade',
      ],
    ];
  }
  else {
    $this
      ->messenger()
      ->addError($this
      ->t('There are no consumer plugins available. You can enable the Authorization Drupal Roles submodule to provide integration with core user roles or write your own using that as a template.'));
    $form['#access'] = FALSE;
    $form['#markup'] = $this
      ->t('Authorization profile cannot be created.');
    $form['#cache'] = [
      'tags' => [],
      'contexts' => [],
      'max-age' => 0,
    ];
  }
}