edlp_corpus.module 9.6 KB

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