You are here

public function HomeboxForm::form in Homebox 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/Form/HomeboxForm.php, line 133

Class

HomeboxForm
Class HomeboxForm.

Namespace

Drupal\homebox\Form

Code

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

  /* @var \Drupal\homebox\Entity\HomeboxInterface $homebox */
  $homebox = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $homebox
      ->label(),
    '#description' => $this
      ->t("Label for the Homebox."),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $homebox
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\homebox\\Entity\\Homebox::load',
    ],
    '#disabled' => !$homebox
      ->isNew(),
  ];
  $form['path'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Path'),
    '#maxlength' => 255,
    '#default_value' => $homebox
      ->getPath(),
    '#description' => $this
      ->t("Specify a URL by which this page can be accessed. For example, type \"dashboard\" when creating a Dashboard page. Use a relative path and don't add a trailing slash or the URL alias won't work."),
    '#required' => TRUE,
  ];
  $form['options'] = [
    '#type' => 'checkboxes',
    '#options' => [
      'menu' => t('Visible menu item'),
      'status' => t('Enable the page'),
    ],
  ];
  $roles = user_roles();
  $role_list = [];
  foreach ($roles as $id => $role) {
    if (!$role
      ->isAdmin()) {
      $role_list[$role
        ->id()] = $role
        ->label();
    }
  }
  $form['roles'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Allow only certain roles to access the page'),
    '#options' => array_map('\\Drupal\\Component\\Utility\\Html::escape', $role_list),
    '#description' => $this
      ->t('Select which roles can view the page.'),
  ];
  $form['columns'] = [
    '#type' => 'select',
    '#title' => t('Number of columns'),
    '#options' => $this->layoutPluginManager
      ->getLayoutOptions(),
    '#description' => t('Set the number of columns you want to activate for this Homebox page.'),
    '#default_value' => $homebox
      ->getRegions(),
  ];
  if (!$homebox
    ->isNew()) {
    $form['options']['#default_value'] = $homebox
      ->getOptions();
    $form['roles']['#default_value'] = $homebox
      ->getRoles();
  }
  return $form;
}