You are here

public function GoogleAnalyticsCounterConfigureTypesForm::buildForm in Google Analytics Counter 8.3

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/GoogleAnalyticsCounterConfigureTypesForm.php, line 88

Class

GoogleAnalyticsCounterConfigureTypesForm
The form for editing content types with the custom google analytics counter field.

Namespace

Drupal\google_analytics_counter\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $options = NULL) {
  $config = $this
    ->config('google_analytics_counter.settings');

  // Add a checkbox to determine whether the storage for the custom field should be removed.
  $form['gac_custom_field_storage_status'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Custom field storage information'),
    '#open' => TRUE,
  ];
  $form['gac_custom_field_storage_status']['gac_type_remove_storage'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Remove the custom field'),
    '#description' => $this
      ->t('Removes the custom Google Analytics Counter field from the system completely.'),
    '#default_value' => $config
      ->get("general_settings.gac_type_remove_storage"),
  ];

  // Add a checkbox field for each content type.
  $form['gac_content_types'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Content types'),
    '#description' => $this
      ->t('Check the content types to add the custom Google Analytics Counter field to.'),
    '#open' => TRUE,
  ];
  $content_types = \Drupal::service('entity.manager')
    ->getStorage('node_type')
    ->loadMultiple();
  foreach ($content_types as $machine_name => $content_type) {
    $form['gac_content_types']["gac_type_{$machine_name}"] = [
      '#type' => 'checkbox',
      '#title' => $content_type
        ->label(),
      '#default_value' => $config
        ->get("general_settings.gac_type_{$machine_name}"),
      '#states' => [
        'disabled' => [
          ':input[name="gac_type_remove_storage"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }
  return parent::buildForm($form, $form_state);
}