You are here

public function EditForm::form in Access Filter 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/EditForm.php, line 58

Class

EditForm
Provides edit filter form.

Namespace

Drupal\access_filter\Form

Code

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

  /* @var Filter $filter */
  $filter = $this->entity;
  $filter
    ->parse();
  $form['general'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('General'),
  ];
  $form['general']['status'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#default_value' => $filter
      ->status(),
  ];
  $form['general']['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Name'),
    '#default_value' => $filter
      ->label(),
    '#max_length' => 255,
  ];
  $form['general']['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $filter
      ->id(),
    '#machine_name' => [
      'source' => [
        'general',
        'name',
      ],
      'exists' => [
        Filter::class,
        'load',
      ],
    ],
    '#disabled' => !$filter
      ->isNew(),
  ];
  $form['conditions'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Conditions'),
  ];
  $form['conditions']['conditions'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Conditions'),
    '#title_display' => 'invisible',
    '#description' => $this
      ->t('Write in YAML format like below...') . '<br>' . $this
      ->renderPluginDescription($this->conditionPluginManager
      ->getDefinitions()) . $this
      ->t('All conditions can be negated by specifying "negate: 1".'),
    '#attributes' => [
      'data-yaml-editor' => 'true',
    ],
    '#default_value' => $filter
      ->get('conditions'),
  ];
  $form['rules'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Rules'),
  ];
  $form['rules']['rules'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Rules'),
    '#title_display' => 'invisible',
    '#description' => $this
      ->t('Write in YAML format like below...') . $this
      ->renderPluginDescription($this->rulePluginManager
      ->getDefinitions()),
    '#attributes' => [
      'data-yaml-editor' => 'true',
    ],
    '#default_value' => $filter
      ->get('rules'),
  ];
  $form['response'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Response'),
    '#tree' => TRUE,
  ];
  $form['response']['code'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Response code'),
    '#options' => [
      200 => '200 OK',
      301 => '301 Moved Permanently',
      302 => '302 Moved Temporarily',
      403 => '403 Forbidden',
      404 => '404 Not Found',
      410 => '410 Gone',
      500 => '500 Internal Server Error',
      503 => '503 Service Unavailable',
    ],
    '#default_value' => $filter
      ->get('parsedResponse')['code'],
  ];
  $form['response']['redirect_url'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Redirect URL'),
    '#description' => $this
      ->t('Affects only response code 301, 302.'),
    '#default_value' => $filter
      ->get('parsedResponse')['redirect_url'],
  ];
  $form['response']['body'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Response body'),
    '#description' => $this
      ->t('Affects only response code except 301, 302.'),
    '#default_value' => $filter
      ->get('parsedResponse')['body'],
  ];
  $form['actions']['save'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
  ];
  $form['#attached']['library'][] = 'access_filter/common';
  return $form;
}