edlptheme.theme 7.2 KB

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