Pārlūkot izejas kodu

activated cache for corpus map in edlp_corpus module

Bachir Soussi Chiadmi 7 gadi atpakaļ
vecāks
revīzija
3c72614b3f

+ 58 - 3
sites/all/modules/figli/edlp_corpus/edlp_corpus.module

@@ -7,6 +7,7 @@
 # @Last modified time: 20-12-2017
 # @Last modified time: 20-12-2017
 # @License: GPL-V3
 # @License: GPL-V3
 
 
+use Drupal\Core\Entity\EntityInterface;
 
 
 /**
 /**
  * Implements hook_theme().
  * Implements hook_theme().
@@ -25,7 +26,6 @@ function edlp_corpus_theme($existing, $type, $theme, $path) {
   );
   );
 }
 }
 
 
-
 /**
 /**
 * hook_entity_extra_field_info()
 * hook_entity_extra_field_info()
 *
 *
@@ -41,7 +41,6 @@ function edlp_corpus_entity_extra_field_info(){
   return $extra;
   return $extra;
 }
 }
 
 
-
 /**
 /**
  * Implements hook_ENTITY_TYPE_view().
  * Implements hook_ENTITY_TYPE_view().
  * @see https://www.amazeelabs.com/en/render-menu-tree-custom-code-drupal-8
  * @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().
  * Implements hook_page_attachments().
  * @param array $attachments
  * @param array $attachments
@@ -140,3 +138,60 @@ function edlp_corpus_page_attachments(array &$attachments) {
   $attachments['#attached']['drupalSettings']['edlp_corpus']['colors'] = $js_list;
   $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();
+  }
+}

+ 15 - 6
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\Core\Controller\ControllerBase;
 use Drupal\workflow\Entity\WorkflowManager;
 use Drupal\workflow\Entity\WorkflowManager;
 use Drupal\File\Entity\File;
 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 {
 class CorpusController extends ControllerBase {
@@ -27,8 +29,6 @@ class CorpusController extends ControllerBase {
     // Get a node storage object.
     // Get a node storage object.
     // $file_storage = \Drupal::entityManager()->getStorage('node');
     // $file_storage = \Drupal::entityManager()->getStorage('node');
 
 
-    $response = new JsonResponse();
-
     $config = \Drupal::config('system.site');
     $config = \Drupal::config('system.site');
 
 
     $query = \Drupal::entityQuery('node')
     $query = \Drupal::entityQuery('node')
@@ -76,8 +76,7 @@ class CorpusController extends ControllerBase {
       // if($has_article && $article_value[0]['value'] == "")
       // if($has_article && $article_value[0]['value'] == "")
       //   dpm($article_value);
       //   dpm($article_value);
 
 
-      // TODO: favoris marker
-
+      // favoris marker
       $nodes_data[] = array(
       $nodes_data[] = array(
         "nid" => $node->get('nid')->getString(),
         "nid" => $node->get('nid')->getString(),
         "title" => $node->get('title')->getString(),
         "title" => $node->get('title')->getString(),
@@ -107,7 +106,17 @@ class CorpusController extends ControllerBase {
       'entrees' => $entrees
       '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 $response;
     // return array(
     // return array(