popsu.theme 6.8 KB

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