edlptheme.theme 7.2 KB

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