edlptheme.theme 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * @file
  4. * Functions to support theming in the edlp theme.
  5. */
  6. use Drupal\Core\Url;
  7. use Drupal\Core\Form\FormStateInterface;
  8. use Drupal\Core\Template\Attribute;
  9. use Drupal\Component\Utility\Unicode;
  10. use Drupal\Core\Render\Element;
  11. // function edlptheme_preprocess_input(&$vars){
  12. // dsm($vars, 'vars');
  13. // $vars['attributes']['placeholder'] = "salut";
  14. // }
  15. function edlptheme_preprocess_node(&$vars){
  16. $node = $vars['elements']['#node'];
  17. $options = ['absolute' => TRUE];
  18. $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  19. $system_path = $url->getInternalPath();
  20. $vars['link_attributes'] = new Attribute(array(
  21. 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path
  22. ));
  23. }
  24. /**
  25. * Implements hook_form_alter
  26. */
  27. function edlptheme_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  28. // dpm($form_id, 'form_id');
  29. // dpm($form, 'form');
  30. $form['name']['#attributes']['placeholder'] = (string) $form['name']['#title'];
  31. unset($form['name']['#title']);
  32. $form['pass']['#attributes']['placeholder'] = (string) $form['pass']['#title'];
  33. unset($form['pass']['#title']);
  34. }
  35. function edlptheme_preprocess_edlp_productions(&$vars){
  36. foreach($vars['nodes'] as &$node){
  37. switch($node['vm']){
  38. case "image_2_columns":
  39. $cols = 4;
  40. break;
  41. case "image_1_columns":
  42. $cols = 2;
  43. break;
  44. case "text_1_column":
  45. $cols = 2;
  46. break;
  47. };
  48. $node['cols'] = $cols;
  49. }
  50. // dpm($vars);
  51. }
  52. function edlptheme_preprocess_node__enregistrement__index(&$vars){
  53. $node = $vars['elements']['#node'];
  54. $options = ['absolute' => TRUE];
  55. $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options);
  56. $system_path = $url->getInternalPath();
  57. // get the audio file url
  58. $field_son_values = $node->get('field_son')->getValue();
  59. $son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
  60. $son_file = \Drupal\file\Entity\File::load($son_fid);
  61. $son_url = null;
  62. if($son_file){
  63. $son_uri = $son_file->getFileUri();
  64. $son_url = file_create_url($son_uri);
  65. }
  66. $vars['link_attributes'] = new Attribute(array(
  67. 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  68. 'audio_url' => $son_url,
  69. 'nid' => $node->id(),
  70. 'class' => array('audio-link', 'ajax-link')
  71. ));
  72. // dpm($vars['link_attributes']);
  73. }
  74. function edlptheme_preprocess_node__enregistrement__player_cartel(&$vars){
  75. // dpm($vars);
  76. // if transcript not empty
  77. $url = Url::fromRoute('entity.node.canonical', ['node'=>$vars['node']->id()], array(
  78. 'attributes' => array(
  79. 'class' => ['link-transcript', 'ajax-link'],
  80. 'viewmode'=>'transcript'
  81. )
  82. ));
  83. $vars['link_transcript'] = array(
  84. '#title' => t("Lire le text."),
  85. '#type' => 'link',
  86. '#url' => $url,
  87. '#options'=>array(
  88. 'attributes' => array(
  89. 'data-drupal-link-system-path' => $url->getInternalPath()
  90. )
  91. )
  92. );
  93. // TODO: refacorize the link generator as following :
  94. // $url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term'=>$tid]);
  95. // $url->setOptions(array(
  96. // 'attributes' => array(
  97. // 'class' => ['index-link', 'ajax-link'],
  98. // 'viewmode'=>'index',
  99. // 'tid'=>$tid,
  100. // 'data-drupal-link-system-path' => $url->getInternalPath()
  101. // )
  102. // ));
  103. // $entree['index_link'] = Link::fromTextAndUrl('index', $url);
  104. // if article not empty
  105. $url = Url::fromRoute('entity.node.canonical', ['node'=>$vars['node']->id()], array(
  106. 'attributes' => array(
  107. 'class' => ['link-article', 'ajax-link'],
  108. 'viewmode'=>'article'
  109. )
  110. ));
  111. $vars['link_article'] = array(
  112. '#title' => t("Lire l'article."),
  113. '#type' => 'link',
  114. '#url' => $url,
  115. '#options' => array(
  116. 'attributes'=>array(
  117. 'data-drupal-link-system-path' => $url->getInternalPath()
  118. )
  119. )
  120. );
  121. // if article or transcript
  122. $vars['col_left'] = true;
  123. // if
  124. $vars['col_right'] = true;
  125. }
  126. /**
  127. * Prepares variables for image formatter templates.
  128. *
  129. * Default template: image-formatter.html.twig.
  130. *
  131. * @param array $variables
  132. * An associative array containing:
  133. * - item: An ImageItem object.
  134. * - item_attributes: An optional associative array of html attributes to be
  135. * placed in the img tag.
  136. * - image_style: An optional image style.
  137. * - url: An optional \Drupal\Core\Url object.
  138. */
  139. function edlptheme_preprocess_image_formatter(&$vars){
  140. if(isset($vars['url'])){
  141. $system_path = $vars['url']->getInternalPath();
  142. $vars['link_attributes'] = new Attribute(array(
  143. 'data-drupal-link-system-path' => $system_path=='' ? '<front>' : $system_path,
  144. 'class' => array('ajax-link')
  145. ));
  146. // dpm($vars);
  147. }
  148. }