You are here

public function UserDefaultPageConfigEntityForm::form in User Default Page 8

Same name and namespace in other branches
  1. 8.2 src/Form/UserDefaultPageConfigEntityForm.php \Drupal\user_default_page\Form\UserDefaultPageConfigEntityForm::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/UserDefaultPageConfigEntityForm.php, line 48

Class

UserDefaultPageConfigEntityForm
Class UserDefaultPageConfigEntityForm.

Namespace

Drupal\user_default_page\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $user_default_page_config_entity = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $user_default_page_config_entity
      ->label(),
    '#description' => $this
      ->t("Label for the User default page."),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $user_default_page_config_entity
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\user_default_page\\Entity\\UserDefaultPageConfigEntity::load',
    ],
    '#disabled' => !$user_default_page_config_entity
      ->isNew(),
  ];
  $form['roles_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('User / Role'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  ];
  $roles = [
    '' => '-Select-',
  ];
  foreach (user_roles(TRUE) as $role) {
    $roles[$role
      ->id()] = $role
      ->label();
  }
  $form['roles_fieldset']['user_roles'] = [
    '#title' => $this
      ->t('User Roles'),
    '#type' => 'select',
    '#description' => $this
      ->t("Select user roles"),
    '#options' => $roles,
    '#default_value' => $user_default_page_config_entity
      ->getUserRoles(),
    '#multiple' => TRUE,
  ];
  $form['roles_fieldset']['markup'] = [
    '#markup' => '<b>' . $this
      ->t('Select Role or User or both.') . '</b>',
  ];
  $user_values = $user_default_page_config_entity
    ->getUsers();
  $uids = explode(',', $user_values);
  $user_entity = $this->entityTypeManager
    ->getStorage('user');
  $default_users = $user_entity
    ->loadMultiple($uids);
  $form['roles_fieldset']['users'] = [
    '#type' => 'entity_autocomplete',
    '#target_type' => 'user',
    '#selection_settings' => [
      'include_anonymous' => FALSE,
    ],
    '#title' => $this
      ->t('Select User'),
    '#description' => $this
      ->t('Type Username here. Add multiple users as comma separated.'),
    '#tags' => TRUE,
    '#default_value' => $default_users,
  ];
  $weights = [
    -1 => '-None',
  ];
  for ($wi = 0; $wi <= 10; $wi++) {
    $weights[$wi] = $wi;
  }
  $form['roles_fieldset']['weight'] = [
    '#title' => $this
      ->t('Rule Weight'),
    '#type' => 'select',
    '#description' => $this
      ->t('The higher the value, the higher priority it receives.'),
    '#options' => $weights,
    '#default_value' => $user_default_page_config_entity
      ->getWeight(),
    '#multiple' => FALSE,
  ];
  $form['login_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Login'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  ];
  $form['login_fieldset']['login_redirect'] = [
    '#title' => $this
      ->t('Redirect to URL'),
    '#type' => 'textfield',
    '#size' => 64,
    '#description' => $this
      ->t("Enter the internal path."),
    '#default_value' => $user_default_page_config_entity
      ->getLoginRedirect(),
  ];
  $form['login_fieldset']['login_redirect_message'] = [
    '#title' => $this
      ->t('Message'),
    '#type' => 'textarea',
    '#description' => $this
      ->t("Enter the message to be displayed."),
    '#default_value' => $user_default_page_config_entity
      ->getLoginRedirectMessage(),
  ];
  $form['logout_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Logout'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  ];
  $form['logout_fieldset']['logout_redirect'] = [
    '#title' => $this
      ->t('Redirect to URL'),
    '#type' => 'textfield',
    '#size' => 64,
    '#description' => $this
      ->t("Enter the internal path."),
    '#default_value' => $user_default_page_config_entity
      ->getLogoutRedirect(),
  ];
  $form['logout_fieldset']['logout_redirect_message'] = [
    '#title' => $this
      ->t('Message'),
    '#type' => 'textarea',
    '#description' => $this
      ->t("Enter the message to be displayed."),
    '#default_value' => $user_default_page_config_entity
      ->getLogoutRedirectMessage(),
  ];
  return $form;
}