edlp_corpus.module 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /**
  10. * Implements hook_theme().
  11. */
  12. function edlp_corpus_theme($existing, $type, $theme, $path) {
  13. // @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
  14. return array(
  15. 'blockentrees' => array(
  16. // 'render element' => '',
  17. 'file' => 'blockentrees.inc',
  18. 'variables' => array(
  19. 'entrees_items' => array(),
  20. ),
  21. ),
  22. );
  23. }
  24. /**
  25. * hook_entity_extra_field_info()
  26. *
  27. */
  28. function edlp_corpus_entity_extra_field_info(){
  29. $extra = [];
  30. $extra['taxonomy_term']['entrees']['display']['index'] = [
  31. 'label' => t('Index'),
  32. 'description' => 'Display index of all documents tagued with the term',
  33. 'weight' => 99,
  34. // 'visible' => FALSE,
  35. ];
  36. return $extra;
  37. }
  38. /**
  39. * Implements hook_ENTITY_TYPE_view().
  40. * @see https://www.amazeelabs.com/en/render-menu-tree-custom-code-drupal-8
  41. */
  42. function edlp_corpus_taxonomy_term_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  43. $index_display_settings = $display->getComponent('index');
  44. if (!empty($index_display_settings)) {
  45. // dpm($entity);
  46. // dpm($entity->id());
  47. $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
  48. $query = \Drupal::entityQuery('node')
  49. ->condition('status', 1)
  50. ->condition('type', 'enregistrement')
  51. ->condition('field_entrees', $entity->id());
  52. $documents_nids = $query->execute();
  53. $documents = entity_load_multiple('node', $documents_nids);
  54. $documents_list = array (
  55. '#theme' => 'item_list',
  56. '#items' => [],
  57. );
  58. foreach($documents as $doc){
  59. $documents_list['#items'][] = $view_builder->view($doc, 'index');
  60. }
  61. // And the last step is to actually build the tree.
  62. $build['index'] = array(
  63. '#type'=>"container",
  64. '#attributes'=>array(
  65. 'class'=>'index'
  66. ),
  67. 'label'=>array(
  68. '#type' => 'container',
  69. "#markup"=> t("Index"),
  70. '#attributes'=>array(
  71. 'class'=>'field__label'
  72. ),
  73. ),
  74. 'documents'=> $documents_list
  75. );
  76. }
  77. }
  78. /**
  79. * Implements hook_page_attachments().
  80. * @param array $attachments
  81. */
  82. function edlp_corpus_page_attachments(array &$attachments) {
  83. // css class
  84. // :root{
  85. // --e-col-130:#4B8F7E;
  86. // }
  87. // javascript list
  88. // drupalSettings edlp_corpus{
  89. // "e_col_130": "rgb(75, 143, 126)",
  90. // }
  91. // get all entries
  92. $query = \Drupal::entityQuery('taxonomy_term')
  93. ->condition('vid', 'entrees');
  94. $tids = $query->execute();
  95. $terms = entity_load_multiple('taxonomy_term', $tids);
  96. $js_list = [];
  97. $css_str = ":root{\n";
  98. foreach ($terms as $term) {
  99. $tid = $term->id();
  100. // get the color value
  101. $color = $term->get('field_color')->color;
  102. // build colors list
  103. $css_str .= "\t".'--e-col-'.$tid.':'.$color.";\n";
  104. $js_list['e_col_'.$tid] = $color;
  105. }
  106. $css_str .= '}';
  107. // attache css
  108. // we should find a better way
  109. // https://www.drupal.org/docs/8/creating-custom-modules/adding-stylesheets-css-and-javascript-js-to-a-drupal-8-module
  110. $attachments['#attached']['html_head'][] = [
  111. [
  112. '#type' => 'html_tag',
  113. '#tag' => 'style',
  114. '#value' => $css_str,
  115. ],
  116. // A key, to make it possible to recognize this HTML element when altering.
  117. 'edlp_colors',
  118. ];
  119. $attachments['#attached']['drupalSettings']['edlp_corpus']['colors'] = $js_list;
  120. }