You are here

public function ConfigPagesTypeForm::form in Config Pages 8

Same name and namespace in other branches
  1. 8.3 src/ConfigPagesTypeForm.php \Drupal\config_pages\ConfigPagesTypeForm::form()
  2. 8.2 src/ConfigPagesTypeForm.php \Drupal\config_pages\ConfigPagesTypeForm::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/ConfigPagesTypeForm.php, line 54

Class

ConfigPagesTypeForm
Base form for category edit forms.

Namespace

Drupal\config_pages

Code

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

  /* @var \Drupal\config_pages\ConfigPagesTypeInterface $config_pages_type */
  $config_pages_type = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#maxlength' => 255,
    '#default_value' => $config_pages_type
      ->label(),
    '#description' => t("Provide a label for this config page type to help identify it in the administration pages."),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $config_pages_type
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\config_pages\\Entity\\ConfigPagesType::load',
    ],
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#disabled' => !$config_pages_type
      ->isNew(),
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('Save'),
  ];
  $options = [];
  $items = \Drupal::service('plugin.manager.config_pages_context')
    ->getDefinitions();
  foreach ($items as $plugin_id => $item) {
    $options[$plugin_id] = $item['label'];
  }

  // Menu.
  $form['menu'] = [
    '#type' => 'details',
    '#title' => t('Menu'),
    '#tree' => TRUE,
    '#open' => TRUE,
  ];
  $form['menu']['path'] = [
    '#type' => 'textfield',
    '#description' => t('Menu path which will be used for form display.'),
    '#default_value' => !empty($config_pages_type->menu['path']) ? $config_pages_type->menu['path'] : [],
    '#required' => FALSE,
  ];

  // Context.
  $form['context'] = [
    '#type' => 'details',
    '#title' => t('Context'),
    '#tree' => TRUE,
    '#open' => FALSE,
  ];
  $form['context']['show_warning'] = [
    '#type' => 'checkbox',
    '#title' => t('Show context info message on ConfigPage edit form.'),
    '#default_value' => !empty($config_pages_type->context['show_warning']) ? $config_pages_type->context['show_warning'] : TRUE,
    '#required' => FALSE,
  ];
  $form['context']['group'] = [
    '#type' => 'checkboxes',
    '#description' => t('Consider following context for this configuration'),
    '#options' => $options,
    '#default_value' => !empty($config_pages_type->context['group']) ? $config_pages_type->context['group'] : [],
    '#required' => FALSE,
  ];
  return $form;
}