You are here

class CancelButtonSettingsForm in Entity Form Cancel Button 8

Configure generic settings for the Cancel Button.

Hierarchy

Expanded class hierarchy of CancelButtonSettingsForm

1 string reference to 'CancelButtonSettingsForm'
cancel_button.routing.yml in ./cancel_button.routing.yml
cancel_button.routing.yml

File

src/Form/CancelButtonSettingsForm.php, line 16

Namespace

Drupal\cancel_button\Form
View source
class CancelButtonSettingsForm extends ConfigFormBase {

  /**
   * The path validator.
   *
   * @var \Drupal\Core\Path\PathValidatorInterface
   */
  protected $pathValidator;

  /**
   * The request context.
   *
   * @var \Drupal\Core\Routing\RequestContext
   */
  protected $requestContext;

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a CancelButtonSettingsForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
   *   The path validator.
   * @param \Drupal\Core\Routing\RequestContext $request_context
   *   The request context.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, PathValidatorInterface $path_validator, RequestContext $request_context, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($config_factory);
    $this->pathValidator = $path_validator;
    $this->requestContext = $request_context;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('path.validator'), $container
      ->get('router.request_context'), $container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'cancel_button_admin_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'cancel_button.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $result = $this
      ->getEntityTypesToDisplay();
    $entity_types = $result["entity_types"];
    $bundles = $result["bundles"];
    $entity_types += [
      'default' => NULL,
    ];

    // Based on whether the entity type has bundles or not, the fieldsets
    // contain settings for either the entity type or the bundles.
    // Validate appropriately.
    foreach ($entity_types as $id => $type) {
      if (!(array_key_exists($id, $bundles) && count($bundles[$id]) > 0)) {
        $field = $id . '_cancel_destination';
        $value = $form_state
          ->getValue($field);
        if (empty($value) && $form_state
          ->getValue($id . '_cancel_enabled')) {
          $form_state
            ->setErrorByName($field, $this
            ->t('You must enter a path in this field'));
          $form['entity_type_cancel_destination'][$id]['#open'] = TRUE;
        }
        elseif (!$this->pathValidator
          ->isValid($value)) {
          $form_state
            ->setErrorByName($field, $this
            ->t("The path '%path' is either invalid or you do not have access to it.", [
            '%path' => $value,
          ]));
          $form['entity_type_cancel_destination'][$id]['#open'] = TRUE;
        }
      }
      if (array_key_exists($id, $bundles) && count($bundles[$id]) > 0) {
        foreach ($bundles[$id] as $bundle) {
          $field = $id . '_' . $bundle
            ->id() . '_cancel_destination';
          $value = $form_state
            ->getValue($field);
          if (empty($value) && $form_state
            ->getValue($id . '_cancel_enabled')) {
            $form_state
              ->setErrorByName($field, $this
              ->t('You must enter a path in this field'));
            $form['entity_type_cancel_destination'][$id][$id . '_bundles']['#open'] = TRUE;
          }
          elseif (!$this->pathValidator
            ->isValid($value)) {
            $form_state
              ->setErrorByName($field, $this
              ->t("The path '%path' is either invalid or you do not have access to it.", [
              '%path' => $value,
            ]));
            $form['entity_type_cancel_destination'][$id][$id . '_bundles']['#open'] = TRUE;
          }
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('cancel_button.settings');
    $result = $this
      ->getEntityTypesToDisplay();
    $entity_types = $result["entity_types"];
    $bundles = $result["bundles"];
    $entity_types += [
      'default' => NULL,
    ];

    // If the entity type is not fieldable (doesn't have bundles), store config
    // for entity type, otherwise store config for a combination of entity type
    // and bundle.
    foreach ($entity_types as $id => $type) {
      if (array_key_exists($id, $bundles) && count($bundles[$id]) > 0) {
        foreach ($bundles[$id] as $bundle) {
          $config
            ->set('entity_type_cancel_destination.' . $id . '_' . $bundle
            ->id() . '.path', $form_state
            ->getValue($id . '_' . $bundle
            ->id() . '_cancel_destination'));
        }
      }
      else {
        $config
          ->set('entity_type_cancel_destination.' . $id . '.path', $form_state
          ->getValue($id . '_cancel_destination'));
      }
      $config
        ->set('entity_type_cancel_destination.' . $id . '.enabled', (bool) $form_state
        ->getValue($id . '_cancel_enabled'));
    }
    $config
      ->save();
    parent::submitForm($form, $form_state);
  }

  /**
   * Gets all the entity types and bundles if applicable for processing.
   */
  public function getEntityTypesToDisplay() {
    $entity_types = $this->entityTypeManager
      ->getDefinitions();
    $entity_types_to_return = [];
    $bundles = [];
    foreach ($entity_types as $entity_type_id => $entity_type) {

      // Do not consider entities with wizard forms.
      if (array_key_exists('wizard', $entity_type
        ->getHandlerClasses())) {
        continue;
      }
      if ($entity_type
        ->hasKey('bundle')) {
        $bundle_entity_type = $entity_type
          ->getBundleEntityType();
        if (!empty($bundle_entity_type)) {
          $bundles[$entity_type_id] = $this->entityTypeManager
            ->getStorage($bundle_entity_type)
            ->loadMultiple();
        }
      }
      $entity_types_to_return[$entity_type_id] = $entity_type;
    }
    return [
      'entity_types' => $entity_types_to_return,
      'bundles' => $bundles,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CancelButtonSettingsForm::$entityTypeManager protected property The entity manager.
CancelButtonSettingsForm::$pathValidator protected property The path validator.
CancelButtonSettingsForm::$requestContext protected property The request context.
CancelButtonSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
CancelButtonSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
CancelButtonSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
CancelButtonSettingsForm::getEntityTypesToDisplay public function Gets all the entity types and bundles if applicable for processing.
CancelButtonSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
CancelButtonSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
CancelButtonSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
CancelButtonSettingsForm::__construct public function Constructs a CancelButtonSettingsForm object. Overrides ConfigFormBase::__construct
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.