edlp_corpus.module 9.8 KB

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