edlptheme.theme 8.6 KB

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