You are here

public function UrlRedirectForm::form in Url Redirect 8.2

Same name and namespace in other branches
  1. 8 src/Form/UrlRedirectForm.php \Drupal\url_redirect\Form\UrlRedirectForm::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/UrlRedirectForm.php, line 46

Class

UrlRedirectForm
Form handler for the Example add and edit forms.

Namespace

Drupal\url_redirect\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $url_redirect = $this->entity;
  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $url_redirect
      ->label(),
    '#description' => $this
      ->t("Label for the UrlRedirect."),
    '#required' => TRUE,
  );
  $form['id'] = array(
    '#type' => 'machine_name',
    '#default_value' => $url_redirect
      ->id(),
    '#machine_name' => array(
      'exists' => array(
        $this,
        'exist',
      ),
    ),
    '#disabled' => !$url_redirect
      ->isNew(),
  );
  $form['url'] = array(
    '#type' => 'fieldset',
    '#title' => t('Url Redirect'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['url']['path'] = array(
    '#type' => 'textfield',
    '#title' => 'Path',
    '#attributes' => array(
      'placeholder' => 'Enter Path',
    ),
    '#required' => TRUE,
    '#default_value' => $url_redirect
      ->get_path(),
    '#disabled' => !$url_redirect
      ->isNew(),
    '#description' => t('This can be an internal Drupal path such as node/add Enter <front> to link to the front page.'),
  );
  $form['url']['redirect_path'] = array(
    '#type' => 'textfield',
    '#title' => 'Redirect Path',
    '#attributes' => array(
      'placeholder' => 'Enter Redirect Path',
    ),
    '#required' => TRUE,
    '#default_value' => $url_redirect
      ->get_redirect_path(),
    '#description' => t('This redirect path can be internal Drupal path such as node/add Enter <front> to link to the front page.'),
  );
  $form['url']['checked_for'] = array(
    '#type' => 'radios',
    '#title' => t('Select Redirect path for'),
    '#options' => array(
      'Role' => t('Role'),
      'User' => t('User'),
    ),
    '#default_value' => $url_redirect
      ->get_checked_for(),
    '#required' => TRUE,
  );
  $form['url']['url_roles'] = array(
    '#type' => 'container',
    '#states' => array(
      'visible' => array(
        ':input[name="checked_for"]' => array(
          'value' => 'Role',
        ),
      ),
    ),
  );
  $user_roles = user_role_names();
  $form['url']['url_roles']['roles'] = array(
    '#type' => 'select',
    '#title' => t('Select Roles'),
    '#options' => $user_roles,
    '#multiple' => TRUE,
    '#tree' => TRUE,
    '#default_value' => $url_redirect
      ->get_roles(),
  );
  $form['url']['url_user'] = array(
    '#type' => 'container',
    '#states' => array(
      'visible' => array(
        ':input[name="checked_for"]' => array(
          'value' => 'User',
        ),
      ),
    ),
  );
  $redirect_users = $url_redirect
    ->get_users();
  if ($redirect_users) {
    $default_users = User::loadMultiple(array_column($redirect_users, 'target_id'));
  }
  else {
    $default_users = '';
  }
  $form['url']['url_user']['user'] = array(
    '#type' => 'entity_autocomplete',
    '#target_type' => 'user',
    '#default_value' => $default_users,
    '#multiple' => TRUE,
    '#tags' => TRUE,
  );
  $form['url']['negate'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Negate the condition'),
    '#default_value' => $url_redirect
      ->get('negate'),
  );
  $form['url']['message'] = array(
    '#type' => 'radios',
    '#title' => t('Display Message for Redirect'),
    '#required' => TRUE,
    '#description' => t('Show a message for redirect path.'),
    '#options' => array(
      'Yes' => t('Yes'),
      'No' => t('No'),
    ),
    '#default_value' => $url_redirect
      ->get_message(),
  );
  $form['url']['status'] = array(
    '#type' => 'radios',
    '#title' => t('Status'),
    '#options' => array(
      0 => t('Disabled'),
      1 => t('Enabled'),
    ),
    '#required' => TRUE,
    '#default_value' => $url_redirect
      ->get_status(),
  );
  return $form;
}