You are here

public function AnalyticsServiceForm::form in Analytics 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/AnalyticsServiceForm.php, line 53

Class

AnalyticsServiceForm

Namespace

Drupal\analytics\Form

Code

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

  // Plugin is not set when the entity is initially created.
  $plugin = $this->entity
    ->get('service') ? $this->entity
    ->getService() : NULL;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $this->entity
      ->label(),
    '#description' => $this
      ->t('The label for this service.'),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $this->entity
      ->id(),
    '#disabled' => !$this->entity
      ->isNew(),
    '#maxlength' => 64,
    '#machine_name' => [
      'exists' => [
        AnalyticsService::class,
        'load',
      ],
    ],
  ];
  $form['service'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Service'),
    '#options' => $this->analyticsServiceManager
      ->getDefinitionOptions(),
    '#default_value' => $plugin ? $plugin
      ->getPluginId() : NULL,
    '#required' => TRUE,
    '#ajax' => [
      'callback' => '::updateServiceSettings',
      'wrapper' => 'analytics-service-configuration-wrapper',
    ],
    '#disabled' => !$this->entity
      ->isNew(),
  ];
  $form['service_configuration'] = [
    '#type' => 'container',
    '#tree' => TRUE,
    '#prefix' => '<div id="analytics-service-configuration-wrapper">',
    '#suffix' => '</div>',
  ];
  if ($plugin) {
    $form['service_configuration'] = $plugin
      ->buildConfigurationForm($form['service_configuration'], $this
      ->getPluginSubFormState($form, $form_state));
  }
  return $form;
}