HomeController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_nodes"] = 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_nodes"] += 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_nodes"] = 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_nodes"] += 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. $now = new DrupalDateTime('now');
  132. $now->setTimezone(new \DateTimeZone(DATETIME_STORAGE_TIMEZONE));
  133. $query = \Drupal::entityQuery('node')
  134. ->condition('status', 1)
  135. ->condition('type', 'evenement')
  136. ->condition('field_date', $now->format(DATETIME_DATETIME_STORAGE_FORMAT), '>=')
  137. ->range(0,5)
  138. ->sort('field_date');
  139. $events = $query->execute();
  140. $renderable['#agenda_items'] = entity_load_multiple('node', $events);
  141. // Collection
  142. // TODO: get the link to mobile collection page
  143. $collection_url = Url::fromRoute('edlp_corpus.collection');
  144. $renderable['#collection_link'] = array(
  145. 'url' => $collection_url,
  146. 'internal_path' => $collection_url->getInternalPath(),
  147. );
  148. return $renderable;
  149. }
  150. public function home_mobile() {
  151. return $this->toMobileHomeRenderable();
  152. }
  153. /**
  154. * Get home mobile data as json through ajax.
  155. *
  156. * @return json
  157. */
  158. // NOT NEEDED ANY MORE
  159. public function home_mobilejson() {
  160. $renderable = $this->toMobileHomeRenderable();
  161. // $rendered = render($renderable);
  162. // We can't render directly the entity as it throw an exception with cachable data
  163. //http://blog.dcycle.com/blog/2018-01-24/caching-drupal-8-rest-resource/#the-dreaded-leaked-metadata-error
  164. $rendered = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($renderable) {
  165. return render($renderable);
  166. });
  167. $data = [
  168. 'rendered'=> $rendered,
  169. 'title'=>'Home Mobile',
  170. ];
  171. // translations links
  172. // use Drupal\Core\Url;
  173. // use Drupal\Core\Language\LanguageInterface;
  174. $route_name = 'edlp_home.home_mobile';
  175. $links = \Drupal::languageManager()->getLanguageSwitchLinks(LanguageInterface::TYPE_URL, Url::fromRoute($route_name));
  176. if (isset($links->links)) {
  177. $translations_build = [
  178. '#theme' => 'links__language_block',
  179. '#links' => $links->links,
  180. '#attributes' => ['class' => ["language-switcher-{$links->method_id}",],],
  181. '#set_active_class' => TRUE,
  182. ];
  183. $translations_rendered = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($translations_build) {return render($translations_build);});
  184. $data['translations_links'] = $translations_rendered;
  185. }
  186. $data['#cache'] = [
  187. 'max-age' => \Drupal\Core\Cache\Cache::PERMANENT,
  188. 'tags' => ['edlp-home-cache'],
  189. 'contexts' => [
  190. 'languages:language_content'
  191. ]
  192. ];
  193. // $response = new JsonResponse();
  194. // $response->setData($data);
  195. $response = new CacheableJsonResponse($data);
  196. $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data));
  197. $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($renderable));
  198. return $response;
  199. }
  200. }