popsu.theme 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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['head_title']['slogan'] = \Drupal::config('system.site')->get('slogan');
  18. //var_dump($variables['head_title']);
  19. if( $frontpage == true){
  20. $variables['head_title'] = $variables['head_title']["name"]." | ".$variables['head_title']['slogan'];
  21. }else{
  22. $variables['head_title'] = $variables['head_title']["name"];
  23. }
  24. if ($node){
  25. if($node->hasField('field_programme') && $node->get('field_programme')->getString() != ""){
  26. $variables['attributes']['class'][] = 'programme-' . $node->get('field_programme')->getString();
  27. }else{
  28. if ($node->id()){
  29. $variables['attributes']['class'][] = 'programme-' . $node->id();
  30. }
  31. }
  32. }
  33. }
  34. function popsu_preprocess_node(&$variables){
  35. $node = &$variables['node'];
  36. if($node->hasField('field_programme') && $node->get('field_programme')->getString() != ""){
  37. $variables['attributes']['class'][] = 'programme-' . $node->get('field_programme')->getString();
  38. }
  39. }
  40. function popsu_preprocess_field(&$variables){
  41. $element = &$variables['element'];
  42. if (isset($element['#field_name'])) {
  43. $variables['attributes']['class'][] = $element['#field_name'];
  44. if ($element['#field_name'] == 'field_texte') {
  45. $variables['attributes']['class'][] = 'crop';
  46. }
  47. if($element["#field_name"] == "field_programme"){
  48. foreach ($element["#items"]->getIterator() as $id => $item) {
  49. $nid = $item->getValue()['target_id'];
  50. $variables['items'][$id]['attributes']['class'] = new Attribute();
  51. $variables['items'][$id]['attributes']->addClass("programme-".$nid);
  52. }
  53. }
  54. }
  55. }
  56. function popsu_preprocess_views_view_unformatted(&$variables) {
  57. $view = &$variables['view'];
  58. $rows = &$variables['rows'];
  59. if($view->id() == "programmes"){
  60. foreach ($rows as $id => $row) {
  61. $variables['rows'][$id]['attributes'] = new Attribute();
  62. $c = $variables['rows'][$id]['content']["#row"]->nid;
  63. $variables['rows'][$id]['attributes']->addClass("programme-".$c);
  64. }
  65. }else{
  66. foreach ($rows as $id => $row) {
  67. if(isset($row['content']['#row'])){
  68. $r = $row['content']['#row'];
  69. $entity = $r->_entity;
  70. if ($entity->hasField('field_programme') && $entity->get('field_programme')->getString() != "" ) {
  71. $c = $entity->get('field_programme')->getString();
  72. $variables['rows'][$id]['attributes'] = new Attribute();
  73. $variables['rows'][$id]['attributes']->addClass("programme-".$c);
  74. if ($entity->hasField('field_type_de_ressource') && $entity->get('field_type_de_ressource')->getString() == "74" ) {
  75. $type = $entity->get('field_type_de_ressource')->getString();
  76. $variables['rows'][$id]['attributes']['class'][] = "type-".$type;
  77. }
  78. }
  79. }else if(isset($row['content']['#node'])){
  80. $r = $row['content']['#node'];
  81. if ($r->hasField('field_programme') && $r->get('field_programme')->getString() != "" ) {
  82. $c = $r->get('field_programme')->getString();
  83. $variables['rows'][$id]['attributes'] = new Attribute();
  84. $variables['rows'][$id]['attributes']->addClass("programme-".$c);
  85. }
  86. }
  87. }
  88. }
  89. };
  90. /* implements template_preprocess_region() */
  91. function popsu_preprocess_region(&$variables) {
  92. if (isset($variables['region'])) {
  93. $variables['attributes']['class'][] = $variables['region'].'_container';
  94. }
  95. }
  96. /**
  97. * Implements hook_preprocess_views_view_unformatted().
  98. */
  99. function popsu_preprocess_views_view_grid(&$variables){
  100. // we pass all variables by reference
  101. // https://www.php.net/manual/en/language.references.php
  102. $items =& $variables['items'];
  103. // loop through grid items (row and cols) not through views\rows
  104. foreach ($items as $r => &$row) {
  105. foreach ($row['content'] as $c => &$col) {
  106. // get the entity
  107. $entity = $col['content']["#row"]->_entity;
  108. // build generique classe
  109. $classes = array(
  110. $entity->getEntityTypeId(),
  111. $entity->getEntityTypeId().'-'.$entity->bundle(),
  112. $entity->getEntityTypeId().'-'.$entity->bundle().'-'.$entity->id(),
  113. );
  114. // kint($entity);die();
  115. // if field_programme exists, add a class
  116. if ($entity->hasField('field_programme')) {
  117. $prog_id = $entity->get('field_programme')->getString();
  118. $classes[] = "programme-".$prog_id;
  119. }
  120. if ($entity->hasField('field_type_de_ressource')) {
  121. $type = $entity->get('field_type_de_ressource')->getString();
  122. $classes[] = "type-".$type;
  123. }
  124. // add the classes to the column
  125. // Drupal\Core\Template\Attribute
  126. // https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Template%21Attribute.php/class/Attribute/8.2.x
  127. $col['attributes']->addClass($classes);
  128. }
  129. }
  130. }
  131. function popsu_preprocess_views_view_field(&$variables){
  132. // check if link to entity option is true
  133. $settings = isset($variables['field']->options['settings']) ? $variables['field']->options['settings'] : false;
  134. if ($settings && isset($settings['link_to_entity']) && $settings['link_to_entity']) {
  135. // get the entity and build the classes
  136. $entity = $variables['row']->_entity;
  137. // $entity_type = $entity->getEntityType();
  138. if ($entity->getEntityType()->id() == "node" && $entity->id() == 5) {
  139. // temporarely desactivate popsumonde link
  140. // get the entity link and url
  141. $link = $entity->toLink();
  142. // // $url = $entity->toLink()->getUrl();
  143. // $url = URL::fromUserInput('#');
  144. // // update the url of the link
  145. // $link->setUrl($url);
  146. // overwrite the output
  147. $variables['output'] = [
  148. '#type' => 'html_tag',
  149. '#tag' => 'span',
  150. '#value' => $link->getText(),
  151. '#attributes' => [
  152. "class" => array(
  153. $entity->getEntityTypeId(),
  154. $entity->getEntityTypeId().'-'.$entity->bundle(),
  155. $entity->getEntityTypeId().'-'.$entity->bundle().'-'.$entity->id(),
  156. )
  157. ]
  158. ];
  159. }else{
  160. // build new classes
  161. $new_classes = array(
  162. $entity->getEntityTypeId(),
  163. $entity->getEntityTypeId().'-'.$entity->bundle(),
  164. $entity->getEntityTypeId().'-'.$entity->bundle().'-'.$entity->id(),
  165. );
  166. // get the entity link and url
  167. $link = $entity->toLink();
  168. $url = $entity->toLink()->getUrl();
  169. // set the active_class option to the url
  170. $url->setOption('set_active_class', true);
  171. // add new classes to the url (without overwriting existing once)
  172. $options = $url->getOptions();
  173. if(isset($options['attributes']) && $attributes = $options['attributes']){
  174. if(isset($attributes['class']) && $classes = $attributes['class']){
  175. $attributes['class'] += $new_classes;
  176. }else{
  177. $attributes['class'] = $new_classes;
  178. }
  179. }else{
  180. $attributes = array(
  181. "class" => $new_classes
  182. );
  183. }
  184. $url->setOption('attributes', $attributes);
  185. // update the url of the link
  186. $link->setUrl($url);
  187. // overwrite the output
  188. $variables['output'] = $link->toRenderable();
  189. }
  190. }
  191. }
  192. function popsu_preprocess_block__entity_field(&$variables){
  193. $field = &$variables['content']['field'];
  194. $items = &$field['#items'];
  195. if ($field['#field_type'] === "entity_reference_revisions") {
  196. $entity = $items->first()->get('entity');
  197. $entityAdapter = $entity->getTarget();
  198. $referencedEntity = $entityAdapter->getValue();
  199. $field_definitions = $referencedEntity->getFieldDefinitions();
  200. $all_empty = TRUE;
  201. foreach ($field_definitions as $field_name => $definition) {
  202. if (!method_exists($definition, 'isBaseField')) {
  203. $field_instance = $referencedEntity->get($field_name);
  204. if (!$field_instance->isEmpty()) {
  205. $all_empty = FALSE;
  206. }
  207. }
  208. }
  209. if ($all_empty) {
  210. $variables['isEmpty'] = true;
  211. }
  212. }
  213. }