edlptheme.theme 9.6 KB

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