popsu.theme 6.3 KB

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