HomeController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace Drupal\edlp_home\Controller;
  3. use Drupal\Core\Controller\ControllerBase;
  4. use Drupal\Core\Datetime\DrupalDateTime;
  5. use Drupal\taxonomy\Entity\Term;
  6. use Drupal\workflow\Entity\WorkflowManager;
  7. use Drupal\Core\Url;
  8. use Drupal\Core\Language\LanguageInterface;
  9. // use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Drupal\Core\Cache\CacheableJsonResponse;
  11. use Drupal\Core\Cache\CacheableMetadata;
  12. use Drupal\core\render\RenderContext;
  13. class HomeController extends ControllerBase {
  14. /**
  15. * Display home as a page.
  16. *
  17. * @return renderable array
  18. */
  19. public function home() {
  20. // $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
  21. $contents = array("#theme"=>'edlp_home');
  22. // first get static pages
  23. $query = \Drupal::entityQuery('node')
  24. ->condition('status', 1)
  25. ->condition('field_afficher_en_page_d_acceuil', 1)
  26. ->condition('type', 'static');
  27. $promoted_nids = $query->execute();
  28. $contents["#promoted_statics"] = entity_load_multiple('node', $promoted_nids);
  29. // then get production pages
  30. $query = \Drupal::entityQuery('node')
  31. ->condition('status', 1)
  32. ->condition('field_afficher_en_page_d_acceuil', 1)
  33. ->condition('type', 'page');
  34. $promoted_nids = $query->execute();
  35. $contents["#promoted_prods"] = entity_load_multiple('node', $promoted_nids);
  36. // presentation
  37. // $query = \Drupal::entityQuery('node')
  38. // ->condition('status', 1)
  39. // ->condition('nid', 12242);
  40. // // TODO: présentation nid should be a setting
  41. //
  42. // $pres_nid = $query->execute();
  43. // $contents["#presentation_node"] = entity_load('node', array_pop($pres_nid));
  44. // last fil
  45. // $query = \Drupal::entityQuery('node')
  46. // ->condition('status', 1)
  47. // ->condition('type', 'fil')
  48. // ->sort('created', 'DESC')
  49. // ->range(0,1);
  50. //
  51. // $fil = $query->execute();
  52. // $contents["#last_fil_node"] = entity_load('node', array_pop($fil));
  53. // $contents["#last_fil_node"] = array('#markup'=>'En développement.');
  54. // last production
  55. // $query = \Drupal::entityQuery('node')
  56. // ->condition('status', 1)
  57. // ->condition('type', 'page')
  58. // ->condition('field_page_type', array('1168'), 'NOT IN')
  59. // ->sort('created', 'DESC')
  60. // ->range(0,1);
  61. //
  62. // $prod = $query->execute();
  63. // $contents["#last_production_node"] = entity_load('node', array_pop($prod));
  64. // last documents
  65. $query = \Drupal::entityQuery('node')
  66. ->condition('status', 1)
  67. ->condition('type', 'enregistrement')
  68. ->sort('created', 'DESC')
  69. ->range(0,50);
  70. $nids = $query->execute();
  71. $nodes = entity_load_multiple('node', $nids);
  72. $lastdocs = [];
  73. $i = 0;
  74. foreach ($nodes as $node) {
  75. // remove masqué
  76. $sid = WorkflowManager::getCurrentStateId($node, 'field_workflow');
  77. if($sid != 'corpus_documents_publie') continue;
  78. $lastdocs[] = $node;
  79. $i++;
  80. if($i>9) break;
  81. }
  82. // $contents["#lastdocs_items"] = entity_load_multiple('node', $lastdocs);
  83. $contents["#lastdocs_items"] = $lastdocs;
  84. // dsm($contents["#lastdocs_items"], "#lastdocs_items");
  85. // agenda
  86. $now = new DrupalDateTime('now');
  87. $now->setTimezone(new \DateTimeZone(DATETIME_STORAGE_TIMEZONE));
  88. $query = \Drupal::entityQuery('node')
  89. ->condition('status', 1)
  90. ->condition('type', 'evenement')
  91. ->condition('field_date', $now->format(DATETIME_DATETIME_STORAGE_FORMAT), '>=')
  92. ->range(0,5)
  93. ->sort('field_date');
  94. $events = $query->execute();
  95. $contents['#agenda_items'] = entity_load_multiple('node', $events);
  96. return $contents;
  97. }
  98. /**
  99. * Display home mobile as a page.
  100. *
  101. * @return renderable array
  102. */
  103. public function toMobileHomeRenderable() {
  104. // $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
  105. $renderable = array("#theme"=>'edlp_home');
  106. // first get static pages
  107. $query = \Drupal::entityQuery('node')
  108. ->condition('status', 1)
  109. ->condition('field_afficher_en_page_d_acceuil', 1)
  110. ->condition('type', 'static');
  111. $promoted_nids = $query->execute();
  112. $renderable["#promoted_statics"] = entity_load_multiple('node', $promoted_nids);
  113. // then get production pages
  114. $query = \Drupal::entityQuery('node')
  115. ->condition('status', 1)
  116. ->condition('field_afficher_en_page_d_acceuil', 1)
  117. ->condition('type', 'page');
  118. $promoted_nids = $query->execute();
  119. $renderable["#promoted_prods"] = entity_load_multiple('node', $promoted_nids);
  120. // last fil
  121. // $query = \Drupal::entityQuery('node')
  122. // ->condition('status', 1)
  123. // ->condition('type', 'fil')
  124. // ->sort('created', 'DESC')
  125. // ->range(0,1);
  126. //
  127. // $fil = $query->execute();
  128. // $renderable["#last_fil_node"] = entity_load('node', array_pop($fil));
  129. // $renderable["#last_fil_node"] = array('#markup'=>'En développement.');
  130. // agenda
  131. /*
  132. $now = new DrupalDateTime('now');
  133. $now->setTimezone(new \DateTimeZone(DATETIME_STORAGE_TIMEZONE));
  134. $query = \Drupal::entityQuery('node')
  135. ->condition('status', 1)
  136. ->condition('type', 'evenement')
  137. ->condition('field_date', $now->format(DATETIME_DATETIME_STORAGE_FORMAT), '>=')
  138. ->range(0,5)
  139. ->sort('field_date');
  140. $events = $query->execute();
  141. $renderable['#agenda_items'] = entity_load_multiple('node', $events);
  142. */
  143. // Collection
  144. // TODO: get the link to mobile collection page
  145. $collection_url = Url::fromRoute('edlp_corpus.collection');
  146. $renderable['#collection_link'] = array(
  147. 'url' => $collection_url,
  148. 'internal_path' => $collection_url->getInternalPath(),
  149. );
  150. // Production
  151. $production_url = Url::fromRoute('edlp_productions.productions');
  152. $renderable['#production_link'] = array(
  153. 'url' => $production_url,
  154. 'internal_path' => $production_url->getInternalPath(),
  155. );
  156. // Agenda
  157. $agenda_url = Url::fromRoute('edlp_agenda.agenda');
  158. $renderable['#agenda_link'] = array(
  159. 'url' => $agenda_url,
  160. 'internal_path' => $agenda_url->getInternalPath(),
  161. );
  162. return $renderable;
  163. }
  164. public function home_mobile() {
  165. return $this->toMobileHomeRenderable();
  166. }
  167. /**
  168. * Get home mobile data as json through ajax.
  169. *
  170. * @return json
  171. */
  172. // NOT NEEDED ANY MORE
  173. public function home_mobilejson() {
  174. $renderable = $this->toMobileHomeRenderable();
  175. // $rendered = render($renderable);
  176. // We can't render directly the entity as it throw an exception with cachable data
  177. //http://blog.dcycle.com/blog/2018-01-24/caching-drupal-8-rest-resource/#the-dreaded-leaked-metadata-error
  178. $rendered = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($renderable) {
  179. return render($renderable);
  180. });
  181. $data = [
  182. 'rendered'=> $rendered,
  183. 'title'=>'Home Mobile',
  184. ];
  185. // translations links
  186. // use Drupal\Core\Url;
  187. // use Drupal\Core\Language\LanguageInterface;
  188. $route_name = 'edlp_home.home_mobile';
  189. $links = \Drupal::languageManager()->getLanguageSwitchLinks(LanguageInterface::TYPE_URL, Url::fromRoute($route_name));
  190. if (isset($links->links)) {
  191. $translations_build = [
  192. '#theme' => 'links__language_block',
  193. '#links' => $links->links,
  194. '#attributes' => ['class' => ["language-switcher-{$links->method_id}",],],
  195. '#set_active_class' => TRUE,
  196. ];
  197. $translations_rendered = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($translations_build) {return render($translations_build);});
  198. $data['translations_links'] = $translations_rendered;
  199. }
  200. $data['#cache'] = [
  201. 'max-age' => \Drupal\Core\Cache\Cache::PERMANENT,
  202. 'tags' => ['edlp-home-cache'],
  203. 'contexts' => [
  204. 'languages:language_content'
  205. ]
  206. ];
  207. // $response = new JsonResponse();
  208. // $response->setData($data);
  209. $response = new CacheableJsonResponse($data);
  210. $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data));
  211. $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($renderable));
  212. return $response;
  213. }
  214. }