edlp_corpus.module 11 KB

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