Browse Source

improved edlp_corpus cache invalidation

Bachir Soussi Chiadmi 6 years ago
parent
commit
f741a01bbe

+ 12 - 23
sites/all/modules/figli/edlp_corpus/edlp_corpus.module

@@ -8,6 +8,7 @@
 # @License: GPL-V3
 
 use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
 
 /**
  * Implements hook_theme().
@@ -28,7 +29,6 @@ function edlp_corpus_theme($existing, $type, $theme, $path) {
 
 /**
 * hook_entity_extra_field_info()
-*
 */
 function edlp_corpus_entity_extra_field_info(){
   $extra = [];
@@ -45,7 +45,7 @@ function edlp_corpus_entity_extra_field_info(){
  * Implements hook_ENTITY_TYPE_view().
  * @see https://www.amazeelabs.com/en/render-menu-tree-custom-code-drupal-8
  */
-function edlp_corpus_taxonomy_term_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
+function edlp_corpus_taxonomy_term_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
   $index_display_settings = $display->getComponent('index');
   if (!empty($index_display_settings)) {
     // dpm($entity);
@@ -140,58 +140,47 @@ function edlp_corpus_page_attachments(array &$attachments) {
 }
 
 
-function edlp_corpus_invalidate_corpus_cache(){
-  \Drupal::service('cache_tags.invalidator')->invalidateTags(['rebuild-corpus-cache']);
+function _edlp_corpus_invalidate_corpus_cache($node){
+  if($node->getType() == 'enregistrement'){
+    $sid = WorkflowManager::getCurrentStateId($node, 'field_workflow');
+    if($sid == 'corpus_documents_publie'){
+      \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();
-  }
+  _edlp_corpus_invalidate_corpus_cache($node);
 }
-
 /**
  * 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();
-  }
+  _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();
-  }
+  _edlp_corpus_invalidate_corpus_cache();
 }

+ 5 - 3
sites/all/modules/figli/edlp_corpus/src/Controller/CorpusController.php

@@ -8,6 +8,7 @@ use Drupal\File\Entity\File;
 // use Symfony\Component\HttpFoundation\JsonResponse;
 use Drupal\Core\Cache\CacheableJsonResponse;
 use Drupal\Core\Cache\CacheableMetadata;
+use Drupal\Core\Ajax\AjaxResponse;
 
 
 class CorpusController extends ControllerBase {
@@ -119,9 +120,10 @@ class CorpusController extends ControllerBase {
     $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data));
 
     return $response;
-    // return array(
-    //   '#markup'=>'Hello Corpus'
-    // );
+
+    return array(
+      '#markup'=>'Hello Corpus'
+    );
   }
 
 }