class FormModeManagerDisplayEditForm in Form mode manager 8
Same name and namespace in other branches
- 8.2 src/Form/FormModeManagerDisplayEditForm.php \Drupal\form_mode_manager\Form\FormModeManagerDisplayEditForm
Form Mode Manager enhancements for edit form of the EntityFormDisplay.
This class permit to add a more specific caches and routes invalidate onto, formDisplay entity form elements. We haven't more common way to be plugged, in EntityDisplay form edit event and identify with precision when an user, add a form-mode onto an EntityType. With following code we have a flexible, and light way to add Form Mode Manager custom comportements like field_ui, way with `EntityFormDisplayEditForm`.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\field_ui\Form\EntityDisplayFormBase
- class \Drupal\field_ui\Form\EntityFormDisplayEditForm
- class \Drupal\form_mode_manager\Form\FormModeManagerDisplayEditForm
- class \Drupal\field_ui\Form\EntityFormDisplayEditForm
- class \Drupal\field_ui\Form\EntityDisplayFormBase
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of FormModeManagerDisplayEditForm
File
- src/
Form/ FormModeManagerDisplayEditForm.php, line 23
Namespace
Drupal\form_mode_manager\FormView source
class FormModeManagerDisplayEditForm extends EntityFormDisplayEditForm {
/**
* The cache tags invalidator.
*
* @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
*/
protected $cacheTagsInvalidator;
/**
* The route builder service.
*
* @var \Drupal\Core\Routing\RouteBuilderInterface
*/
protected $routeBuilder;
/**
* Constructs a new FormModeManagerDisplayEditForm.
*
* @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
* The field type manager.
* @param \Drupal\Component\Plugin\PluginManagerBase $plugin_manager
* The widget or formatter plugin manager.
* @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
* The cache tags invalidator.
* @param \Drupal\Core\Routing\RouteBuilderInterface $route_builder
* The route builder service.
*/
public function __construct(FieldTypePluginManagerInterface $field_type_manager, PluginManagerBase $plugin_manager, CacheTagsInvalidatorInterface $cache_tags_invalidator, RouteBuilderInterface $route_builder) {
parent::__construct($field_type_manager, $plugin_manager);
$this->cacheTagsInvalidator = $cache_tags_invalidator;
$this->routeBuilder = $route_builder;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.field.field_type'), $container
->get('plugin.manager.field.widget'), $container
->get('cache_tags.invalidator'), $container
->get('router.builder'));
}
/**
* {@inheritdoc}
*
* Add more precise cache/routes invalidation when we are sure these,
* form-mode as added/deleted onto entities.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
if ($this
->isDefaultForm() && $this
->formModesUpdated($form, $form_state)) {
$this->cacheTagsInvalidator
->invalidateTags([
'local_action',
'local_tasks',
'entity_types',
'rendered',
]);
$this->routeBuilder
->rebuild();
}
}
/**
* Determine if the current form-mode is 'default'.
*
* @return bool
* True if this entityFormDisplay do rebuild routes.
*/
public function isDefaultForm() {
return 'default' === $this->entity
->getMode();
}
/**
* Determine whenever a formMode(s) has added/deleted onto entityTypes.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return bool
* True if this entityFormDisplay do rebuild routes.
*/
private function formModesUpdated(array $form, FormStateInterface $form_state) {
if (!isset($form['modes'])) {
return FALSE;
}
if ($this
->isNewFormMode($this
->defaultDisplayModes($form), $this
->submittedDisplayModes($form_state))) {
return TRUE;
}
if ($this
->updateDisplayModes($form, $form_state)) {
return TRUE;
}
return FALSE;
}
/**
* Determine whenever a formMode(s) has added/deleted onto entityTypes.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return bool
* True if this entityFormDisplay do rebuild routes.
*/
public function updateDisplayModes(array $form, FormStateInterface $form_state) {
return !empty(array_diff_assoc($this
->getDefaultModes($form), $this
->getSubmittedModes($form_state)));
}
/**
* Retrieve all form-modes selected before changes.
*
* @param array $form
* An associative array containing the structure of the form.
*
* @return array
* An array with all form-modes selected or empty.
*/
public function defaultDisplayModes(array $form) {
$display_modes = $this
->getDefaultModes($form);
if (!empty($display_modes)) {
return $display_modes;
}
return [];
}
/**
* Retrieve all form-modes submitted form-modes.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* An array with all form-modes selected or empty.
*/
public function submittedDisplayModes(FormStateInterface $form_state) {
$display_modes = $this
->getSubmittedModes($form_state);
if (!empty($display_modes) && is_array($display_modes)) {
return array_keys($display_modes);
}
return [];
}
/**
* Get value of 'display_modes_custom' element.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array|null
* An array with form-mode-id selected by users.
*/
public function getSubmittedModes(FormStateInterface $form_state) {
return $form_state
->getValue('display_modes_custom');
}
/**
* Get value of 'display_modes_custom' element.
*
* @param array $form
* An associative array containing the structure of the form.
*
* @return array|null
* An array with form-mode-id present before submission.
*/
public function getDefaultModes(array $form) {
return $form['modes']['display_modes_custom']['#default_value'];
}
/**
* Determine if we haven't any form-modes checked previously.
*
* @param array $form_mode_enabled
* An associative array containing all form-modes already used.
* @param array $display_mode_selected
* An associative array containing all form-modes to be enabled.
*
* @return bool
* True if this submit is the first form-mode to enable.
*/
private function isNewFormMode(array $form_mode_enabled, array $display_mode_selected) {
return empty($form_mode_enabled) && !empty($display_mode_selected);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityDisplayFormBase:: |
protected | property |
The entity being used by this form. Overrides EntityForm:: |
|
EntityDisplayFormBase:: |
protected | property | The entity display repository. | |
EntityDisplayFormBase:: |
protected | property | The entity field manager. | |
EntityDisplayFormBase:: |
protected | property | A list of field types. | |
EntityDisplayFormBase:: |
protected | property | The widget or formatter plugin manager. | |
EntityDisplayFormBase:: |
protected | function | Builds the table row structure for a single extra field. | |
EntityDisplayFormBase:: |
protected | function |
Copies top-level form values to entity properties Overrides EntityForm:: |
|
EntityDisplayFormBase:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
EntityDisplayFormBase:: |
protected | function | Returns an array of applicable widget or formatter options for a field. | |
EntityDisplayFormBase:: |
protected | function | Returns entity (form) displays for the current entity display type. | |
EntityDisplayFormBase:: |
protected | function | Returns form or view modes statuses for the bundle used by this form. | |
EntityDisplayFormBase:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityForm:: |
|
EntityDisplayFormBase:: |
protected | function | Returns the extra fields of the entity type and bundle used by this form. | |
EntityDisplayFormBase:: |
protected | function | Collects the definitions of fields whose display is configurable. | |
EntityDisplayFormBase:: |
public | function | Returns an associative array of all regions. | |
EntityDisplayFormBase:: |
public | function | Get the regions needed to create the overview form. | |
EntityDisplayFormBase:: |
public | function | Returns the region to which a row in the display overview belongs. | |
EntityDisplayFormBase:: |
public | function | Ajax handler for multistep buttons. | |
EntityDisplayFormBase:: |
public | function | Form submission handler for multistep buttons. | |
EntityDisplayFormBase:: |
public | function | Determines the rendering order of an array representing a tree. | |
EntityDisplayFormBase:: |
protected | function | Saves the updated display mode statuses. | |
EntityDisplayFormBase:: |
public | function | Performs pre-render tasks on field_ui_table elements. | |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityFormInterface:: |
41 |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
EntityFormDisplayEditForm:: |
protected | property |
The display context. Either 'view' or 'form'. Overrides EntityDisplayFormBase:: |
|
EntityFormDisplayEditForm:: |
protected | function |
Alters the widget or formatter settings summary. Overrides EntityDisplayFormBase:: |
|
EntityFormDisplayEditForm:: |
protected | function |
Builds the table row structure for a single field. Overrides EntityDisplayFormBase:: |
|
EntityFormDisplayEditForm:: |
protected | function |
Returns the ID of the default widget or formatter plugin for a field type. Overrides EntityDisplayFormBase:: |
|
EntityFormDisplayEditForm:: |
protected | function |
Returns an array of form or view mode options. Overrides EntityDisplayFormBase:: |
|
EntityFormDisplayEditForm:: |
protected | function |
Returns the form or view modes used by this form. Overrides EntityDisplayFormBase:: |
|
EntityFormDisplayEditForm:: |
protected | function |
Returns a link to the form or view mode admin page. Overrides EntityDisplayFormBase:: |
|
EntityFormDisplayEditForm:: |
protected | function |
Returns an entity display object to be used by this form. Overrides EntityDisplayFormBase:: |
|
EntityFormDisplayEditForm:: |
protected | function |
Returns the Url object for a specific entity (form) display edit form. Overrides EntityDisplayFormBase:: |
|
EntityFormDisplayEditForm:: |
protected | function |
Returns an array containing the table headers. Overrides EntityDisplayFormBase:: |
|
EntityFormDisplayEditForm:: |
protected | function |
Adds the widget or formatter third party settings forms. Overrides EntityDisplayFormBase:: |
|
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
FormModeManagerDisplayEditForm:: |
protected | property | The cache tags invalidator. | |
FormModeManagerDisplayEditForm:: |
protected | property | The route builder service. | |
FormModeManagerDisplayEditForm:: |
public static | function |
Instantiates a new instance of this class. Overrides EntityFormDisplayEditForm:: |
|
FormModeManagerDisplayEditForm:: |
public | function | Retrieve all form-modes selected before changes. | |
FormModeManagerDisplayEditForm:: |
private | function | Determine whenever a formMode(s) has added/deleted onto entityTypes. | |
FormModeManagerDisplayEditForm:: |
public | function | Get value of 'display_modes_custom' element. | |
FormModeManagerDisplayEditForm:: |
public | function | Get value of 'display_modes_custom' element. | |
FormModeManagerDisplayEditForm:: |
public | function | Determine if the current form-mode is 'default'. | |
FormModeManagerDisplayEditForm:: |
private | function | Determine if we haven't any form-modes checked previously. | |
FormModeManagerDisplayEditForm:: |
public | function |
Add more precise cache/routes invalidation when we are sure these,
form-mode as added/deleted onto entities. Overrides EntityDisplayFormBase:: |
|
FormModeManagerDisplayEditForm:: |
public | function | Retrieve all form-modes submitted form-modes. | |
FormModeManagerDisplayEditForm:: |
public | function | Determine whenever a formMode(s) has added/deleted onto entityTypes. | |
FormModeManagerDisplayEditForm:: |
public | function |
Constructs a new FormModeManagerDisplayEditForm. Overrides EntityDisplayFormBase:: |
|
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |