popsu.theme 8.8 KB

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