edlptheme.theme 6.6 KB

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