edlp_corpus.module 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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\Core\Link;
  13. use Drupal\workflow\Entity\WorkflowManager;
  14. /**
  15. * Implements hook_theme().
  16. */
  17. function edlp_corpus_theme($existing, $type, $theme, $path) {
  18. // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
  19. return array(
  20. 'blockentrees' => array(
  21. // 'render element' => '',
  22. 'file' => 'blockentrees.inc',
  23. 'variables' => array(
  24. 'entrees_items' => array(),
  25. ),
  26. ),
  27. );
  28. }
  29. /**
  30. * hook_entity_extra_field_info()
  31. */
  32. function edlp_corpus_entity_extra_field_info(){
  33. $extra = [];
  34. $extra['taxonomy_term']['entrees']['display']['index'] = [
  35. 'label' => t('Index'),
  36. 'description' => 'Display index of all documents tagued with the term',
  37. 'weight' => 99,
  38. // 'visible' => FALSE,
  39. ];
  40. $extra['node']['enregistrement']['display']['relations'] = [
  41. 'label' => t('Relations'),
  42. 'description' => 'Display enregistrement relations with other content types',
  43. 'weight' => 99,
  44. ];
  45. return $extra;
  46. }
  47. /**
  48. * Implements hook_ENTITY_TYPE_view().
  49. * @see https://www.amazeelabs.com/en/render-menu-tree-custom-code-drupal-8
  50. */
  51. function edlp_corpus_taxonomy_term_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  52. $index_display_settings = $display->getComponent('index');
  53. if (!empty($index_display_settings)) {
  54. // dpm($entity);
  55. // dpm($entity->id());
  56. $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
  57. $query = \Drupal::entityQuery('node')
  58. ->condition('status', 1)
  59. ->condition('type', 'enregistrement')
  60. ->sort('title')
  61. ->condition('field_entrees', $entity->id());
  62. $documents_nids = $query->execute();
  63. $documents = entity_load_multiple('node', $documents_nids);
  64. $documents_list = array (
  65. '#theme' => 'item_list',
  66. '#items' => [],
  67. );
  68. foreach($documents as $doc){
  69. // remove masqué
  70. $sid = WorkflowManager::getCurrentStateId($doc, 'field_workflow');
  71. if($sid != 'corpus_documents_publie') continue;
  72. // TODO: instead of workflow, just check access
  73. $documents_list['#items'][] = $view_builder->view($doc, 'index');
  74. }
  75. // And the last step is to actually build the tree.
  76. $build['index'] = array(
  77. '#type'=>"container",
  78. '#attributes'=>array(
  79. 'class'=>'index'
  80. ),
  81. 'label'=>array(
  82. '#type' => 'container',
  83. "#markup"=> t("Index"),
  84. '#attributes'=>array(
  85. 'class'=>'field__label'
  86. ),
  87. ),
  88. 'documents'=> $documents_list
  89. );
  90. }
  91. }
  92. function edlp_corpus_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  93. if($entity->bundle() == "enregistrement"){
  94. $relations_display_settings = $display->getComponent('relations');
  95. if(!empty($relations_display_settings)){
  96. // querying all productions page that have this document in their entity reference "field_documents_liés"
  97. $query = \Drupal::entityQuery('node')
  98. ->condition('status', 1)
  99. ->condition('type', 'page') // page (production)
  100. ->exists('field_page_type')
  101. ->condition('field_documents_lies.target_id', $entity->id(), 'IN');
  102. $nids = $query->execute();
  103. $nodes = entity_load_multiple('node', $nids);
  104. // dpm($nodes, '$nodes');
  105. // build the array of relateds classified by page_type (taxonomy)
  106. $relateds = array();
  107. foreach ($nodes as $nid => $node) {
  108. // $page_type = $node->get('field_page_type')->get(0)->getValue();
  109. // dpm($page_type, 'page_type');
  110. // $term = entity_load('taxonomy_term', $page_type['target_id']);
  111. // dpm($term, 'term');
  112. // $relateds[$term->getName()][] = $node->getTitle();
  113. $url = Url::fromRoute('entity.node.canonical', ['node'=>$node->id()]);
  114. $url->setOptions(array(
  115. 'attributes' => array(
  116. 'class' => ['ajax-link'],
  117. 'data-drupal-link-system-path' => $url->getInternalPath()
  118. )
  119. ));
  120. $link = Link::fromTextAndUrl(trim($node->getTitle()), $url);
  121. // $relateds[$term->getName()][] = $link;
  122. $relateds[] = $link->toRenderable();
  123. }
  124. // dpm($relateds, 'relateds');
  125. // if relateds, build the sentence for display and the render array
  126. if(count($relateds)){
  127. // $relations = '<h3>' . t('This sound is about :') . '</h3>';
  128. // foreach ($relateds as $cat => $titles) {
  129. // // $relations .= '<span class="cat">' . $cat . ' :</span> ' . implode(', ', $titles) . ' | ';
  130. // $relations[] =
  131. // }
  132. // $relations = preg_replace('/\s\|\s$/', '', $relations);
  133. // $relations .= '</p>';
  134. $build['relations'] = array(
  135. '#type'=>"container",
  136. '#attributes'=>array(
  137. 'class'=>'relations'
  138. ),
  139. 'title' => array(
  140. "#markup"=> '<h3>' . t('This sound is about :') . '</h3>',
  141. ),
  142. 'content' => array (
  143. '#theme' => 'item_list',
  144. '#items' => $relateds,
  145. ),
  146. );
  147. }
  148. }
  149. }
  150. }
  151. /**
  152. * Implements hook_page_attachments().
  153. * @param array $attachments
  154. */
  155. function edlp_corpus_page_attachments(array &$attachments) {
  156. // css class
  157. // :root{
  158. // --e-col-130:#4B8F7E;
  159. // }
  160. // javascript list
  161. // drupalSettings edlp_corpus{
  162. // "e_col_130": "rgb(75, 143, 126)",
  163. // }
  164. // get all entries
  165. $query = \Drupal::entityQuery('taxonomy_term')
  166. ->condition('vid', 'entrees');
  167. $tids = $query->execute();
  168. $terms = entity_load_multiple('taxonomy_term', $tids);
  169. $js_list = [];
  170. $css_str = ":root{\n";
  171. foreach ($terms as $term) {
  172. $tid = $term->id();
  173. // get the color value
  174. $color = $term->get('field_color')->color;
  175. // build colors list
  176. $css_str .= "\t".'--e-col-'.$tid.':'.$color.";\n";
  177. $js_list['e_col_'.$tid] = $color;
  178. }
  179. $css_str .= '}';
  180. // attache css
  181. // we should find a better way
  182. // https://www.drupal.org/docs/8/creating-custom-modules/adding-stylesheets-css-and-javascript-js-to-a-drupal-8-module
  183. $attachments['#attached']['html_head'][] = [
  184. [
  185. '#type' => 'html_tag',
  186. '#tag' => 'style',
  187. '#value' => $css_str,
  188. ],
  189. // A key, to make it possible to recognize this HTML element when altering.
  190. 'edlp_colors',
  191. ];
  192. $attachments['#attached']['drupalSettings']['edlp_corpus']['colors'] = $js_list;
  193. // load corpus
  194. $url = Url::fromRoute('edlp_corpus.corpusjson');
  195. $attachments['#attached']['drupalSettings']['edlp_corpus']['load_corpus_ajax_url'] = $url->getInternalPath();
  196. }
  197. function _edlp_corpus_invalidate_corpus_cache($node){
  198. if($node->getType() == 'enregistrement'){
  199. $sid = WorkflowManager::getCurrentStateId($node, 'field_workflow');
  200. if($sid == 'corpus_documents_publie'){
  201. \Drupal::service('cache_tags.invalidator')->invalidateTags(['rebuild-corpus-cache']);
  202. }
  203. }
  204. }
  205. /**
  206. * Acts when creating a new entity of a specific type.
  207. * This hook runs after a new entity object has just been instantiated.
  208. * @param \Drupal\Core\Entity\EntityInterface $entity
  209. * The entity object.
  210. * @ingroup entity_crud
  211. * @see hook_entity_create()
  212. */
  213. function edlp_corpus_node_create(EntityInterface $node) {
  214. _edlp_corpus_invalidate_corpus_cache($node);
  215. }
  216. /**
  217. * Respond to updates to an entity of a particular type.
  218. * This hook runs once the entity storage has been updated. Note that hook
  219. * implementations may not alter the stored entity data. Get the original entity
  220. * object from $entity->original.
  221. * @param \Drupal\Core\Entity\EntityInterface $entity
  222. * The entity object.
  223. * @ingroup entity_crud
  224. * @see hook_entity_update()
  225. */
  226. function edlp_corpus_node_update(EntityInterface $node) {
  227. _edlp_corpus_invalidate_corpus_cache($node);
  228. }
  229. /**
  230. * Respond to entity deletion.
  231. * This hook runs once the entity has been deleted from the storage.
  232. * @param \Drupal\Core\Entity\EntityInterface $entity
  233. * The entity object for the entity that has been deleted.
  234. * @ingroup entity_crud
  235. * @see hook_ENTITY_TYPE_delete()
  236. */
  237. function edlp_corpus_node_delete(EntityInterface $node) {
  238. _edlp_corpus_invalidate_corpus_cache($node);
  239. }