popsu.theme 7.6 KB

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