popsu.theme 6.4 KB

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