popsu.theme 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. }
  22. }
  23. };
  24. /* implements template_preprocess_region() */
  25. function popsu_preprocess_region(&$variables) {
  26. if (isset($variables['region'])) {
  27. $variables['attributes']['class'][] = $variables['region'].'_container';
  28. }
  29. }
  30. /**
  31. * Implements hook_preprocess_block().
  32. */
  33. function popsu_preprocess_views_view_fields(&$variables){
  34. $entity = $variables['row']->_entity;
  35. if($entity->hasField('field_programme')){
  36. $target_id = $entity->get('field_programme')->getString();
  37. //kint($variables['row']);die();
  38. // $variables['row']['attributes'] = new Attribute();
  39. // $variables['row']['attributes']->addClass("pouet-node-".$target_id);
  40. }
  41. }
  42. function popsu_preprocess_views_view_field(&$variables){
  43. // check if link to entoty option is true
  44. $settings = isset($variables['field']->options['settings']) ? $variables['field']->options['settings'] : false;
  45. if ($settings && isset($settings['link_to_entity']) && $settings['link_to_entity']) {
  46. // get the entity and build the classes
  47. $entity = $variables['row']->_entity;
  48. // build new classes
  49. $new_classes = array(
  50. $entity->getEntityTypeId(),
  51. $entity->getEntityTypeId().'-'.$entity->bundle(),
  52. $entity->getEntityTypeId().'-'.$entity->bundle().'-'.$entity->id(),
  53. );
  54. // get the entity link and url
  55. $link = $entity->toLink();
  56. $url = $entity->toLink()->getUrl();
  57. // set the active_class option to the url
  58. $url->setOption('set_active_class', true);
  59. // add new classes to the url (without overwriting existing once)
  60. $options = $url->getOptions();
  61. if(isset($options['attributes']) && $attributes = $options['attributes']){
  62. if(isset($attributes['class']) && $classes = $attributes['class']){
  63. $attributes['class'] += $new_classes;
  64. }else{
  65. $attributes['class'] = $new_classes;
  66. }
  67. }else{
  68. $attributes = array(
  69. "class" => $new_classes
  70. );
  71. }
  72. $url->setOption('attributes', $attributes);
  73. // update the url of the link
  74. $link->setUrl($url);
  75. // overwrite the output
  76. $variables['output'] = $link->toRenderable();
  77. }
  78. }