popsu.theme 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * @files
  4. * Theme Template Funtions
  5. */
  6. use Drupal\Core\Template\Attribute;
  7. /* implements template_preprocess_views_view() */
  8. // function popsu_preprocess_views_view(&$vars) {
  9. // $view = $vars['view'];
  10. // kint($view);die();
  11. // }
  12. /* implements template_preprocess_views_view_unformatted() */
  13. function popsu_preprocess_views_view_unformatted(&$variables) {
  14. $view = $variables['view'];
  15. if($view->id() == "programmes"){
  16. $rows = $variables['rows'];
  17. foreach ($rows as $id => $row) {
  18. $variables['rows'][$id]['attributes'] = new Attribute();
  19. $c = $variables['rows'][$id]['content']["#row"]->nid;
  20. $variables['rows'][$id]['attributes']->addClass("popsu-node-".$c);
  21. //kint($variables['rows'][$id]['content']["#row"]->nid);
  22. }
  23. }
  24. // else{
  25. // if ($view->current_display == "block_1") {
  26. // $rows = $variables['rows'];
  27. // foreach ($rows as $id => $row) {
  28. // $variables['rows'][$id]['attributes'] = new Attribute();
  29. // $c = $variables['rows'][$id]['content']["#row"]->nid;
  30. // $variables['rows'][$id]['attributes']->addClass("popsu-node-".$c);
  31. // }
  32. // }
  33. }
  34. };
  35. /* implements template_preprocess_region() */
  36. function popsu_preprocess_region(&$variables) {
  37. if (isset($variables['region'])) {
  38. $variables['attributes']['class'][] = $variables['region'].'_container';
  39. }
  40. // if ($variables['region'] == 'footer_bottom_right') {
  41. // if ($node = \Drupal::routeMatch()->getParameter('node')) {
  42. // $t = $node->getType();
  43. // }
  44. // }
  45. }
  46. // function popsu_preprocess_field(&$variables){
  47. // $v = $variables;
  48. // }
  49. // function popsu_theme_suggestions_views_view_alter(array &$suggestions, array $variables) {
  50. // $v = $variables;
  51. // $suggestions[] = 'views_view__headerView';
  52. // }
  53. /**
  54. * Implements hook_preprocess_block().
  55. */
  56. function popsu_preprocess_views_view_fields(&$variables){
  57. $entity = $variables['row']->_entity;
  58. if($entity->hasField('field_programme')){
  59. $target_id = $entity->get('field_programme')->getString();
  60. // kint($variables);die();
  61. // $variables['row']['attributes'] = new Attribute();
  62. // $variables['row']['attributes']->addClass("pouet-node-".$target_id);
  63. }
  64. }
  65. function popsu_preprocess_views_view_field(&$variables){
  66. // check if link to entoty option is true
  67. $settings = isset($variables['field']->options['settings']) ? $variables['field']->options['settings'] : false;
  68. if ($settings && isset($settings['link_to_entity']) && $settings['link_to_entity']) {
  69. // get the entity and build the classes
  70. $entity = $variables['row']->_entity;
  71. // build new classes
  72. $new_classes = array(
  73. $entity->getEntityTypeId(),
  74. $entity->getEntityTypeId().'-'.$entity->bundle(),
  75. $entity->getEntityTypeId().'-'.$entity->bundle().'-'.$entity->id(),
  76. );
  77. // get the entity link and url
  78. $link = $entity->toLink();
  79. $url = $entity->toLink()->getUrl();
  80. // set the active_class option to the url
  81. $url->setOption('set_active_class', true);
  82. // add new classes to the url (without overwriting existing once)
  83. $options = $url->getOptions();
  84. if(isset($options['attributes']) && $attributes = $options['attributes']){
  85. if(isset($attributes['class']) && $classes = $attributes['class']){
  86. $attributes['class'] += $new_classes;
  87. }else{
  88. $attributes['class'] = $new_classes;
  89. }
  90. }else{
  91. $attributes = array(
  92. "class" => $new_classes
  93. );
  94. }
  95. $url->setOption('attributes', $attributes);
  96. // update the url of the link
  97. $link->setUrl($url);
  98. // overwrite the output
  99. $variables['output'] = $link->toRenderable();
  100. }
  101. }