You are here

public function SettingsForm::buildForm in Trailing Slash 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/SettingsForm.php, line 67

Class

SettingsForm
Class SettingsForm

Namespace

Drupal\trailing_slash\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('trailing_slash.settings');
  $form['enabled'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#default_value' => $config
      ->get('enabled'),
  ];
  $form['paths'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('List of paths with trailing slash'),
    '#description' => $this
      ->t("Write a path per line where you want a trailing slash. Paths start with slash. (e.g., '/book')"),
    '#default_value' => $config
      ->get('paths'),
  ];
  $form['enabled_entity_types'] = [
    '#type' => 'details',
    '#open' => TRUE,
    '#title' => $this
      ->t('Enabled entity types'),
    '#description' => $this
      ->t('Enable to add a trailing slash for the given type.'),
    '#tree' => TRUE,
  ];
  $entity_types = TrailingSlashSettingsHelper::getContentEntityTypes();
  $bundle_info = $this->entityTypeBundleInfo
    ->getAllBundleInfo();
  $enabled_entity_types = unserialize($config
    ->get('enabled_entity_types'));
  foreach ($entity_types as $entity_type_id => $entity_type) {
    $entity_type_bundles = $bundle_info[$entity_type_id];
    $form['enabled_entity_types'][$entity_type_id] = [
      '#type' => 'details',
      '#open' => TRUE,
      '#title' => $entity_type
        ->getLabel(),
      '#tree' => TRUE,
    ];
    foreach ($entity_type_bundles as $bundle_id => $bundle) {
      $form['enabled_entity_types'][$entity_type_id][$bundle_id] = [
        '#type' => 'checkbox',
        '#title' => $bundle['label'],
        '#default_value' => $enabled_entity_types[$entity_type_id][$bundle_id],
      ];
    }
  }
  return parent::buildForm($form, $form_state);
}