You are here

public function CustomFilterForm::form in Custom filter 2.0.x

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

Class

CustomFilterForm
Builds the form to add/edit a Custom Filter.

Namespace

Drupal\customfilter\Form

Code

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

  /** @var \Drupal\customfilter\Entity\CustomFilter $filter */
  $filter = $this->entity;
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $filter
      ->label(),
    '#description' => $this
      ->t("Label for the filter."),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $filter
      ->id(),
    '#description' => $this
      ->t('The machine-readable name of the filter. This name
         must contain only lowercase letters, numbers, and underscores and
         can not be changed latter'),
    '#machine_name' => [
      'exists' => [
        CustomFilter::class,
        'load',
      ],
      'source' => [
        'name',
      ],
    ],
    '#disabled' => !$filter
      ->isNew(),
    '#required' => TRUE,
  ];
  $form['disable_cache'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Force disable cache'),
    '#description' => $this
      ->t("BEWARE: you probably don't want to uncheck this. If you do, the filter result will not be cached and will rebuild everytime the content is displayed."),
    '#default_value' => !$filter
      ->getCache(),
  ];
  $form['description'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Description'),
    '#description' => $this
      ->t('A description of the purpose of this filter.'),
    '#default_value' => $filter
      ->getDescription(),
  ];
  $form['shorttip'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Tips (short)'),
    '#default_value' => $filter
      ->getShorttip(),
    '#rows' => 5,
    '#description' => $this
      ->t('This tip will be show in edit content/comments forms.'),
  ];
  $form['longtip'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Tips (full)'),
    '#default_value' => $filter
      ->getLongtip(),
    '#rows' => 20,
  ];
  $form['rules'] = [
    '#type' => 'value',
    '#value' => $filter->rules,
  ];
  return $form;
}