edlptheme.theme 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. // $head_title = $vars['head_title'];
  31. // dpm($vars);
  32. $site_config = \Drupal::config('system.site');
  33. // dpm($site_config->get('slogan'));
  34. // array_push($head_title, [
  35. // 'name' => $site_config->get('name'),
  36. // ]);
  37. // $vars['head_title'] = $head_title;
  38. $description = [
  39. '#tag' => 'meta',
  40. '#attributes' => [
  41. 'name' => 'description',
  42. 'content' => $site_config->get('slogan'),
  43. ],
  44. ];
  45. $vars['page']['#attached']['html_head'][] = [$description, 'description'];
  46. $gv = [
  47. '#tag' => 'meta',
  48. '#attributes' => [
  49. 'name' => 'google-site-verification',
  50. 'content' => "Y6PSbMfj67bXtMRAT-mFTAxrIeZPzC5jWSpH3M7yhkk",
  51. ],
  52. ];
  53. $vars['page']['#attached']['html_head'][] = [$gv, "google-site-verification"];
  54. }
  55. function edlptheme_preprocess_page(&$vars){
  56. // dsm($vars, 'vars');
  57. }
  58. function edlptheme_preprocess_node(&$vars){
  59. $node = $vars['elements']['#node'];
  60. $options = ['absolute' => TRUE];
  61. $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  62. $system_path = $url->getInternalPath();
  63. $vars['link_attributes'] = new Attribute(array(
  64. 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path
  65. ));
  66. if($node->bundle() == 'enregistrement' && in_array($vars['view_mode'], ['article','transcript'])){
  67. $vars['page'] = true;
  68. $vars['sur_title'] = $vars['view_mode'];
  69. }
  70. }
  71. /**
  72. * Implements hook_form_alter
  73. */
  74. function edlptheme_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  75. // dpm($form_id, 'form_id');
  76. // dpm($form, 'form');
  77. $form['name']['#attributes']['placeholder'] = (string) $form['name']['#title'];
  78. unset($form['name']['#title']);
  79. $form['pass']['#attributes']['placeholder'] = (string) $form['pass']['#title'];
  80. unset($form['pass']['#title']);
  81. }
  82. /**
  83. * Implements hook_theme_suggestions_HOOK_alter().
  84. */
  85. function edlptheme_theme_suggestions_taxonomy_term_alter(&$suggestions, $variables) {
  86. $elements = $variables['elements'];
  87. if (isset($elements['#taxonomy_term']) && isset($elements['#theme']) && isset($elements['#view_mode'])) {
  88. $term = $elements['#taxonomy_term'];
  89. if (is_object($term)) {
  90. $suggestions[] = $elements['#theme'] . '__' . $term->getVocabularyId() . '__' . $elements['#view_mode'];
  91. $suggestions[] = $elements['#theme'] . '__' . $term->id() . '__' . $elements['#view_mode'];
  92. }
  93. }
  94. }
  95. function edlptheme_preprocess_edlp_home(&$vars){
  96. foreach($vars['nodes'] as &$node){
  97. switch($node['vm']){
  98. case "image_2_columns":
  99. $cols = 4;
  100. break;
  101. case "image_1_columns":
  102. $cols = 2;
  103. break;
  104. case "text_1_column":
  105. $cols = 2;
  106. break;
  107. default:
  108. $cols = 3;
  109. };
  110. $node['cols'] = $cols;
  111. }
  112. }
  113. function edlptheme_preprocess_taxonomy_term__entrees__mobile_home(&$vars){
  114. // dpm($vars, 'vars');
  115. }
  116. function edlptheme_preprocess_edlp_productions(&$vars){
  117. foreach($vars['nodes'] as &$node){
  118. switch($node['vm']){
  119. case "image_2_columns":
  120. $cols = 4;
  121. break;
  122. case "image_1_columns":
  123. $cols = 2;
  124. break;
  125. case "text_1_column":
  126. $cols = 2;
  127. break;
  128. };
  129. $node['cols'] = $cols;
  130. }
  131. // dpm($vars);
  132. }
  133. function edlptheme_preprocess_node__enregistrement__index(&$vars){
  134. // $node = $vars['elements']['#node'];
  135. // $options = ['absolute' => TRUE];
  136. // $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  137. // $system_path = $url->getInternalPath();
  138. // // get the audio file url
  139. // $field_son_values = $node->get('field_son')->getValue();
  140. // $son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
  141. // $son_file = \Drupal\file\Entity\File::load($son_fid);
  142. // $son_url = null;
  143. // if($son_file){
  144. // $son_uri = $son_file->getFileUri();
  145. // $son_url = file_create_url($son_uri);
  146. // }
  147. //
  148. // $vars['link_attributes'] = new Attribute(array(
  149. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  150. // 'audio_url' => $son_url,
  151. // 'nid' => $node->id(),
  152. // 'class' => array('audio-link', 'ajax-link')
  153. // ));
  154. edlptheme_prepare_audio_link($vars);
  155. // dpm($vars['link_attributes']);
  156. }
  157. // forhome mobile index
  158. function edlptheme_preprocess_node__enregistrement__index_home(&$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. //
  173. // $vars['link_attributes'] = new Attribute(array(
  174. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  175. // 'audio_url' => $son_url,
  176. // 'nid' => $node->id(),
  177. // 'class' => array('audio-link', 'ajax-link')
  178. // ));
  179. edlptheme_prepare_audio_link($vars);
  180. // dpm($vars['link_attributes']);
  181. }
  182. function edlptheme_preprocess_node__enregistrement__search_index(&$vars){
  183. // $node = $vars['elements']['#node'];
  184. // $options = ['absolute' => TRUE];
  185. // $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  186. // $system_path = $url->getInternalPath();
  187. // // get the audio file url
  188. // $field_son_values = $node->get('field_son')->getValue();
  189. // $son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
  190. // $son_file = \Drupal\file\Entity\File::load($son_fid);
  191. // $son_url = null;
  192. // if($son_file){
  193. // $son_uri = $son_file->getFileUri();
  194. // $son_url = file_create_url($son_uri);
  195. // }
  196. //
  197. // $vars['link_attributes'] = new Attribute(array(
  198. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  199. // 'audio_url' => $son_url,
  200. // 'nid' => $node->id(),
  201. // 'class' => array('audio-link', 'ajax-link')
  202. // ));
  203. edlptheme_prepare_audio_link($vars);
  204. // dpm($vars['link_attributes']);
  205. }
  206. function edlptheme_preprocess_node__enregistrement__lastdocs(&$vars){
  207. // $node = $vars['elements']['#node'];
  208. // $options = ['absolute' => TRUE];
  209. // $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  210. // $system_path = $url->getInternalPath();
  211. // // get the audio file url
  212. // $field_son_values = $node->get('field_son')->getValue();
  213. // $son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
  214. // $son_file = \Drupal\file\Entity\File::load($son_fid);
  215. // $son_url = null;
  216. // if($son_file){
  217. // $son_uri = $son_file->getFileUri();
  218. // $son_url = file_create_url($son_uri);
  219. // }
  220. //
  221. // $vars['link_attributes'] = new Attribute(array(
  222. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  223. // 'audio_url' => $son_url,
  224. // 'nid' => $node->id(),
  225. // 'class' => array('audio-link', 'ajax-link')
  226. // ));
  227. edlptheme_prepare_audio_link($vars);
  228. // dpm($vars['link_attributes']);
  229. }
  230. function edlptheme_preprocess_node__enregistrement__compo(&$vars){
  231. // $node = $vars['elements']['#node'];
  232. // $options = ['absolute' => TRUE];
  233. // $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  234. // $system_path = $url->getInternalPath();
  235. // // get the audio file url
  236. // $field_son_values = $node->get('field_son')->getValue();
  237. // $son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
  238. // $son_file = \Drupal\file\Entity\File::load($son_fid);
  239. // $son_url = null;
  240. // if($son_file){
  241. // $son_uri = $son_file->getFileUri();
  242. // $son_url = file_create_url($son_uri);
  243. // }
  244. //
  245. // $vars['link_attributes'] = new Attribute(array(
  246. // 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  247. // 'audio_url' => $son_url,
  248. // 'nid' => $node->id(),
  249. // 'class' => array('audio-link', 'ajax-link')
  250. // ));
  251. edlptheme_prepare_audio_link($vars);
  252. // dpm($vars);
  253. $title = $vars['label'][0]['#context']['value'];
  254. $vars['label'][0]['#context']['value'] = Unicode::truncate($title, 25, true, true);
  255. // dpm($vars['link_attributes']);
  256. }
  257. function edlptheme_prepare_audio_link(&$vars){
  258. $node = $vars['elements']['#node'];
  259. $options = ['absolute' => TRUE];
  260. $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  261. $system_path = $url->getInternalPath();
  262. // get the audio file url
  263. $field_son_values = $node->get('field_son')->getValue();
  264. $son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
  265. $son_file = \Drupal\file\Entity\File::load($son_fid);
  266. $son_url = null;
  267. if($son_file){
  268. $son_uri = $son_file->getFileUri();
  269. $son_url = file_create_url($son_uri);
  270. }
  271. $vars['link_attributes'] = new Attribute(array(
  272. 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  273. 'audio_url' => $son_url,
  274. 'nid' => $node->id(),
  275. 'class' => array('audio-link', 'ajax-link')
  276. ));
  277. }
  278. function edlptheme_preprocess_node__enregistrement__player_cartel(&$vars){
  279. // dpm($vars);
  280. $node = $vars['node'];
  281. $vars['col_left'] = false;
  282. $vars['col_right'] = false;
  283. // if transcript not empty
  284. $transcript = $node->get('field_transcript_vo');
  285. if(!$transcript->isEmpty()){
  286. $vars['col_left'] = true;
  287. $url = Url::fromRoute('entity.node.canonical', ['node'=>$node->id()]);
  288. $url->setOptions(array(
  289. 'attributes' => array(
  290. 'class' => ['link-transcript', 'ajax-link'],
  291. 'viewmode'=>'transcript',
  292. 'data-drupal-link-system-path' => $url->getInternalPath()
  293. )
  294. ));
  295. $vars['link_transcript'] = Link::fromTextAndUrl(t("Read the text."), $url);
  296. }
  297. // if article not empty
  298. $article = $node->get('body');
  299. if(!$article->isEmpty()){
  300. $vars['col_left'] = true;
  301. $url = Url::fromRoute('entity.node.canonical', ['node'=>$node->id()]);
  302. $url->setOptions(array(
  303. 'attributes' => array(
  304. 'class' => ['link-article', 'ajax-link'],
  305. 'viewmode'=>'article',
  306. 'data-drupal-link-system-path' => $url->getInternalPath()
  307. )
  308. ));
  309. $vars['link_article'] = Link::fromTextAndUrl(t("Read the article."), $url);
  310. }
  311. // relations (defined in edlp_corpus.module)
  312. if(isset($vars['content']['relations'])){
  313. // dpm($relations);
  314. $vars['col_right'] = true;
  315. }
  316. if($vars['col_left'] || $vars['col_right']){
  317. $vars['second_cartel'] = true;
  318. }
  319. }
  320. /**
  321. * Prepares variables for image formatter templates.
  322. *
  323. * Default template: image-formatter.html.twig.
  324. *
  325. * @param array $variables
  326. * An associative array containing:
  327. * - item: An ImageItem object.
  328. * - item_attributes: An optional associative array of html attributes to be
  329. * placed in the img tag.
  330. * - image_style: An optional image style.
  331. * - url: An optional \Drupal\Core\Url object.
  332. */
  333. function edlptheme_preprocess_image_formatter(&$vars){
  334. if(isset($vars['url'])){
  335. $system_path = $vars['url']->getInternalPath();
  336. $vars['link_attributes'] = new Attribute(array(
  337. 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  338. 'class' => array('ajax-link')
  339. ));
  340. // dpm($vars);
  341. }
  342. }
  343. function edlptheme_preprocess_links__language_block(&$vars){
  344. // dpm($vars);
  345. foreach ($vars['links'] as $lang_code => $link) {
  346. $vars['links'][$lang_code]['text'] = $lang_code;
  347. $vars['links'][$lang_code]['link']['#title'] = $lang_code;
  348. }
  349. }
  350. function edlptheme_preprocess_file_link(&$vars){
  351. }