popsu.theme 7.8 KB

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