You are here

abstract class FormModeManagerFormBase in Form mode manager 8.2

Base for implementing system configuration forms for Form Mode Manager.

This abstract class allow you to play with all entities and form modes, without write ton of redondant code to loop on each entities compatible, with form mode manager and his form modes associated.

Hierarchy

Expanded class hierarchy of FormModeManagerFormBase

2 files declare their use of FormModeManagerFormBase
FormModeManagerRolesForm.php in modules/form_mode_user_roles_assign/src/Form/FormModeManagerRolesForm.php
FormModeThemeSwitcherForm.php in modules/form_mode_theme_switcher/src/Form/FormModeThemeSwitcherForm.php

File

src/Form/FormModeManagerFormBase.php, line 21

Namespace

Drupal\form_mode_manager\Form
View source
abstract class FormModeManagerFormBase extends ConfigFormBase {

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

  /**
   * The settings object.
   *
   * @var \Drupal\Core\Site\Settings
   */
  protected $settings;

  /**
   * The entity display repository.
   *
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
   */
  protected $entityDisplayRepository;

  /**
   * The form_mode_manager service.
   *
   * @var \Drupal\form_mode_manager\FormModeManager
   */
  protected $formModeManager;

  /**
   * The cache tags invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   */
  protected $cacheTagsInvalidator;

  /**
   * The form modes list to exclude are used or ignored.
   *
   * @var bool
   */
  protected $ignoreExcluded;

  /**
   * Ignore Active display checks from list.
   *
   * @var bool
   */
  protected $ignoreActiveDisplay;

  /**
   * Constructs a CropWidgetForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
   *   The entity display repository.
   * @param \Drupal\form_mode_manager\FormModeManagerInterface $form_mode_manager
   *   The form_mode_manager service.
   * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
   *   The cache tags invalidator.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityDisplayRepositoryInterface $entity_display_repository, FormModeManagerInterface $form_mode_manager, CacheTagsInvalidatorInterface $cache_tags_invalidator, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($config_factory);
    $this->settings = $this
      ->getConfig();
    $this->entityDisplayRepository = $entity_display_repository;
    $this->formModeManager = $form_mode_manager;
    $this->cacheTagsInvalidator = $cache_tags_invalidator;
    $this->entityTypeManager = $entity_type_manager;
    $this->ignoreExcluded = FALSE;
    $this->ignoreActiveDisplay = TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('entity_display.repository'), $container
      ->get('form_mode.manager'), $container
      ->get('cache_tags.invalidator'), $container
      ->get('entity_type.manager'));
  }

  /**
   * Retrieve the current class editable config name.
   *
   * @return \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig
   *   The setting object.
   */
  protected function getConfig() {
    return $this
      ->config(current($this
      ->getEditableConfigNames()));
  }

  /**
   * Build Form Mode Manager settings form for each entity and form modes.
   *
   * This method implement use abstract buildFormPerEntity &,
   * buildFormPerFormMode to take control of element you need for each,
   * compatible entities and form modes used by Form Mode Manager module.
   * You can use one or all methods to applies your own logic on all,
   * entities/modes you need without code duplication.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   *
   * @return array
   *   The structure of the form populated.
   */
  public function buildFormModeForm(array &$form) {
    $form_modes = $this->formModeManager
      ->getAllFormModesDefinitions($this->ignoreExcluded, $this->ignoreActiveDisplay);
    if (empty($form_modes)) {
      $form['empty']['#markup'] = t('Any Form modes activated in form display found.');
      return $form;
    }
    foreach (array_keys($form_modes) as $entity_type_id) {
      $this
        ->buildFormPerEntity($form, $form_modes, $entity_type_id);
      foreach ($form_modes[$entity_type_id] as $form_mode) {
        $this
          ->buildFormPerFormMode($form, $form_mode, $entity_type_id);
      }
    }
    return $form;
  }

  /**
   * Set Form Mode Manager settings from form for each entity and form modes.
   *
   * This method implement use abstract buildSettingsPerEntity &,
   * buildSettingsPerFormMode to take control of element you need,
   * to save for all compatible entities and/or form modes.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function setFormModeFormSettings(FormStateInterface $form_state) {
    $form_modes = $this->formModeManager
      ->getAllFormModesDefinitions($this->ignoreExcluded, $this->ignoreActiveDisplay);
    if (empty($form_modes)) {
      return;
    }
    foreach (array_keys($form_modes) as $entity_type_id) {
      $this
        ->setSettingsPerEntity($form_state, $form_modes, $entity_type_id);
      foreach ($form_modes[$entity_type_id] as $form_mode) {
        $this
          ->setSettingsPerFormMode($form_state, $form_mode, $entity_type_id);
      }
    }
  }

  /**
   * Build form element per compatible entities.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param array $form_modes
   *   The form modes collection for given entity type.
   * @param string $entity_type_id
   *   The entity type ID of entity.
   *
   * @return string
   *   The name of the theme
   */
  public abstract function buildFormPerEntity(array &$form, array $form_modes, $entity_type_id);

  /**
   * Build form element per form modes linked by given entity type.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param array $form_mode
   *   The form mode definition.
   * @param string $entity_type_id
   *   The entity type ID of entity.
   *
   * @return $this|false
   *   The form Object.
   */
  public abstract function buildFormPerFormMode(array &$form, array $form_mode, $entity_type_id);

  /**
   * Set settings per compatible entities.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param array $form_modes
   *   The form modes collection for given entity type.
   * @param string $entity_type_id
   *   The entity type ID of entity.
   *
   * @return $this|false
   *   The form Object.
   */
  public abstract function setSettingsPerEntity(FormStateInterface $form_state, array $form_modes, $entity_type_id);

  /**
   * Set settings per form modes for a given entity type.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param array $form_mode
   *   The form mode definition.
   * @param string $entity_type_id
   *   The entity type ID of entity.
   *
   * @return $this|false
   *   The form Object.
   */
  public abstract function setSettingsPerFormMode(FormStateInterface $form_state, array $form_mode, $entity_type_id);

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $this
      ->buildFormModeForm($form);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $this
      ->setFormModeFormSettings($form_state);
    $this->settings
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
ConfigFormBaseTrait::getEditableConfigNames abstract protected function Gets the configuration names that will be editable. 32
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
FormInterface::getFormId public function Returns a unique string identifying the form. 236
FormModeManagerFormBase::$cacheTagsInvalidator protected property The cache tags invalidator.
FormModeManagerFormBase::$entityDisplayRepository protected property The entity display repository.
FormModeManagerFormBase::$entityTypeManager protected property The entity type manager service.
FormModeManagerFormBase::$formModeManager protected property The form_mode_manager service.
FormModeManagerFormBase::$ignoreActiveDisplay protected property Ignore Active display checks from list.
FormModeManagerFormBase::$ignoreExcluded protected property The form modes list to exclude are used or ignored.
FormModeManagerFormBase::$settings protected property The settings object.
FormModeManagerFormBase::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm 4
FormModeManagerFormBase::buildFormModeForm public function Build Form Mode Manager settings form for each entity and form modes.
FormModeManagerFormBase::buildFormPerEntity abstract public function Build form element per compatible entities. 4
FormModeManagerFormBase::buildFormPerFormMode abstract public function Build form element per form modes linked by given entity type. 4
FormModeManagerFormBase::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create 1
FormModeManagerFormBase::getConfig protected function Retrieve the current class editable config name.
FormModeManagerFormBase::setFormModeFormSettings public function Set Form Mode Manager settings from form for each entity and form modes.
FormModeManagerFormBase::setSettingsPerEntity abstract public function Set settings per compatible entities. 4
FormModeManagerFormBase::setSettingsPerFormMode abstract public function Set settings per form modes for a given entity type. 4
FormModeManagerFormBase::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm 4
FormModeManagerFormBase::__construct public function Constructs a CropWidgetForm object. Overrides ConfigFormBase::__construct 4
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.