Skip to content
Snippets Groups Projects
Commit 72c2ac31 authored by Alexandre Mallet's avatar Alexandre Mallet Committed by Alexandre Mallet
Browse files

Issue #2870096 by woprrr: Fix caches of routes / local action

parent 83a49ac5
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,6 @@
* Contains form_mode_manager.module.
*/
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
......@@ -29,6 +28,15 @@ function form_mode_manager_entity_operation(EntityInterface $entity) {
->entityOperation($entity);
}
/**
* Implements hook_entity_update().
*/
function form_mode_manager_entity_update(EntityInterface $entity) {
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityTypeInfo::class)
->entityUpdate($entity);
}
/**
* Implements hook_theme().
*/
......@@ -67,20 +75,3 @@ function template_preprocess_form_mode_manager_add_list(array &$variables) {
}
}
}
/**
* Implements hook_entity_update().
*/
function form_mode_manager_entity_update(EntityInterface $entity) {
if ($entity instanceof EntityFormDisplay) {
$parent_entity = \Drupal::entityTypeManager()->getStorage($entity->getTargetEntityTypeId())->getEntityType();
// When user enable an form_mode we can create all,
// routes/elements to use/view it on entity lists.
if (!$parent_entity->hasLinkTemplate("{$entity->getMode()}") && ($entity->status() && $entity->getMode() != 'default')) {
// @TODO Found another solution to flush specific cache to,
// entities views listing cache each element and it's impossible,
// to predict the name of entity listing.
drupal_flush_all_caches();
}
}
}
......@@ -2,7 +2,9 @@
namespace Drupal\form_mode_manager;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
......@@ -40,6 +42,13 @@ class EntityTypeInfo implements ContainerInjectionInterface {
*/
protected $formModeManager;
/**
* The cache tags invalidator.
*
* @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
*/
protected $cacheTagsInvalidator;
/**
* EntityTypeInfo constructor.
*
......@@ -49,11 +58,14 @@ class EntityTypeInfo implements ContainerInjectionInterface {
* The entity display repository.
* @param \Drupal\form_mode_manager\FormModeManagerInterface $form_mode_manager
* The form mode manager.
* @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator
* The cache tags invalidator.
*/
public function __construct(AccountInterface $current_user, EntityDisplayRepositoryInterface $entity_display_repository, FormModeManagerInterface $form_mode_manager) {
public function __construct(AccountInterface $current_user, EntityDisplayRepositoryInterface $entity_display_repository, FormModeManagerInterface $form_mode_manager, CacheTagsInvalidatorInterface $cache_tags_invalidator) {
$this->entityDisplayRepository = $entity_display_repository;
$this->currentUser = $current_user;
$this->formModeManager = $form_mode_manager;
$this->cacheTagsInvalidator = $cache_tags_invalidator;
}
/**
......@@ -63,7 +75,8 @@ class EntityTypeInfo implements ContainerInjectionInterface {
return new static(
$container->get('current_user'),
$container->get('entity_display.repository'),
$container->get('form_mode.manager')
$container->get('form_mode.manager'),
$container->get('cache_tags.invalidator')
);
}
......@@ -83,14 +96,11 @@ class EntityTypeInfo implements ContainerInjectionInterface {
/* @var \Drupal\Core\Entity\EntityTypeInterface $entity_definition */
if ($entity_definition = $entity_types[$entity_type_id]) {
$form_modes = $this->formModeManager->getFormModesIdByEntity($entity_type_id);
// Add default callback to "edit as..." task.
if (($entity_definition->getFormClass('default') || $entity_definition->getFormClass('edit')) && $entity_definition->hasLinkTemplate('edit-form')) {
$entity_definition->setLinkTemplate('form-modes-links-task', "/form-mode-manager/$entity_type_id/{{$entity_type_id}}");
}
foreach ($form_modes as $form_mode_name) {
if ($default_form = $entity_definition->getFormClass('default')) {
$entity_definition->setFormClass($form_mode_name, $default_form);
}
// Add one entity operation for "edit" context.
if ($entity_definition->getFormClass($form_mode_name) && $entity_definition->hasLinkTemplate('edit-form')) {
$entity_definition->setLinkTemplate("edit-form.$form_mode_name", $entity_definition->getLinkTemplate('edit-form') . '/' . $form_mode_name);
......@@ -135,4 +145,26 @@ class EntityTypeInfo implements ContainerInjectionInterface {
return $operations;
}
/**
* Invalidate necessary tags on form mode activation .
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity on which to define an operation.
*
* `local_action` tag correspond to menu links action blocksn
* on overview entity list.
* `entity_types` tag need to be invalidate to inform system of new,
* Form & Link template are enabled/created on entityType basis.
*
* @see hook_entity_update()
*/
public function entityUpdate(EntityInterface $entity) {
if ($entity instanceof EntityFormDisplay && ($entity->status() && 'default' !== $entity->getMode())) {
$this->cacheTagsInvalidator->invalidateTags([
'local_action',
'entity_types',
]);
}
}
}
......@@ -69,8 +69,6 @@ class FormModeManagerConverter implements ParamConverterInterface {
* Extract the entity_type_id of current entity.
*/
protected function getEntityForm(array $defaults, array $definition) {
kint($defaults);
kint($definition);
$entity_form = $defaults['_entity_form'];
$form_mode_id = $defaults['_route_object']->getOption('_form_mode_manager_entity_type_id') . '.' . $defaults['form_mode_name'];
if ($entity_form === $form_mode_id) {
......
......@@ -6,6 +6,7 @@ use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\form_mode_manager\FormModeManagerInterface;
use Symfony\Component\Routing\Route;
......@@ -198,10 +199,8 @@ class RouteSubscriber extends RouteSubscriberBase {
if ($entity_add_admin_route = $collection->get('user.admin_create')) {
$route = $this->setRoutes($entity_add_admin_route, $entity_type, $form_mode);
$route
->setDefaults([
'_controller' => '\Drupal\form_mode_manager\Controller\UserFormModeController::entityAdd',
'_title_callback' => '\Drupal\form_mode_manager\Controller\UserFormModeController::addPageTitle',
]);
->setDefault('_controller', '\Drupal\form_mode_manager\Controller\UserFormModeController::entityAdd')
->setDefault('_title_callback', '\Drupal\form_mode_manager\Controller\UserFormModeController::addPageTitle');
return $route;
}
......@@ -225,10 +224,9 @@ class RouteSubscriber extends RouteSubscriberBase {
if ($entity_add_register_route = $collection->get('user.register')) {
$route = $this->setRoutes($entity_add_register_route, $entity_type, $form_mode);
$route
->setDefaults([
'_controller' => '\Drupal\form_mode_manager\Controller\UserFormModeController::entityAdd',
'_title_callback' => '\Drupal\form_mode_manager\Controller\UserFormModeController::addPageTitle',
]);
->setDefault('_controller', '\Drupal\form_mode_manager\Controller\UserFormModeController::entityAdd')
->setDefault('_title_callback', '\Drupal\form_mode_manager\Controller\UserFormModeController::addPageTitle');
return $route;
}
return NULL;
......@@ -249,15 +247,7 @@ class RouteSubscriber extends RouteSubscriberBase {
*/
protected function getFormModeManagerEditRoute(RouteCollection $collection, EntityTypeInterface $entity_type, array $form_mode) {
if ($entity_edit_route = $collection->get("entity.{$entity_type->id()}.edit_form")) {
$route = $this->setRoutes($entity_edit_route, $entity_type, $form_mode);
$route
->setDefaults([
'_controller' => '\Drupal\form_mode_manager\Controller\EntityFormModeController::entityAdd',
'_title_callback' => '\Drupal\form_mode_manager\Controller\EntityFormModeController::addPageTitle',
$entity_type->getBundleEntityType() => "{{$entity_type->getBundleEntityType()}}",
]);
return $route;
return $this->setRoutes($entity_edit_route, $entity_type, $form_mode);
}
return NULL;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment