popsu.theme 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. }
  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_page(&$variables){
  29. // $rows = $variables['rows'];
  30. // }
  31. function popsu_preprocess_views_view_grid(&$variables){
  32. $rows = $variables['rows'];
  33. foreach ($rows as $id => $row) {
  34. $entity = $row["#row"]->_entity;
  35. $prog_id = $entity->get('field_programme')->getString();
  36. // une fois qu'on obtient l'id du programme
  37. // on crée une class pour chaque colonne
  38. $options = $row["#view"]->style_plugin->options;
  39. //V0
  40. $options['col_class_custom'] = "popsu-node-".$prog_id;
  41. //notes
  42. // $attrs['class'] = "popsu-node-".$prog_id;
  43. // $rows[$id]["#options"]["col_class_custom"] = new Attribute($attrs);
  44. //$rows[$id]["#options"]["col_class_custom"] = 'class';
  45. }
  46. }
  47. function popsu_preprocess_views_view_field(&$variables){
  48. // check if link to entoty option is true
  49. $settings = isset($variables['field']->options['settings']) ? $variables['field']->options['settings'] : false;
  50. if ($settings && isset($settings['link_to_entity']) && $settings['link_to_entity']) {
  51. // get the entity and build the classes
  52. $entity = $variables['row']->_entity;
  53. // build new classes
  54. $new_classes = array(
  55. $entity->getEntityTypeId(),
  56. $entity->getEntityTypeId().'-'.$entity->bundle(),
  57. $entity->getEntityTypeId().'-'.$entity->bundle().'-'.$entity->id(),
  58. );
  59. // get the entity link and url
  60. $link = $entity->toLink();
  61. $url = $entity->toLink()->getUrl();
  62. // set the active_class option to the url
  63. $url->setOption('set_active_class', true);
  64. // add new classes to the url (without overwriting existing once)
  65. $options = $url->getOptions();
  66. if(isset($options['attributes']) && $attributes = $options['attributes']){
  67. if(isset($attributes['class']) && $classes = $attributes['class']){
  68. $attributes['class'] += $new_classes;
  69. }else{
  70. $attributes['class'] = $new_classes;
  71. }
  72. }else{
  73. $attributes = array(
  74. "class" => $new_classes
  75. );
  76. }
  77. $url->setOption('attributes', $attributes);
  78. // update the url of the link
  79. $link->setUrl($url);
  80. // overwrite the output
  81. $variables['output'] = $link->toRenderable();
  82. }
  83. }