edlptheme.theme 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * @file
  4. * Functions to support theming in the edlp theme.
  5. */
  6. use Drupal\Core\Url;
  7. use Drupal\Core\Link;
  8. use Drupal\Core\Form\FormStateInterface;
  9. use Drupal\Core\Template\Attribute;
  10. use Drupal\Component\Utility\Unicode;
  11. use Drupal\Core\Render\Element;
  12. /**
  13. * Implements hook_page_attachments().
  14. * @param array $attachments
  15. */
  16. // this does not work with themes
  17. // function edlptheme_page_attachments(array &$attachments) {
  18. // dpm('edlptheme_page_attachments', $attachments);
  19. // }
  20. function edlptheme_preprocess_page(&$vars){
  21. // dsm($vars, 'vars');
  22. }
  23. function edlptheme_preprocess_node(&$vars){
  24. $node = $vars['elements']['#node'];
  25. $options = ['absolute' => TRUE];
  26. $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  27. $system_path = $url->getInternalPath();
  28. $vars['link_attributes'] = new Attribute(array(
  29. 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path
  30. ));
  31. if($node->bundle() == 'enregistrement' && in_array($vars['view_mode'], ['article','transcript'])){
  32. $vars['page'] = true;
  33. $vars['sur_title'] = $vars['view_mode'];
  34. }
  35. }
  36. /**
  37. * Implements hook_form_alter
  38. */
  39. function edlptheme_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  40. // dpm($form_id, 'form_id');
  41. // dpm($form, 'form');
  42. $form['name']['#attributes']['placeholder'] = (string) $form['name']['#title'];
  43. unset($form['name']['#title']);
  44. $form['pass']['#attributes']['placeholder'] = (string) $form['pass']['#title'];
  45. unset($form['pass']['#title']);
  46. }
  47. function edlptheme_preprocess_edlp_home(&$vars){
  48. foreach($vars['nodes'] as &$node){
  49. switch($node['vm']){
  50. case "image_2_columns":
  51. $cols = 4;
  52. break;
  53. case "image_1_columns":
  54. $cols = 2;
  55. break;
  56. case "text_1_column":
  57. $cols = 2;
  58. break;
  59. default:
  60. $cols = 3;
  61. };
  62. $node['cols'] = $cols;
  63. }
  64. }
  65. function edlptheme_preprocess_edlp_productions(&$vars){
  66. foreach($vars['nodes'] as &$node){
  67. switch($node['vm']){
  68. case "image_2_columns":
  69. $cols = 4;
  70. break;
  71. case "image_1_columns":
  72. $cols = 2;
  73. break;
  74. case "text_1_column":
  75. $cols = 2;
  76. break;
  77. };
  78. $node['cols'] = $cols;
  79. }
  80. // dpm($vars);
  81. }
  82. function edlptheme_preprocess_node__enregistrement__index(&$vars){
  83. $node = $vars['elements']['#node'];
  84. $options = ['absolute' => TRUE];
  85. $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  86. $system_path = $url->getInternalPath();
  87. // get the audio file url
  88. $field_son_values = $node->get('field_son')->getValue();
  89. $son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
  90. $son_file = \Drupal\file\Entity\File::load($son_fid);
  91. $son_url = null;
  92. if($son_file){
  93. $son_uri = $son_file->getFileUri();
  94. $son_url = file_create_url($son_uri);
  95. }
  96. $vars['link_attributes'] = new Attribute(array(
  97. 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  98. 'audio_url' => $son_url,
  99. 'nid' => $node->id(),
  100. 'class' => array('audio-link', 'ajax-link')
  101. ));
  102. // dpm($vars['link_attributes']);
  103. }
  104. function edlptheme_preprocess_node__enregistrement__search_index(&$vars){
  105. $node = $vars['elements']['#node'];
  106. $options = ['absolute' => TRUE];
  107. $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  108. $system_path = $url->getInternalPath();
  109. // get the audio file url
  110. $field_son_values = $node->get('field_son')->getValue();
  111. $son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
  112. $son_file = \Drupal\file\Entity\File::load($son_fid);
  113. $son_url = null;
  114. if($son_file){
  115. $son_uri = $son_file->getFileUri();
  116. $son_url = file_create_url($son_uri);
  117. }
  118. $vars['link_attributes'] = new Attribute(array(
  119. 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  120. 'audio_url' => $son_url,
  121. 'nid' => $node->id(),
  122. 'class' => array('audio-link', 'ajax-link')
  123. ));
  124. // dpm($vars['link_attributes']);
  125. }
  126. function edlptheme_preprocess_node__enregistrement__compo(&$vars){
  127. $node = $vars['elements']['#node'];
  128. $options = ['absolute' => TRUE];
  129. $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  130. $system_path = $url->getInternalPath();
  131. // get the audio file url
  132. $field_son_values = $node->get('field_son')->getValue();
  133. $son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
  134. $son_file = \Drupal\file\Entity\File::load($son_fid);
  135. $son_url = null;
  136. if($son_file){
  137. $son_uri = $son_file->getFileUri();
  138. $son_url = file_create_url($son_uri);
  139. }
  140. $vars['link_attributes'] = new Attribute(array(
  141. 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  142. 'audio_url' => $son_url,
  143. 'nid' => $node->id(),
  144. 'class' => array('audio-link', 'ajax-link')
  145. ));
  146. // dpm($vars);
  147. $title = $vars['label'][0]['#context']['value'];
  148. $vars['label'][0]['#context']['value'] = Unicode::truncate($title, 25, true, true);
  149. // dpm($vars['link_attributes']);
  150. }
  151. function edlptheme_preprocess_node__enregistrement__player_cartel(&$vars){
  152. // dpm($vars);
  153. $node = $vars['node'];
  154. $vars['col_left'] = false;
  155. $vars['col_right'] = false;
  156. // if transcript not empty
  157. $transcript = $node->get('field_transcript_vo');
  158. if(!$transcript->isEmpty()){
  159. $vars['col_left'] = true;
  160. $url = Url::fromRoute('entity.node.canonical', ['node'=>$node->id()]);
  161. $url->setOptions(array(
  162. 'attributes' => array(
  163. 'class' => ['link-transcript', 'ajax-link'],
  164. 'viewmode'=>'transcript',
  165. 'data-drupal-link-system-path' => $url->getInternalPath()
  166. )
  167. ));
  168. $vars['link_transcript'] = Link::fromTextAndUrl(t("Lire le text."), $url);
  169. }
  170. // if article not empty
  171. $article = $node->get('body');
  172. if(!$article->isEmpty()){
  173. $vars['col_left'] = true;
  174. $url = Url::fromRoute('entity.node.canonical', ['node'=>$node->id()]);
  175. $url->setOptions(array(
  176. 'attributes' => array(
  177. 'class' => ['link-article', 'ajax-link'],
  178. 'viewmode'=>'article',
  179. 'data-drupal-link-system-path' => $url->getInternalPath()
  180. )
  181. ));
  182. $vars['link_article'] = Link::fromTextAndUrl(t("Lire l'article."), $url);
  183. }
  184. // relations (defined in edlp_corpus.module)
  185. if(isset($vars['content']['relations'])){
  186. // dpm($relations);
  187. $vars['col_right'] = true;
  188. }
  189. if($vars['col_left'] || $vars['col_right']){
  190. $vars['second_cartel'] = true;
  191. }
  192. }
  193. /**
  194. * Prepares variables for image formatter templates.
  195. *
  196. * Default template: image-formatter.html.twig.
  197. *
  198. * @param array $variables
  199. * An associative array containing:
  200. * - item: An ImageItem object.
  201. * - item_attributes: An optional associative array of html attributes to be
  202. * placed in the img tag.
  203. * - image_style: An optional image style.
  204. * - url: An optional \Drupal\Core\Url object.
  205. */
  206. function edlptheme_preprocess_image_formatter(&$vars){
  207. if(isset($vars['url'])){
  208. $system_path = $vars['url']->getInternalPath();
  209. $vars['link_attributes'] = new Attribute(array(
  210. 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  211. 'class' => array('ajax-link')
  212. ));
  213. // dpm($vars);
  214. }
  215. }
  216. function edlptheme_preprocess_links__language_block(&$vars){
  217. // dpm($vars);
  218. foreach ($vars['links'] as $lang_code => $link) {
  219. $vars['links'][$lang_code]['text'] = $lang_code;
  220. $vars['links'][$lang_code]['link']['#title'] = $lang_code;
  221. }
  222. }