You are here

public function ConfigPagesTypeForm::form in Config Pages 8.3

Same name and namespace in other branches
  1. 8 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 73

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,
  ];
  $weight = [];
  foreach (range(-50, 50) as $number) {
    $weight[$number] = $number;
  }
  $form['menu']['weight'] = [
    '#type' => 'select',
    '#description' => t('Weight of menu item.'),
    '#options' => $weight,
    '#default_value' => !empty($config_pages_type->menu['weight']) ? $config_pages_type->menu['weight'] : 0,
    '#required' => FALSE,
  ];
  $form['menu']['description'] = [
    '#type' => 'textfield',
    '#description' => t('Description will be displayed under link in Drupal BO.'),
    '#default_value' => !empty($config_pages_type->menu['description']) ? $config_pages_type->menu['description'] : '',
    '#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,
  ];
  $default_options = [];
  if (!empty($config_pages_type->context['group'])) {
    foreach ($config_pages_type->context['group'] as $key => $value) {
      if ($value) {
        $default_options[] = $key;
      }
    }
  }
  $form['context']['group'] = [
    '#type' => 'checkboxes',
    '#description' => t('Consider following context for this configuration'),
    '#options' => $options,
    '#default_value' => $default_options,
    '#required' => FALSE,
  ];
  $form['context']['fallback_text'] = [
    '#prefix' => '<h2>',
    '#suffix' => '</h2>',
    '#markup' => $this
      ->t('Fallback for contexts'),
  ];
  foreach ($options as $contextId => $contextLabel) {
    $form['context']['fallback'][$contextId] = [
      '#type' => 'textfield',
      '#title' => $contextLabel,
      '#description' => $this
        ->t('Value that the context is going to have when no config page is found for the current context'),
      '#default_value' => empty($config_pages_type->context['fallback'][$contextId]) ? '' : $config_pages_type->context['fallback'][$contextId],
      '#required' => FALSE,
    ];
  }
  return $form;
}