popsu.theme 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /*
  3. * @files
  4. * Theme Template Funtions
  5. */
  6. use Drupal\Core\Template\Attribute;
  7. /* implements template_preprocess_views_view_unformatted() */
  8. function popsu_preprocess_views_view_unformatted(&$variables) {
  9. $view = $variables['view'];
  10. if($view->id() == "programmes"){
  11. $rows = $variables['rows'];
  12. foreach ($rows as $id => $row) {
  13. $variables['rows'][$id]['attributes'] = new Attribute();
  14. $c = $variables['rows'][$id]['content']["#row"]->nid;
  15. $variables['rows'][$id]['attributes']->addClass("programme-".$c);
  16. }
  17. }
  18. };
  19. /* implements template_preprocess_region() */
  20. function popsu_preprocess_region(&$variables) {
  21. if (isset($variables['region'])) {
  22. $variables['attributes']['class'][] = $variables['region'].'_container';
  23. }
  24. }
  25. /**
  26. * Implements hook_preprocess_page().
  27. */
  28. function popsu_preprocess_views_view(&$variables){
  29. $vars = $variables;
  30. if(isset($variables['css_class']) && $variables['css_class'] == 'last-news'){
  31. $rows =& $variables['rows'];
  32. foreach ($rows as $i => &$row ){
  33. foreach ($row['#rows'] as $j => &$elem) {
  34. $entity = $elem["#row"]->_entity;
  35. if ($entity->hasField('field_programme')) {
  36. $prog_id = $entity->get('field_programme')->getString();
  37. $class = "programme-".$prog_id;
  38. $attributes = array(
  39. "class" => $class
  40. );
  41. }
  42. // $elem['id'] = new Attribute("socks");
  43. //$elem['attributes']->addClass($class);
  44. }
  45. }
  46. }
  47. }
  48. function popsu_preprocess_views_view_grid(&$variables){
  49. // we pass all variables by reference
  50. // https://www.php.net/manual/en/language.references.php
  51. $items =& $variables['items'];
  52. // loop through grid items (row and cols) not through views\rows
  53. foreach ($items as $r => &$row) {
  54. foreach ($row['content'] as $c => &$col) {
  55. // get the entity
  56. $entity = $col['content']["#row"]->_entity;
  57. // build generique classe
  58. $classes = array(
  59. $entity->getEntityTypeId(),
  60. $entity->getEntityTypeId().'-'.$entity->bundle(),
  61. $entity->getEntityTypeId().'-'.$entity->bundle().'-'.$entity->id(),
  62. );
  63. // if field_programme exists, add a class
  64. if ($entity->hasField('field_programme')) {
  65. $prog_id = $entity->get('field_programme')->getString();
  66. $classes[] = "programme-".$prog_id;
  67. }
  68. // add the classes to the column
  69. // Drupal\Core\Template\Attribute
  70. // https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Template%21Attribute.php/class/Attribute/8.2.x
  71. $col['attributes']->addClass($classes);
  72. }
  73. }
  74. }
  75. function popsu_preprocess_views_view_field(&$variables){
  76. // check if link to entoty option is true
  77. $settings = isset($variables['field']->options['settings']) ? $variables['field']->options['settings'] : false;
  78. if ($settings && isset($settings['link_to_entity']) && $settings['link_to_entity']) {
  79. // get the entity and build the classes
  80. $entity = $variables['row']->_entity;
  81. // build new classes
  82. $new_classes = array(
  83. $entity->getEntityTypeId(),
  84. $entity->getEntityTypeId().'-'.$entity->bundle(),
  85. $entity->getEntityTypeId().'-'.$entity->bundle().'-'.$entity->id(),
  86. );
  87. // get the entity link and url
  88. $link = $entity->toLink();
  89. $url = $entity->toLink()->getUrl();
  90. // set the active_class option to the url
  91. $url->setOption('set_active_class', true);
  92. // add new classes to the url (without overwriting existing once)
  93. $options = $url->getOptions();
  94. if(isset($options['attributes']) && $attributes = $options['attributes']){
  95. if(isset($attributes['class']) && $classes = $attributes['class']){
  96. $attributes['class'] += $new_classes;
  97. }else{
  98. $attributes['class'] = $new_classes;
  99. }
  100. }else{
  101. $attributes = array(
  102. "class" => $new_classes
  103. );
  104. }
  105. $url->setOption('attributes', $attributes);
  106. // update the url of the link
  107. $link->setUrl($url);
  108. // overwrite the output
  109. $variables['output'] = $link->toRenderable();
  110. }
  111. }