You are here

public function CancelButtonSettingsForm::buildForm in Entity Form Cancel Button 8

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/CancelButtonSettingsForm.php, line 90

Class

CancelButtonSettingsForm
Configure generic settings for the Cancel Button.

Namespace

Drupal\cancel_button\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $url_prefix = $this->requestContext
    ->getCompleteBaseUrl();

  // Get the entities and bundles which will be listed in the settings form.
  // We only show entities that don't have wizards.
  $result = $this
    ->getEntityTypesToDisplay();
  $entity_types = $bundles = [];
  if (!empty($result)) {
    $entity_types = $result['entity_types'];
    $bundles = $result['bundles'];
  }

  // Get existing config, if any.
  $config = $this
    ->config('cancel_button.settings');
  $entity_type_cancel_destinations = $config
    ->get('entity_type_cancel_destination');
  $entity_types += [
    'default' => NULL,
  ];

  // The form elements in this form are built based on whether the entity type
  // has bundles or not. If there are no bundles, we save the path/ display
  // corresponding form elements for the entity type, otherwise we save the
  // path /list form elements for each bundle in the entity type.
  // The setting for enabling the cancel button is stored at the entity type
  // level.
  foreach ($entity_types as $id => $entity_type) {

    // Set title and description values.
    if (!empty($entity_type)) {
      $label = $entity_type
        ->getLabel();
      $title = $this
        ->t('@label entity form', [
        '@label' => $label,
      ]);
      $description = $this
        ->t('The default destination for the <strong>Cancel</strong> button on @label entity forms.', [
        '@label' => $label,
      ]);
    }
    else {
      $title = $this
        ->t('Default entity form');
      $description = $this
        ->t('The default destination for the <strong>Cancel</strong> button on all other entity forms not otherwise configured here.');
      $label = 'Default';
    }

    // Settings related to enabling the cancel button on the entity forms.
    if (array_key_exists($id, $entity_type_cancel_destinations)) {
      $default_enabled = isset($entity_type_cancel_destinations[$id]['enabled']) ? $entity_type_cancel_destinations[$id]['enabled'] : TRUE;
    }
    else {
      $default_enabled = isset($entity_type_cancel_destinations['default']['enabled']) ? $entity_type_cancel_destinations['default']['enabled'] : TRUE;
    }
    $checkbox = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable cancel button for forms of the @label entity type.', [
        '@label' => $label,
      ]),
      '#default_value' => $default_enabled,
    ];
    $enabled_checkbox = $id . '_cancel_enabled';

    // Determine states for the path textfield based on whether the cancel
    // button is enabled for the entity type.
    $states = [
      'enabled' => [
        ':input[name="' . $enabled_checkbox . '"]' => [
          'checked' => TRUE,
        ],
      ],
      'required' => [
        ':input[name="' . $enabled_checkbox . '"]' => [
          'checked' => TRUE,
        ],
      ],
    ];

    // If this entity type has bundles, only display elements for each
    // bundle.
    if (array_key_exists($id, $bundles) && count($bundles[$id]) > 0) {

      // Wrap all the bundles within one detail element.
      if (!empty($entity_type)) {
        $form['entity_type_cancel_destination'][$id . '_bundles'] = [
          '#type' => 'details',
          '#title' => $this
            ->t('@label', [
            '@label' => $label,
          ]),
          '#open' => FALSE,
          '#description' => $this
            ->t('For specifying settings for each bundle in this entity type, enter values below.'),
        ];
      }

      // Display one 'enabled' checkbox for the entity type.
      $form['entity_type_cancel_destination'][$id . '_bundles'][$id . '_cancel_enabled'] = $checkbox;

      // Build the form elements for all bundles.
      foreach ($bundles[$id] as $bundle) {
        $bundle_label = $bundle
          ->label();

        // Set title and description values.
        if (!empty($entity_type)) {
          $title = $this
            ->t('@label Bundle form', [
            '@label' => $bundle_label,
          ]);
          $description = $this
            ->t('The default destination for the <strong>Cancel</strong> button on @label bundle forms.', [
            '@label' => $bundle_label,
          ]);
        }
        if (array_key_exists($id . '_' . $bundle
          ->id(), $entity_type_cancel_destinations)) {
          $default_path = $entity_type_cancel_destinations[$id . '_' . $bundle
            ->id()]['path'];
        }
        else {
          $default_path = $entity_type_cancel_destinations['default']['path'];
        }
        $form['entity_type_cancel_destination'][$id . '_bundles'][$id . '_' . $bundle
          ->id() . '_cancel_destination'] = [
          '#type' => 'textfield',
          '#title' => $title,
          '#default_value' => $default_path,
          '#description' => $description,
          '#field_prefix' => $url_prefix,
          '#states' => $states,
        ];
      }
    }
    else {
      if (array_key_exists($id, $entity_type_cancel_destinations)) {
        $default_path = $entity_type_cancel_destinations[$id]['path'];
      }
      else {
        $default_path = $entity_type_cancel_destinations['default']['path'];
      }

      // Wrap each element for the entity type within a details element.
      $form['entity_type_cancel_destination'][$id] = [
        '#type' => 'details',
        '#title' => $this
          ->t('@label', [
          '@label' => $label,
        ]),
        '#open' => FALSE,
        '#description' => '',
      ];
      $form['entity_type_cancel_destination'][$id][$id . '_cancel_enabled'] = $checkbox;

      // Build the form element.
      $form['entity_type_cancel_destination'][$id][$id . '_cancel_destination'] = [
        '#type' => 'textfield',
        '#title' => $title,
        '#default_value' => $default_path,
        '#description' => $description,
        '#field_prefix' => $url_prefix,
        '#states' => $states,
      ];
    }
  }
  return parent::buildForm($form, $form_state);
}