From 3c72614b3fceb6fe3cffd541d0c467f2a9f3a2c0 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Tue, 3 Apr 2018 18:05:41 +0200 Subject: [PATCH] activated cache for corpus map in edlp_corpus module --- .../figli/edlp_corpus/edlp_corpus.module | 61 ++++++++++++++++++- .../src/Controller/CorpusController.php | 21 +++++-- 2 files changed, 73 insertions(+), 9 deletions(-) diff --git a/sites/all/modules/figli/edlp_corpus/edlp_corpus.module b/sites/all/modules/figli/edlp_corpus/edlp_corpus.module index b65dfce0a..b1da4ddf6 100644 --- a/sites/all/modules/figli/edlp_corpus/edlp_corpus.module +++ b/sites/all/modules/figli/edlp_corpus/edlp_corpus.module @@ -7,6 +7,7 @@ # @Last modified time: 20-12-2017 # @License: GPL-V3 +use Drupal\Core\Entity\EntityInterface; /** * Implements hook_theme(). @@ -25,7 +26,6 @@ function edlp_corpus_theme($existing, $type, $theme, $path) { ); } - /** * hook_entity_extra_field_info() * @@ -41,7 +41,6 @@ function edlp_corpus_entity_extra_field_info(){ return $extra; } - /** * Implements hook_ENTITY_TYPE_view(). * @see https://www.amazeelabs.com/en/render-menu-tree-custom-code-drupal-8 @@ -87,7 +86,6 @@ function edlp_corpus_taxonomy_term_view(array &$build, \Drupal\Core\Entity\Entit } } - /** * Implements hook_page_attachments(). * @param array $attachments @@ -140,3 +138,60 @@ function edlp_corpus_page_attachments(array &$attachments) { $attachments['#attached']['drupalSettings']['edlp_corpus']['colors'] = $js_list; } + + +function edlp_corpus_invalidate_corpus_cache(){ + \Drupal::service('cache_tags.invalidator')->invalidateTags(['rebuild-corpus-cache']); +} + +/** + * Acts when creating a new entity of a specific type. + * + * This hook runs after a new entity object has just been instantiated. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity object. + * + * @ingroup entity_crud + * @see hook_entity_create() + */ +function edlp_corpus_node_create(EntityInterface $node) { + if($node->getType() == 'enregistrement'){ + edlp_corpus_invalidate_corpus_cache(); + } +} + +/** + * Respond to updates to an entity of a particular type. + * + * This hook runs once the entity storage has been updated. Note that hook + * implementations may not alter the stored entity data. Get the original entity + * object from $entity->original. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity object. + * + * @ingroup entity_crud + * @see hook_entity_update() + */ +function edlp_corpus_node_update(EntityInterface $node) { + if($node->getType() == 'enregistrement'){ + edlp_corpus_invalidate_corpus_cache(); + } +} +/** + * Respond to entity deletion. + * + * This hook runs once the entity has been deleted from the storage. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity object for the entity that has been deleted. + * + * @ingroup entity_crud + * @see hook_ENTITY_TYPE_delete() + */ +function edlp_corpus_node_delete(EntityInterface $node) { + if($node->getType() == 'enregistrement'){ + edlp_corpus_invalidate_corpus_cache(); + } +} diff --git a/sites/all/modules/figli/edlp_corpus/src/Controller/CorpusController.php b/sites/all/modules/figli/edlp_corpus/src/Controller/CorpusController.php index 89cad0e01..916a06a1f 100644 --- a/sites/all/modules/figli/edlp_corpus/src/Controller/CorpusController.php +++ b/sites/all/modules/figli/edlp_corpus/src/Controller/CorpusController.php @@ -5,7 +5,9 @@ namespace Drupal\edlp_corpus\Controller; use Drupal\Core\Controller\ControllerBase; use Drupal\workflow\Entity\WorkflowManager; use Drupal\File\Entity\File; -use Symfony\Component\HttpFoundation\JsonResponse; +// use Symfony\Component\HttpFoundation\JsonResponse; +use Drupal\Core\Cache\CacheableJsonResponse; +use Drupal\Core\Cache\CacheableMetadata; class CorpusController extends ControllerBase { @@ -27,8 +29,6 @@ class CorpusController extends ControllerBase { // Get a node storage object. // $file_storage = \Drupal::entityManager()->getStorage('node'); - $response = new JsonResponse(); - $config = \Drupal::config('system.site'); $query = \Drupal::entityQuery('node') @@ -76,8 +76,7 @@ class CorpusController extends ControllerBase { // if($has_article && $article_value[0]['value'] == "") // dpm($article_value); - // TODO: favoris marker - + // favoris marker $nodes_data[] = array( "nid" => $node->get('nid')->getString(), "title" => $node->get('title')->getString(), @@ -107,7 +106,17 @@ class CorpusController extends ControllerBase { 'entrees' => $entrees ); - $response->setData($data); + // https://spinningcode.org/2017/05/cached-json-responses-in-drupal-8/ + // cache is invalidated in edlp_corpus.module by tags + $data['#cache'] = [ + 'max-age' => \Drupal\Core\Cache\Cache::PERMANENT, + 'tags' => ['rebuild-corpus-cache'] + ]; + + // $response = new JsonResponse(); + // $response->setData($data); + $response = new CacheableJsonResponse($data); + $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data)); return $response; // return array(