popsu.theme 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /*
  3. * @files
  4. * Theme Template Funtions
  5. */
  6. use Drupal\Core\Template\Attribute;
  7. use Drupal\Core\Link;
  8. use Drupal\Core\Url;
  9. // function popsu_preprocess_views_view_list(&$variables){
  10. // kint($variables);die();
  11. // }
  12. function popsu_preprocess_html(&$variables) {
  13. $node = \Drupal::routeMatch()->getParameter('node');
  14. // $type = $node->getType();
  15. //
  16. $frontpage = \Drupal::service('path.matcher')->isFrontPage();
  17. $variables['site_name'] = \Drupal::config('system.site')->get('name');
  18. $variables['slogan'] = \Drupal::config('system.site')->get('slogan');
  19. //var_dump($variables['head_title']);
  20. if( $frontpage == true){
  21. $variables['head_title'] = [$variables["site_name"], $variables['slogan']];
  22. }
  23. // else{
  24. // $variables['head_title'] = $variables['site_name'];
  25. // }
  26. if ($node){
  27. if($node->hasField('field_programme') && $node->get('field_programme')->getString() != ""){
  28. $variables['attributes']['class'][] = 'programme-' . $node->get('field_programme')->getString();
  29. }else{
  30. if ($node->id()){
  31. $variables['attributes']['class'][] = 'programme-' . $node->id();
  32. }
  33. }
  34. }
  35. }
  36. // function popsu_preprocess_page(&$variables) {
  37. // if(isset($variables['node'])){
  38. // $node = $variables['node'];
  39. // if(isset($node) && $node->hasField('field_videos') && $node->get('field_videos')->getString() != ""){
  40. // $variables['attributes']['class'][] = 'ressources-video';
  41. // }
  42. // }
  43. // }
  44. function popsu_preprocess_page__node(&$variables) {
  45. $node = $variables['node'];
  46. if($node->getType() === "ressource"){
  47. $type_ref_entity = $node->get('field_type_de_ressource')->referencedEntities();
  48. if(count($type_ref_entity)){
  49. // $type = filter_var(strtolower(str_replace(" ", "-", $type_ref_entity[0]->getName())), FILTER_SANITIZE_URL);
  50. // setlocale(LC_ALL, "en_US.utf8");
  51. // $type = iconv('UTF-8','ASCII//TRANSLIT//IGNORE',strtolower(str_replace(" ", "-", $type_ref_entity[0]->getName())));
  52. $trans = \Drupal::transliteration();
  53. $type = $trans->transliterate(strtolower(str_replace(" ", "-", $type_ref_entity[0]->getName())), 'en');
  54. $variables['attributes']['class'][] = 'ressources-' . $type;
  55. }
  56. }
  57. }
  58. function popsu_preprocess_node(&$variables){
  59. $node = &$variables['node'];
  60. if($node->hasField('field_programme') && $node->get('field_programme')->getString() != ""){
  61. $variables['attributes']['class'][] = 'programme-' . $node->get('field_programme')->getString();
  62. }
  63. }
  64. function popsu_preprocess_field(&$variables){
  65. $element = &$variables['element'];
  66. if (isset($element['#field_name'])) {
  67. $variables['attributes']['class'][] = $element['#field_name'];
  68. if ($element['#field_name'] == 'field_texte') {
  69. $variables['attributes']['class'][] = 'crop';
  70. }
  71. if($element["#field_name"] == "field_programme"){
  72. foreach ($element["#items"]->getIterator() as $id => $item) {
  73. $nid = $item->getValue()['target_id'];
  74. //dump($nid);
  75. $variables['items'][$id]['attributes'] = new Attribute();
  76. $variables['items'][$id]['attributes']->addClass("programme-".$nid);
  77. }
  78. }
  79. }
  80. }
  81. function popsu_preprocess_views_view_unformatted(&$variables) {
  82. $view = &$variables['view'];
  83. $rows = &$variables['rows'];
  84. if($view->id() == "programmes"){
  85. foreach ($rows as $id => $row) {
  86. $variables['rows'][$id]['attributes'] = new Attribute();
  87. $c = $variables['rows'][$id]['content']["#row"]->nid;
  88. $variables['rows'][$id]['attributes']->addClass("programme-".$c);
  89. }
  90. }else{
  91. foreach ($rows as $id => $row) {
  92. if(isset($row['content']['#row'])){
  93. $r = $row['content']['#row'];
  94. $entity = $r->_entity;
  95. if ($entity->hasField('field_programme') && $entity->get('field_programme')->getString() != "" ) {
  96. $variables['rows'][$id]['attributes'] = new Attribute();
  97. $referenced_entities = $entity->get('field_programme')->referencedEntities();
  98. foreach ($referenced_entities as $ref_entity) {
  99. $variables['rows'][$id]['attributes']->addClass("programme-".$ref_entity->id());
  100. }
  101. if ($entity->hasField('field_type_de_ressource') && $entity->get('field_type_de_ressource')->getString() == "74" ) {
  102. $type = $entity->get('field_type_de_ressource')->getString();
  103. $variables['rows'][$id]['attributes']['class'][] = "type-".$type;
  104. }
  105. }
  106. }else if(isset($row['content']['#node'])){
  107. $r = $row['content']['#node'];
  108. if ($r->hasField('field_programme') && $r->get('field_programme')->getString() != "" ) {
  109. $c = $r->get('field_programme')->getString();
  110. $variables['rows'][$id]['attributes'] = new Attribute();
  111. $variables['rows'][$id]['attributes']->addClass("programme-".$c);
  112. }
  113. }
  114. }
  115. }
  116. };
  117. /* implements template_preprocess_region() */
  118. function popsu_preprocess_region(&$variables) {
  119. if (isset($variables['region'])) {
  120. $variables['attributes']['class'][] = $variables['region'].'_container';
  121. }
  122. }
  123. /**
  124. * Implements hook_preprocess_views_view_unformatted().
  125. */
  126. function popsu_preprocess_views_view_grid(&$variables){
  127. // we pass all variables by reference
  128. // https://www.php.net/manual/en/language.references.php
  129. $items =& $variables['items'];
  130. // loop through grid items (row and cols) not through views\rows
  131. foreach ($items as $r => &$row) {
  132. foreach ($row['content'] as $c => &$col) {
  133. // get the entity
  134. $entity = $col['content']["#row"]->_entity;
  135. // build generique classe
  136. $classes = array(
  137. $entity->getEntityTypeId(),
  138. $entity->getEntityTypeId().'-'.$entity->bundle(),
  139. $entity->getEntityTypeId().'-'.$entity->bundle().'-'.$entity->id(),
  140. );
  141. // kint($entity);die();
  142. // if field_programme exists, add a class
  143. if ($entity->hasField('field_programme')) {
  144. $referenced_entities = $entity->get('field_programme')->referencedEntities();
  145. foreach ($referenced_entities as $ref_entity) {
  146. $classes[] = "programme-".$ref_entity->id();
  147. }
  148. }
  149. if ($entity->hasField('field_type_de_ressource')) {
  150. $type = $entity->get('field_type_de_ressource')->getString();
  151. $classes[] = "type-".$type;
  152. }
  153. // add the classes to the column
  154. // Drupal\Core\Template\Attribute
  155. // https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Template%21Attribute.php/class/Attribute/8.2.x
  156. $col['attributes']->addClass($classes);
  157. }
  158. }
  159. }
  160. function popsu_preprocess_views_view_field(&$variables){
  161. // check if link to entity option is true
  162. $settings = isset($variables['field']->options['settings']) ? $variables['field']->options['settings'] : false;
  163. if ($settings && isset($settings['link']) && $settings['link']) {
  164. // get the entity and build the classes
  165. $entity = $variables['row']->_entity;
  166. // $entity_type = $entity->getEntityType();
  167. if ($entity->getEntityType()->id() == "node" && $entity->id() == 5) {
  168. // temporarely desactivate popsumonde link
  169. // get the entity link and url
  170. $link = $entity->toLink();
  171. // // $url = $entity->toLink()->getUrl();
  172. // $url = URL::fromUserInput('#');
  173. // // update the url of the link
  174. // $link->setUrl($url);
  175. // overwrite the output
  176. $variables['output'] = [
  177. '#type' => 'html_tag',
  178. '#tag' => 'span',
  179. '#value' => $link->getText(),
  180. '#attributes' => [
  181. "class" => array(
  182. $entity->getEntityTypeId(),
  183. $entity->getEntityTypeId().'-'.$entity->bundle(),
  184. $entity->getEntityTypeId().'-'.$entity->bundle().'-'.$entity->id(),
  185. )
  186. ]
  187. ];
  188. }else if ($entity->getType === 'programme') {
  189. // build new classes
  190. $new_classes = array(
  191. $entity->getEntityTypeId(),
  192. $entity->getEntityTypeId().'-'.$entity->bundle(),
  193. $entity->getEntityTypeId().'-'.$entity->bundle().'-'.$entity->id(),
  194. );
  195. // get the entity link and url
  196. $link = $entity->toLink();
  197. $url = $entity->toLink()->getUrl();
  198. // set the active_class option to the url
  199. $url->setOption('set_active_class', true);
  200. // add new classes to the url (without overwriting existing once)
  201. $options = $url->getOptions();
  202. if(isset($options['attributes']) && $attributes = $options['attributes']){
  203. if(isset($attributes['class']) && $classes = $attributes['class']){
  204. $attributes['class'] += $new_classes;
  205. }else{
  206. $attributes['class'] = $new_classes;
  207. }
  208. }else{
  209. $attributes = array(
  210. "class" => $new_classes
  211. );
  212. }
  213. $url->setOption('attributes', $attributes);
  214. // update the url of the link
  215. $link->setUrl($url);
  216. // overwrite the output
  217. $variables['output'] = $link->toRenderable();
  218. }
  219. }
  220. }
  221. function popsu_preprocess_block__entity_field(&$variables){
  222. $field = &$variables['content']['field'];
  223. $items = &$field['#items'];
  224. if ($field['#field_type'] === "entity_reference_revisions") {
  225. $entity = $items->first()->get('entity');
  226. $entityAdapter = $entity->getTarget();
  227. $referencedEntity = $entityAdapter->getValue();
  228. $field_definitions = $referencedEntity->getFieldDefinitions();
  229. $all_empty = TRUE;
  230. foreach ($field_definitions as $field_name => $definition) {
  231. if (!method_exists($definition, 'isBaseField')) {
  232. $field_instance = $referencedEntity->get($field_name);
  233. if (!$field_instance->isEmpty()) {
  234. $all_empty = FALSE;
  235. }
  236. }
  237. }
  238. if ($all_empty) {
  239. $variables['isEmpty'] = true;
  240. }
  241. }
  242. }