popsu.theme 7.2 KB

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