You are here

public function AccessTokenResourceForm::form in Simple OAuth (OAuth2) & OpenID Connect 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Entity/Form/AccessTokenResourceForm.php, line 43

Class

AccessTokenResourceForm
Class AccessTokenResourceForm.

Namespace

Drupal\simple_oauth\Entity\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /* @var \Drupal\simple_oauth\AccessTokenResourceInterface $access_token_resource */
  $access_token_resource = $this->entity;
  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $access_token_resource
      ->label(),
    '#description' => $this
      ->t('Label for the Access Token Resource.'),
    '#required' => TRUE,
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => $this
      ->t('Description'),
    '#default_value' => $access_token_resource
      ->getDescription(),
    '#description' => $this
      ->t('Description for the Access Token Resource.'),
    '#required' => FALSE,
  );
  $form['id'] = array(
    '#type' => 'machine_name',
    '#default_value' => $access_token_resource
      ->id(),
    '#machine_name' => array(
      'exists' => '\\Drupal\\simple_oauth\\Entity\\AccessTokenResource::load',
    ),
    '#disabled' => !$access_token_resource
      ->isNew(),
  );
  $permissions_list = [];
  $permission_handler = new PermissionHandler($this->moduleHandler, $this->stringTranslation, $this->controllerResolver);
  foreach ($permission_handler
    ->getPermissions() as $permission => $permission_info) {
    $permissions_list[$permission] = $permission_info['title'] . " ({$permission})";
  }
  $form['permissions'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Permissions'),
    '#default_value' => $access_token_resource
      ->get('permissions') ?: [],
    '#description' => $this
      ->t('A collection of permissions around a given feature. If a user is authenticated with a token that grants access to this scope, that user will ony be granted access (at most) to the permissions in this list. This will not grant access to any permissions forbidden to the user by their roles.'),
    '#options' => $permissions_list,
    '#required' => TRUE,
    '#attached' => [
      'library' => [
        'simple_oauth/drupal.access_token',
      ],
    ],
  ];
  return $form;
}