edlp_corpus.module 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. # @Author: Bachir Soussi Chiadmi <bach>
  3. # @Date: 13-12-2017
  4. # @Email: bachir@figureslibres.io
  5. # @Filename: edlp_corpus.routing.yml
  6. # @Last modified by: bach
  7. # @Last modified time: 20-12-2017
  8. # @License: GPL-V3
  9. use Drupal\Core\Entity\EntityInterface;
  10. use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
  11. use Drupal\Core\Url;
  12. use Drupal\workflow\Entity\WorkflowManager;
  13. /**
  14. * Implements hook_theme().
  15. */
  16. function edlp_corpus_theme($existing, $type, $theme, $path) {
  17. // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
  18. return array(
  19. 'blockentrees' => array(
  20. // 'render element' => '',
  21. 'file' => 'blockentrees.inc',
  22. 'variables' => array(
  23. 'entrees_items' => array(),
  24. ),
  25. ),
  26. );
  27. }
  28. /**
  29. * hook_entity_extra_field_info()
  30. */
  31. function edlp_corpus_entity_extra_field_info(){
  32. $extra = [];
  33. $extra['taxonomy_term']['entrees']['display']['index'] = [
  34. 'label' => t('Index'),
  35. 'description' => 'Display index of all documents tagued with the term',
  36. 'weight' => 99,
  37. // 'visible' => FALSE,
  38. ];
  39. return $extra;
  40. }
  41. /**
  42. * Implements hook_ENTITY_TYPE_view().
  43. * @see https://www.amazeelabs.com/en/render-menu-tree-custom-code-drupal-8
  44. */
  45. function edlp_corpus_taxonomy_term_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  46. $index_display_settings = $display->getComponent('index');
  47. if (!empty($index_display_settings)) {
  48. // dpm($entity);
  49. // dpm($entity->id());
  50. $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
  51. $query = \Drupal::entityQuery('node')
  52. ->condition('status', 1)
  53. ->condition('type', 'enregistrement')
  54. ->condition('field_entrees', $entity->id());
  55. $documents_nids = $query->execute();
  56. $documents = entity_load_multiple('node', $documents_nids);
  57. $documents_list = array (
  58. '#theme' => 'item_list',
  59. '#items' => [],
  60. );
  61. foreach($documents as $doc){
  62. // remove masqué
  63. $sid = WorkflowManager::getCurrentStateId($doc, 'field_workflow');
  64. if($sid != 'corpus_documents_publie') continue;
  65. // TODO: instead of workflow, just check access
  66. $documents_list['#items'][] = $view_builder->view($doc, 'index');
  67. }
  68. // And the last step is to actually build the tree.
  69. $build['index'] = array(
  70. '#type'=>"container",
  71. '#attributes'=>array(
  72. 'class'=>'index'
  73. ),
  74. 'label'=>array(
  75. '#type' => 'container',
  76. "#markup"=> t("Index"),
  77. '#attributes'=>array(
  78. 'class'=>'field__label'
  79. ),
  80. ),
  81. 'documents'=> $documents_list
  82. );
  83. }
  84. }
  85. /**
  86. * Implements hook_page_attachments().
  87. * @param array $attachments
  88. */
  89. function edlp_corpus_page_attachments(array &$attachments) {
  90. // css class
  91. // :root{
  92. // --e-col-130:#4B8F7E;
  93. // }
  94. // javascript list
  95. // drupalSettings edlp_corpus{
  96. // "e_col_130": "rgb(75, 143, 126)",
  97. // }
  98. // get all entries
  99. $query = \Drupal::entityQuery('taxonomy_term')
  100. ->condition('vid', 'entrees');
  101. $tids = $query->execute();
  102. $terms = entity_load_multiple('taxonomy_term', $tids);
  103. $js_list = [];
  104. $css_str = ":root{\n";
  105. foreach ($terms as $term) {
  106. $tid = $term->id();
  107. // get the color value
  108. $color = $term->get('field_color')->color;
  109. // build colors list
  110. $css_str .= "\t".'--e-col-'.$tid.':'.$color.";\n";
  111. $js_list['e_col_'.$tid] = $color;
  112. }
  113. $css_str .= '}';
  114. // attache css
  115. // we should find a better way
  116. // https://www.drupal.org/docs/8/creating-custom-modules/adding-stylesheets-css-and-javascript-js-to-a-drupal-8-module
  117. $attachments['#attached']['html_head'][] = [
  118. [
  119. '#type' => 'html_tag',
  120. '#tag' => 'style',
  121. '#value' => $css_str,
  122. ],
  123. // A key, to make it possible to recognize this HTML element when altering.
  124. 'edlp_colors',
  125. ];
  126. $attachments['#attached']['drupalSettings']['edlp_corpus']['colors'] = $js_list;
  127. // load corpus
  128. $url = Url::fromRoute('edlp_corpus.corpusjson');
  129. $attachments['#attached']['drupalSettings']['edlp_corpus']['load_corpus_ajax_url'] = $url->getInternalPath();
  130. }
  131. function _edlp_corpus_invalidate_corpus_cache($node){
  132. if($node->getType() == 'enregistrement'){
  133. $sid = WorkflowManager::getCurrentStateId($node, 'field_workflow');
  134. if($sid == 'corpus_documents_publie'){
  135. \Drupal::service('cache_tags.invalidator')->invalidateTags(['rebuild-corpus-cache']);
  136. }
  137. }
  138. }
  139. /**
  140. * Acts when creating a new entity of a specific type.
  141. * This hook runs after a new entity object has just been instantiated.
  142. * @param \Drupal\Core\Entity\EntityInterface $entity
  143. * The entity object.
  144. * @ingroup entity_crud
  145. * @see hook_entity_create()
  146. */
  147. function edlp_corpus_node_create(EntityInterface $node) {
  148. _edlp_corpus_invalidate_corpus_cache($node);
  149. }
  150. /**
  151. * Respond to updates to an entity of a particular type.
  152. * This hook runs once the entity storage has been updated. Note that hook
  153. * implementations may not alter the stored entity data. Get the original entity
  154. * object from $entity->original.
  155. * @param \Drupal\Core\Entity\EntityInterface $entity
  156. * The entity object.
  157. * @ingroup entity_crud
  158. * @see hook_entity_update()
  159. */
  160. function edlp_corpus_node_update(EntityInterface $node) {
  161. _edlp_corpus_invalidate_corpus_cache($node);
  162. }
  163. /**
  164. * Respond to entity deletion.
  165. * This hook runs once the entity has been deleted from the storage.
  166. * @param \Drupal\Core\Entity\EntityInterface $entity
  167. * The entity object for the entity that has been deleted.
  168. * @ingroup entity_crud
  169. * @see hook_ENTITY_TYPE_delete()
  170. */
  171. function edlp_corpus_node_delete(EntityInterface $node) {
  172. _edlp_corpus_invalidate_corpus_cache($node);
  173. }