You are here

public function LoginDestinationRuleForm::form in Login Destination 8.2

Same name and namespace in other branches
  1. 8 src/Form/LoginDestinationRuleForm.php \Drupal\login_destination\Form\LoginDestinationRuleForm::form()

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/Form/LoginDestinationRuleForm.php, line 61

Class

LoginDestinationRuleForm
Base for controller for login destination add/edit forms.

Namespace

Drupal\login_destination\Form

Code

public function form(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\login_destination\Entity\LoginDestination $login_destination */
  $login_destination = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#default_value' => $login_destination
      ->getLabel(),
    '#description' => $this
      ->t('A short description of this login destination rule.'),
    '#required' => TRUE,
  ];
  $form['name'] = [
    '#type' => 'machine_name',
    '#machine_name' => [
      'exists' => [
        $this->loginDestinationStorage,
        'load',
      ],
    ],
    '#disabled' => !$login_destination
      ->isNew(),
    '#default_value' => $login_destination
      ->id(),
    '#required' => TRUE,
    '#description' => $this
      ->t('A unique machine-readable name for this login destination rule.'),
  ];
  $form['triggers'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Redirect upon triggers'),
    '#options' => [
      LoginDestination::TRIGGER_REGISTRATION => $this
        ->t('Registration'),
      LoginDestination::TRIGGER_LOGIN => $this
        ->t('Login'),
      LoginDestination::TRIGGER_ONE_TIME_LOGIN => $this
        ->t('One-time login link'),
      LoginDestination::TRIGGER_LOGOUT => $this
        ->t('Logout'),
    ],
    '#required' => TRUE,
    '#default_value' => !empty($login_destination->triggers) ? $login_destination
      ->getTriggers() : [],
    '#description' => $this
      ->t('Redirect only upon selected trigger(s).'),
  ];
  $form['destination_path'] = [
    '#type' => 'entity_autocomplete',
    '#target_type' => 'node',
    '#placeholder' => '',
    '#attributes' => [
      'data-autocomplete-first-character-blacklist' => '/#?[',
    ],
    '#title' => $this
      ->t('Redirect destination'),
    '#default_value' => $this
      ->getUriAsDisplayableString($login_destination
      ->getDestination()),
    '#element_validate' => [
      [
        $this,
        'validateUriElement',
      ],
    ],
    '#maxlength' => 2048,
    '#required' => TRUE,
    '#process_default_value' => FALSE,
    '#description' => $this
      ->t('Start typing the title of a piece of content to select it. You can also enter an internal path such as %add-node or an external URL such as %url. Enter %front to link to the front page. Enter %current to link to a current page.', [
      '%front' => '<front>',
      '%current' => '<current>',
      '%add-node' => '/node/add',
      '%url' => 'http://example.com',
    ]),
  ];

  // Add the token tree UI.
  $form['token_tree'] = [
    '#theme' => 'token_tree_link',
    '#token_types' => [
      'user',
    ],
    '#show_restricted' => TRUE,
    '#global_types' => TRUE,
  ];
  $form['pages_type'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Redirect from specific pages'),
    '#default_value' => $login_destination
      ->getPagesType(),
    '#options' => [
      $login_destination::REDIRECT_NOT_LISTED => $this
        ->t('All pages except those listed'),
      $login_destination::REDIRECT_LISTED => $this
        ->t('Only the listed pages'),
    ],
  ];
  $form['pages'] = [
    '#type' => 'textarea',
    '#default_value' => $login_destination
      ->getPages(),
    '#description' => $this
      ->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page. %login is the login form. %register is the registration form. %reset is the one-time login (e-mail validation).", [
      '%blog' => 'blog',
      '%blog-wildcard' => '/blog/*',
      '%front' => '<front>',
      '%login' => '/user',
      '%register' => '/user/register',
      '%reset' => '/user/*/edit',
    ]),
  ];
  $languages[''] = $this
    ->t('All languages');
  foreach ($this->languageManager
    ->getLanguages() as $key => $value) {
    $languages[$key] = $value
      ->getName();
  }
  $form['language'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Redirect for language'),
    '#options' => $languages,
    '#default_value' => $login_destination
      ->getLanguage(),
    '#description' => $this
      ->t('Redirect only for the selected language.'),
  ];
  $form['roles'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Redirect users with roles'),
    '#options' => $login_destination
      ->getAllSystemRoles(),
    '#default_value' => $login_destination
      ->getRoles(),
    '#description' => $this
      ->t('Redirect only the selected role(s). If you select no roles, all users will be redirected.'),
  ];
  $form['uuid'] = [
    '#type' => 'value',
    '#value' => $login_destination
      ->get('uuid'),
  ];
  return parent::form($form, $form_state);
}