popsu.theme 7.2 KB

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