edlptheme.theme 6.9 KB

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