You are here

public function SettingsForm::buildForm in Like & Dislike 8

Defines the settings form for each entity type bundles.

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 Form definition array.

Overrides ConfigFormBase::buildForm

File

src/Form/SettingsForm.php, line 104

Class

SettingsForm
Class SettingsForm.

Namespace

Drupal\like_and_dislike\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('like_and_dislike.settings');
  $form['enabled_types'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Entity types with Like & Dislike widgets enabled'),
    '#description' => $this
      ->t('If you disable any type here, already existing data will remain untouched.'),
    '#tree' => TRUE,
  ];
  foreach ($this->entityTypeManager
    ->getDefinitions() as $entity_type_id => $entity_type) {

    // Only display content entities with a view builder.
    if ($entity_type
      ->getGroup() != 'content' || !$entity_type
      ->hasHandlerClass('view_builder')) {
      continue;
    }
    $form['enabled_types'][$entity_type_id] = [
      '#type' => 'container',
      '#tree' => TRUE,
    ];

    // Checkbox to enable and disable the entity type.
    $form['enabled_types'][$entity_type_id]['enabled'] = [
      '#type' => 'checkbox',
      '#title' => $entity_type
        ->getLabel(),
      '#default_value' => is_array($config
        ->get('enabled_types.' . $entity_type_id)),
    ];

    // Display entity type bundles.
    if ($entity_type
      ->hasKey('bundle')) {
      $bundles = $this->bundleInfoService
        ->getBundleInfo($entity_type_id);

      // Get bundle label.
      $bundles = array_map(function ($bundle_info) {
        return $bundle_info['label'];
      }, $bundles);
      $form['enabled_types'][$entity_type_id]['bundle_info'] = [
        '#title' => $this
          ->getBundleTypeLabel($entity_type),
        '#type' => 'details',
        '#open' => TRUE,
        '#states' => [
          'invisible' => [
            'input[name="enabled_types[' . $entity_type_id . '][enabled]"]' => [
              'checked' => FALSE,
            ],
          ],
        ],
      ];
      $form['enabled_types'][$entity_type_id]['bundle_info']['bundles'] = [
        '#type' => 'checkboxes',
        '#options' => $bundles,
        '#default_value' => $config
          ->get('enabled_types.' . $entity_type_id) ?: [],
      ];
    }
  }

  // Checkbox to allow vote cancellation.
  $form['allow_cancel_vote'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow vote cancellation'),
    '#description' => $this
      ->t('Whether the users should be allowed to cancel their own votes by voting again for the same choice of the same poll.'),
    '#default_value' => $config
      ->get('allow_cancel_vote'),
  ];

  // Checkbox to allow hiding of vote widgets on missing permission.
  $form['hide_vote_widget'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Hide vote widget instead of disabling it'),
    '#description' => $this
      ->t('If checked then instead of disabled widget user will not see widget at all if vote permission is missing.'),
    '#default_value' => $config
      ->get('hide_vote_widget'),
  ];
  return parent::buildForm($form, $form_state);
}