edlptheme.theme 7.9 KB

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