edlptheme.theme 9.2 KB

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