popsu.theme 6.8 KB

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