edlptheme.theme 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. // dpm($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. // render the promoted_statics and promoted_prods
  97. $promoteds = ['statics', 'prods'];
  98. // dpm($vars);
  99. foreach ($promoteds as $index => $p) {
  100. foreach($vars['nodes_' . $p] as &$node){
  101. switch($node['vm']){
  102. case "image_2_columns":
  103. $cols = 4;
  104. break;
  105. case "image_1_columns":
  106. $cols = 2;
  107. break;
  108. case "text_1_column":
  109. $cols = 2;
  110. break;
  111. default:
  112. $cols = 3;
  113. };
  114. $node['cols'] = $cols;
  115. }
  116. }
  117. }
  118. function edlptheme_preprocess_taxonomy_term__entrees__mobile_home(&$vars){
  119. // dpm($vars, 'vars');
  120. }
  121. function edlptheme_preprocess_edlp_productions(&$vars){
  122. foreach($vars['nodes'] as &$node){
  123. switch($node['vm']){
  124. case "image_2_columns":
  125. $cols = 4;
  126. break;
  127. case "image_1_columns":
  128. $cols = 2;
  129. break;
  130. case "text_1_column":
  131. $cols = 2;
  132. break;
  133. };
  134. $node['cols'] = $cols;
  135. }
  136. // dpm($vars);
  137. }
  138. function edlptheme_preprocess_node__enregistrement__index(&$vars){
  139. edlptheme_prepare_audio_link($vars);
  140. }
  141. // forhome mobile index
  142. function edlptheme_preprocess_node__enregistrement__index_home(&$vars){
  143. edlptheme_prepare_audio_link($vars);
  144. }
  145. function edlptheme_preprocess_node__enregistrement__search_index(&$vars){
  146. edlptheme_prepare_audio_link($vars);
  147. }
  148. function edlptheme_preprocess_node__enregistrement__lastdocs(&$vars){
  149. edlptheme_prepare_audio_link($vars);
  150. }
  151. function edlptheme_preprocess_node__enregistrement__docsindex(&$vars){
  152. edlptheme_prepare_audio_link($vars);
  153. }
  154. function edlptheme_preprocess_node__enregistrement__compo(&$vars){
  155. edlptheme_prepare_audio_link($vars);
  156. // dpm($vars);
  157. $title = $vars['label'][0]['#context']['value'];
  158. $vars['label'][0]['#context']['value'] = Unicode::truncate($title, 25, true, true);
  159. // dpm($vars['link_attributes']);
  160. }
  161. function edlptheme_prepare_audio_link(&$vars){
  162. $node = $vars['elements']['#node'];
  163. $options = ['absolute' => TRUE];
  164. $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  165. $system_path = $url->getInternalPath();
  166. // get the audio file url
  167. $field_son_values = $node->get('field_son')->getValue();
  168. $son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
  169. $son_file = \Drupal\file\Entity\File::load($son_fid);
  170. $son_url = null;
  171. if($son_file){
  172. $son_uri = $son_file->getFileUri();
  173. $son_url = file_create_url($son_uri);
  174. }
  175. $vars['link_attributes'] = new Attribute(array(
  176. 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  177. 'audio_url' => $son_url,
  178. 'nid' => $node->id(),
  179. 'class' => array('audio-link', 'ajax-link')
  180. ));
  181. }
  182. function edlptheme_preprocess_node__enregistrement__player_cartel(&$vars){
  183. // dpm($vars);
  184. $node = $vars['node'];
  185. $vars['col_left'] = false;
  186. $vars['col_right'] = false;
  187. // if transcript not empty
  188. $transcript = $node->get('field_transcript_vo');
  189. if(!$transcript->isEmpty()){
  190. $vars['col_left'] = true;
  191. $url = Url::fromRoute('entity.node.canonical', ['node'=>$node->id()]);
  192. $url->setOptions(array(
  193. 'attributes' => array(
  194. 'class' => ['link-transcript', 'ajax-link'],
  195. 'viewmode'=>'transcript',
  196. 'data-drupal-link-system-path' => $url->getInternalPath()
  197. )
  198. ));
  199. $vars['link_transcript'] = Link::fromTextAndUrl(t("Read the text"), $url);
  200. }
  201. // if article not empty
  202. $article = $node->get('body');
  203. if(!$article->isEmpty()){
  204. $vars['col_left'] = true;
  205. $url = Url::fromRoute('entity.node.canonical', ['node'=>$node->id()]);
  206. $url->setOptions(array(
  207. 'attributes' => array(
  208. 'class' => ['link-article', 'ajax-link'],
  209. 'viewmode'=>'article',
  210. 'data-drupal-link-system-path' => $url->getInternalPath()
  211. )
  212. ));
  213. $vars['link_article'] = Link::fromTextAndUrl(t("Read the article"), $url);
  214. }
  215. // relations (defined in edlp_corpus.module)
  216. if(isset($vars['content']['relations'])){
  217. // dpm($relations);
  218. $vars['col_right'] = true;
  219. }
  220. if($vars['col_left'] || $vars['col_right']){
  221. $vars['second_cartel'] = true;
  222. }
  223. // get the audio file url
  224. $field_son_values = $node->get('field_son')->getValue();
  225. $son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
  226. $son_file = \Drupal\file\Entity\File::load($son_fid);
  227. if($son_file){
  228. $son_uri = $son_file->getFileUri();
  229. $son_url = file_create_url($son_uri);
  230. $base = log($son_file->getSize(), 1024);
  231. $suffixes = array('', 'K', 'M', 'G', 'T');
  232. $file_size = round(pow(1024, $base - floor($base)), 0) .' '. $suffixes[floor($base)];
  233. preg_match('/^.*(\.\S+)$/', $son_uri, $matches);
  234. $extension = $matches[1];
  235. $node_title = $node->getTitle();
  236. $new_file_name = str_replace(' ', '_', $node_title) . $extension;
  237. $config = \Drupal::config('system.site');
  238. $site_title = str_replace(' ', '_', $config->get('name'));
  239. $url = Url::fromUri($son_url);
  240. $url->setOptions(array(
  241. 'attributes' => array(
  242. 'class' => ['link-audio-download'],
  243. 'download' => $site_title .'__'. $new_file_name,
  244. 'title' => $new_file_name . ' ('.$file_size.')'
  245. )
  246. ));
  247. $vars['link_audio_download'] = Link::fromTextAndUrl(t("Download"), $url);
  248. }
  249. }
  250. /**
  251. * Prepares variables for image formatter templates.
  252. *
  253. * Default template: image-formatter.html.twig.
  254. *
  255. * @param array $variables
  256. * An associative array containing:
  257. * - item: An ImageItem object.
  258. * - item_attributes: An optional associative array of html attributes to be
  259. * placed in the img tag.
  260. * - image_style: An optional image style.
  261. * - url: An optional \Drupal\Core\Url object.
  262. */
  263. function edlptheme_preprocess_image_formatter(&$vars){
  264. if(isset($vars['url'])){
  265. $system_path = $vars['url']->getInternalPath();
  266. $vars['link_attributes'] = new Attribute(array(
  267. 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  268. 'class' => array('ajax-link')
  269. ));
  270. // dpm($vars);
  271. }
  272. }
  273. function edlptheme_preprocess_links__language_block(&$vars){
  274. // dpm($vars);
  275. foreach ($vars['links'] as $lang_code => $link) {
  276. $vars['links'][$lang_code]['text'] = $lang_code;
  277. $vars['links'][$lang_code]['link']['#title'] = $lang_code;
  278. }
  279. }
  280. function edlptheme_preprocess_file_link(&$vars){
  281. // dpm($vars);
  282. }