You are here

public function ScriptForm::form in Script Manager 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/ScriptForm.php, line 69

Class

ScriptForm
The script entity add form.

Namespace

Drupal\script_manager\Form

Code

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

  // See \Drupal\Core\Condition\ConditionPluginBase::buildConfigurationForm.
  $form_state
    ->setTemporaryValue('gathered_contexts', $this->contextRepository
    ->getAvailableContexts());
  $form['#tree'] = TRUE;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Script Name'),
    '#default_value' => $this->entity
      ->label(),
    '#size' => 30,
    '#required' => TRUE,
    '#maxlength' => 64,
    '#description' => $this
      ->t('A human readable name to easily identify the script, eg "Google analytics".'),
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $this->entity
      ->id(),
    '#required' => TRUE,
    '#disabled' => !$this->entity
      ->isNew(),
    '#size' => 30,
    '#maxlength' => 64,
    '#machine_name' => [
      'exists' => [
        '\\Drupal\\script_manager\\Entity\\Script',
        'load',
      ],
    ],
  ];
  $form['position'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Script Position'),
    '#required' => TRUE,
    '#options' => [
      'top' => $this
        ->t('Top'),
      'bottom' => $this
        ->t('Bottom'),
      'hidden' => $this
        ->t('Not shown'),
    ],
    '#default_value' => $this->entity
      ->getPosition(),
  ];
  $form['snippet'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('HTML Snippet'),
    '#description' => $this
      ->t('The snippet of JavaScript to display on the page.'),
    '#required' => TRUE,
    '#default_value' => $this->entity
      ->getSnippet(),
  ];
  $form['visibility'] = $this
    ->buildVisibilityForm($form_state);
  return $form;
}