popsu.theme 8.3 KB

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