popsu.theme 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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("popsu-node-".$c);
  16. //kint($variables['rows'][$id]['content']["#row"]->nid);
  17. }
  18. }else{
  19. if ($view->current_display == "block_1") {
  20. $rows = $variables['rows'];
  21. foreach ($rows as $id => $row) {
  22. $variables['rows'][$id]['attributes'] = new Attribute();
  23. $c = $variables['rows'][$id]['content']["#row"]->nid;
  24. $variables['rows'][$id]['attributes']->addClass("popsu-node-".$c);
  25. }
  26. }
  27. }
  28. /*
  29. $rows = $variables['rows'];
  30. $style = $view->style_plugin;
  31. $options = $style->options;
  32. $variables['default_row_class'] = !empty($options['default_row_class']);
  33. */
  34. /*
  35. foreach ($rows as $id => $row) {
  36. $variables['rows'][$id] = array();
  37. $variables['rows'][$id]['content'] = $row;
  38. $variables['rows'][$id]['attributes'] = new Attribute();
  39. if ($row_class = $view->style_plugin
  40. ->getRowClass($id)) {
  41. $variables['rows'][$id]['attributes']
  42. ->addClass($row_class);
  43. }
  44. }
  45. */
  46. };
  47. /**
  48. * Implements hook_preprocess_block().
  49. */
  50. // function popsu_preprocess_block(&$variables) {
  51. // $t="t";
  52. // }
  53. function popsu_preprocess_views_view_field(&$variables){
  54. // check if link to entoty option is true
  55. $settings = $variables['field']->options['settings'];
  56. if ($settings['link_to_entity']) {
  57. // get the entity and build the classes
  58. $entity = $variables['row']->_entity;
  59. // build new classes
  60. $new_classes = array(
  61. $entity->getEntityTypeId(),
  62. $entity->getEntityTypeId().'-'.$entity->bundle(),
  63. $entity->getEntityTypeId().'-'.$entity->bundle().'-'.$entity->id()
  64. );
  65. // get the entity link and url
  66. $link = $entity->toLink();
  67. $url = $entity->toLink()->getUrl();
  68. // set the active_class option to the url
  69. $url->setOption('set_active_class', true);
  70. // add new classes to the url (without overwriting existing once)
  71. $options = $url->getOptions();
  72. if(isset($options['attributes']) && $attributes = $options['attributes']){
  73. if(isset($attributes['class']) && $classes = $attributes['class']){
  74. $attributes['class'] += $new_classes;
  75. }else{
  76. $attributes['class'] = $new_classes;
  77. }
  78. }else{
  79. $attributes = array(
  80. "class" => $new_classes
  81. );
  82. }
  83. $url->setOption('attributes', $attributes);
  84. // update the url of the link
  85. $link->setUrl($url);
  86. // overwrite the output
  87. $variables['output'] = $link->toRenderable();
  88. }
  89. }