edlp_corpus.module 8.9 KB

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