edlptheme.theme 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. //
  129. // $vars['link_attributes'] = new Attribute(array(
  130. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  131. // 'audio_url' => $son_url,
  132. // 'nid' => $node->id(),
  133. // 'class' => array('audio-link', 'ajax-link')
  134. // ));
  135. edlptheme_prepare_audio_link($vars);
  136. // dpm($vars['link_attributes']);
  137. }
  138. // forhome mobile index
  139. function edlptheme_preprocess_node__enregistrement__index_home(&$vars){
  140. // $node = $vars['elements']['#node'];
  141. // $options = ['absolute' => TRUE];
  142. // $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  143. // $system_path = $url->getInternalPath();
  144. // // get the audio file url
  145. // $field_son_values = $node->get('field_son')->getValue();
  146. // $son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
  147. // $son_file = \Drupal\file\Entity\File::load($son_fid);
  148. // $son_url = null;
  149. // if($son_file){
  150. // $son_uri = $son_file->getFileUri();
  151. // $son_url = file_create_url($son_uri);
  152. // }
  153. //
  154. // $vars['link_attributes'] = new Attribute(array(
  155. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  156. // 'audio_url' => $son_url,
  157. // 'nid' => $node->id(),
  158. // 'class' => array('audio-link', 'ajax-link')
  159. // ));
  160. edlptheme_prepare_audio_link($vars);
  161. // dpm($vars['link_attributes']);
  162. }
  163. function edlptheme_preprocess_node__enregistrement__search_index(&$vars){
  164. // $node = $vars['elements']['#node'];
  165. // $options = ['absolute' => TRUE];
  166. // $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  167. // $system_path = $url->getInternalPath();
  168. // // get the audio file url
  169. // $field_son_values = $node->get('field_son')->getValue();
  170. // $son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
  171. // $son_file = \Drupal\file\Entity\File::load($son_fid);
  172. // $son_url = null;
  173. // if($son_file){
  174. // $son_uri = $son_file->getFileUri();
  175. // $son_url = file_create_url($son_uri);
  176. // }
  177. //
  178. // $vars['link_attributes'] = new Attribute(array(
  179. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  180. // 'audio_url' => $son_url,
  181. // 'nid' => $node->id(),
  182. // 'class' => array('audio-link', 'ajax-link')
  183. // ));
  184. edlptheme_prepare_audio_link($vars);
  185. // dpm($vars['link_attributes']);
  186. }
  187. function edlptheme_preprocess_node__enregistrement__lastdocs(&$vars){
  188. // $node = $vars['elements']['#node'];
  189. // $options = ['absolute' => TRUE];
  190. // $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  191. // $system_path = $url->getInternalPath();
  192. // // get the audio file url
  193. // $field_son_values = $node->get('field_son')->getValue();
  194. // $son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
  195. // $son_file = \Drupal\file\Entity\File::load($son_fid);
  196. // $son_url = null;
  197. // if($son_file){
  198. // $son_uri = $son_file->getFileUri();
  199. // $son_url = file_create_url($son_uri);
  200. // }
  201. //
  202. // $vars['link_attributes'] = new Attribute(array(
  203. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  204. // 'audio_url' => $son_url,
  205. // 'nid' => $node->id(),
  206. // 'class' => array('audio-link', 'ajax-link')
  207. // ));
  208. edlptheme_prepare_audio_link($vars);
  209. // dpm($vars['link_attributes']);
  210. }
  211. function edlptheme_preprocess_node__enregistrement__compo(&$vars){
  212. // $node = $vars['elements']['#node'];
  213. // $options = ['absolute' => TRUE];
  214. // $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  215. // $system_path = $url->getInternalPath();
  216. // // get the audio file url
  217. // $field_son_values = $node->get('field_son')->getValue();
  218. // $son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
  219. // $son_file = \Drupal\file\Entity\File::load($son_fid);
  220. // $son_url = null;
  221. // if($son_file){
  222. // $son_uri = $son_file->getFileUri();
  223. // $son_url = file_create_url($son_uri);
  224. // }
  225. //
  226. // $vars['link_attributes'] = new Attribute(array(
  227. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  228. // 'audio_url' => $son_url,
  229. // 'nid' => $node->id(),
  230. // 'class' => array('audio-link', 'ajax-link')
  231. // ));
  232. edlptheme_prepare_audio_link($vars);
  233. // dpm($vars);
  234. $title = $vars['label'][0]['#context']['value'];
  235. $vars['label'][0]['#context']['value'] = Unicode::truncate($title, 25, true, true);
  236. // dpm($vars['link_attributes']);
  237. }
  238. function edlptheme_prepare_audio_link(&$vars){
  239. $node = $vars['elements']['#node'];
  240. $options = ['absolute' => TRUE];
  241. $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  242. $system_path = $url->getInternalPath();
  243. // get the audio file url
  244. $field_son_values = $node->get('field_son')->getValue();
  245. $son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
  246. $son_file = \Drupal\file\Entity\File::load($son_fid);
  247. $son_url = null;
  248. if($son_file){
  249. $son_uri = $son_file->getFileUri();
  250. $son_url = file_create_url($son_uri);
  251. }
  252. $vars['link_attributes'] = new Attribute(array(
  253. 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  254. 'audio_url' => $son_url,
  255. 'nid' => $node->id(),
  256. 'class' => array('audio-link', 'ajax-link')
  257. ));
  258. }
  259. function edlptheme_preprocess_node__enregistrement__player_cartel(&$vars){
  260. // dpm($vars);
  261. $node = $vars['node'];
  262. $vars['col_left'] = false;
  263. $vars['col_right'] = false;
  264. // if transcript not empty
  265. $transcript = $node->get('field_transcript_vo');
  266. if(!$transcript->isEmpty()){
  267. $vars['col_left'] = true;
  268. $url = Url::fromRoute('entity.node.canonical', ['node'=>$node->id()]);
  269. $url->setOptions(array(
  270. 'attributes' => array(
  271. 'class' => ['link-transcript', 'ajax-link'],
  272. 'viewmode'=>'transcript',
  273. 'data-drupal-link-system-path' => $url->getInternalPath()
  274. )
  275. ));
  276. $vars['link_transcript'] = Link::fromTextAndUrl(t("Read the text."), $url);
  277. }
  278. // if article not empty
  279. $article = $node->get('body');
  280. if(!$article->isEmpty()){
  281. $vars['col_left'] = true;
  282. $url = Url::fromRoute('entity.node.canonical', ['node'=>$node->id()]);
  283. $url->setOptions(array(
  284. 'attributes' => array(
  285. 'class' => ['link-article', 'ajax-link'],
  286. 'viewmode'=>'article',
  287. 'data-drupal-link-system-path' => $url->getInternalPath()
  288. )
  289. ));
  290. $vars['link_article'] = Link::fromTextAndUrl(t("Read the article."), $url);
  291. }
  292. // relations (defined in edlp_corpus.module)
  293. if(isset($vars['content']['relations'])){
  294. // dpm($relations);
  295. $vars['col_right'] = true;
  296. }
  297. if($vars['col_left'] || $vars['col_right']){
  298. $vars['second_cartel'] = true;
  299. }
  300. }
  301. /**
  302. * Prepares variables for image formatter templates.
  303. *
  304. * Default template: image-formatter.html.twig.
  305. *
  306. * @param array $variables
  307. * An associative array containing:
  308. * - item: An ImageItem object.
  309. * - item_attributes: An optional associative array of html attributes to be
  310. * placed in the img tag.
  311. * - image_style: An optional image style.
  312. * - url: An optional \Drupal\Core\Url object.
  313. */
  314. function edlptheme_preprocess_image_formatter(&$vars){
  315. if(isset($vars['url'])){
  316. $system_path = $vars['url']->getInternalPath();
  317. $vars['link_attributes'] = new Attribute(array(
  318. 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  319. 'class' => array('ajax-link')
  320. ));
  321. // dpm($vars);
  322. }
  323. }
  324. function edlptheme_preprocess_links__language_block(&$vars){
  325. // dpm($vars);
  326. foreach ($vars['links'] as $lang_code => $link) {
  327. $vars['links'][$lang_code]['text'] = $lang_code;
  328. $vars['links'][$lang_code]['link']['#title'] = $lang_code;
  329. }
  330. }
  331. function edlptheme_preprocess_file_link(&$vars){
  332. }