You are here

public function ContainerForm::form in GoogleTagManager 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/ContainerForm.php, line 70

Class

ContainerForm
Defines the Google tag manager container settings form.

Namespace

Drupal\google_tag\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $container = $this->container = $this->entity;
  $this->prefix = '';

  // Store the contexts for other objects to use during form building.
  $form_state
    ->setTemporaryValue('gathered_contexts', $this->contextRepository
    ->getAvailableContexts());

  // The main premise of entity forms is that we get to work with an entity
  // object at all times instead of checking submitted values from the form
  // state.
  // Build form elements.
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => 'Label',
    '#default_value' => $container
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $container
      ->id(),
    '#required' => TRUE,
    '#machine_name' => [
      'exists' => [
        $this,
        'containerExists',
      ],
      'replace_pattern' => '[^a-z0-9_.]+',
    ],
  ];
  $form['settings'] = [
    '#type' => 'vertical_tabs',
    '#title' => $this
      ->t('Container settings'),
    '#description' => $this
      ->t('The settings affecting the snippet contents for this container.'),
    '#attributes' => [
      'class' => [
        'google-tag',
      ],
    ],
  ];
  $form['conditions'] = [
    '#type' => 'vertical_tabs',
    '#title' => $this
      ->t('Insertion conditions'),
    '#description' => $this
      ->t('The snippet insertion conditions for this container.'),
    '#attributes' => [
      'class' => [
        'google-tag',
      ],
    ],
    '#attached' => [
      'library' => [
        'google_tag/drupal.settings_form',
      ],
    ],
  ];
  $form['general'] = $this
    ->generalFieldset($form_state);
  $form['advanced'] = $this
    ->advancedFieldset($form_state);
  $form['path'] = $this
    ->pathFieldset($form_state);
  $form['role'] = $this
    ->roleFieldset($form_state);
  $form['status'] = $this
    ->statusFieldset($form_state);
  $form += $this
    ->conditionsForm([], $form_state);
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => 'Save',
  ];
  $form['actions']['delete'] = [
    '#type' => 'submit',
    '#value' => 'Delete',
  ];
  return $form;
}