edlptheme.theme 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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__player_cartel(&$vars){
  96. // dpm($vars);
  97. // if transcript not empty
  98. $url = Url::fromRoute('entity.node.canonical', ['node'=>$vars['node']->id()], array(
  99. 'attributes' => array(
  100. 'class' => ['link-transcript', 'ajax-link'],
  101. 'viewmode'=>'transcript'
  102. )
  103. ));
  104. $vars['link_transcript'] = array(
  105. '#title' => t("Lire le text."),
  106. '#type' => 'link',
  107. '#url' => $url,
  108. '#options'=>array(
  109. 'attributes' => array(
  110. 'data-drupal-link-system-path' => $url->getInternalPath()
  111. )
  112. )
  113. );
  114. // TODO: refacorize the link generator as following :
  115. // $url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term'=>$tid]);
  116. // $url->setOptions(array(
  117. // 'attributes' => array(
  118. // 'class' => ['index-link', 'ajax-link'],
  119. // 'viewmode'=>'index',
  120. // 'tid'=>$tid,
  121. // 'data-drupal-link-system-path' => $url->getInternalPath()
  122. // )
  123. // ));
  124. // $entree['index_link'] = Link::fromTextAndUrl('index', $url);
  125. // if article not empty
  126. $url = Url::fromRoute('entity.node.canonical', ['node'=>$vars['node']->id()], array(
  127. 'attributes' => array(
  128. 'class' => ['link-article', 'ajax-link'],
  129. 'viewmode'=>'article'
  130. )
  131. ));
  132. $vars['link_article'] = array(
  133. '#title' => t("Lire l'article."),
  134. '#type' => 'link',
  135. '#url' => $url,
  136. '#options' => array(
  137. 'attributes'=>array(
  138. 'data-drupal-link-system-path' => $url->getInternalPath()
  139. )
  140. )
  141. );
  142. // if article or transcript
  143. $vars['col_left'] = true;
  144. // if
  145. $vars['col_right'] = true;
  146. }
  147. /**
  148. * Prepares variables for image formatter templates.
  149. *
  150. * Default template: image-formatter.html.twig.
  151. *
  152. * @param array $variables
  153. * An associative array containing:
  154. * - item: An ImageItem object.
  155. * - item_attributes: An optional associative array of html attributes to be
  156. * placed in the img tag.
  157. * - image_style: An optional image style.
  158. * - url: An optional \Drupal\Core\Url object.
  159. */
  160. function edlptheme_preprocess_image_formatter(&$vars){
  161. if(isset($vars['url'])){
  162. $system_path = $vars['url']->getInternalPath();
  163. $vars['link_attributes'] = new Attribute(array(
  164. 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  165. 'class' => array('ajax-link')
  166. ));
  167. // dpm($vars);
  168. }
  169. }