123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <?php
- namespace Drupal\edlp_home\Controller;
- use Drupal\Core\Controller\ControllerBase;
- use Drupal\Core\Datetime\DrupalDateTime;
- use Drupal\taxonomy\Entity\Term;
- use Drupal\workflow\Entity\WorkflowManager;
- use Drupal\Core\Url;
- use Drupal\Core\Language\LanguageInterface;
- // use Symfony\Component\HttpFoundation\JsonResponse;
- use Drupal\Core\Cache\CacheableJsonResponse;
- use Drupal\Core\Cache\CacheableMetadata;
- use Drupal\core\render\RenderContext;
- class HomeController extends ControllerBase {
- /**
- * Display home as a page.
- *
- * @return renderable array
- */
- public function home() {
- // $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
- $contents = array("#theme"=>'edlp_home');
- // first get static pages
- $query = \Drupal::entityQuery('node')
- ->condition('status', 1)
- ->condition('field_afficher_en_page_d_acceuil', 1)
- ->condition('type', 'static');
- $promoted_nids = $query->execute();
- $contents["#promoted_statics"] = entity_load_multiple('node', $promoted_nids);
- // then get production pages
- $query = \Drupal::entityQuery('node')
- ->condition('status', 1)
- ->condition('field_afficher_en_page_d_acceuil', 1)
- ->condition('type', 'page');
- $promoted_nids = $query->execute();
- $contents["#promoted_prods"] = entity_load_multiple('node', $promoted_nids);
- // presentation
- // $query = \Drupal::entityQuery('node')
- // ->condition('status', 1)
- // ->condition('nid', 12242);
- // // TODO: présentation nid should be a setting
- //
- // $pres_nid = $query->execute();
- // $contents["#presentation_node"] = entity_load('node', array_pop($pres_nid));
- // last fil
- // $query = \Drupal::entityQuery('node')
- // ->condition('status', 1)
- // ->condition('type', 'fil')
- // ->sort('created', 'DESC')
- // ->range(0,1);
- //
- // $fil = $query->execute();
- // $contents["#last_fil_node"] = entity_load('node', array_pop($fil));
- // $contents["#last_fil_node"] = array('#markup'=>'En développement.');
- // last production
- // $query = \Drupal::entityQuery('node')
- // ->condition('status', 1)
- // ->condition('type', 'page')
- // ->condition('field_page_type', array('1168'), 'NOT IN')
- // ->sort('created', 'DESC')
- // ->range(0,1);
- //
- // $prod = $query->execute();
- // $contents["#last_production_node"] = entity_load('node', array_pop($prod));
- // last documents
- $query = \Drupal::entityQuery('node')
- ->condition('status', 1)
- ->condition('type', 'enregistrement')
- ->sort('created', 'DESC')
- ->range(0,50);
- $nids = $query->execute();
- $nodes = entity_load_multiple('node', $nids);
- $lastdocs = [];
- $i = 0;
- foreach ($nodes as $node) {
- // remove masqué
- $sid = WorkflowManager::getCurrentStateId($node, 'field_workflow');
- if($sid != 'corpus_documents_publie') continue;
- $lastdocs[] = $node;
- $i++;
- if($i>9) break;
- }
- // $contents["#lastdocs_items"] = entity_load_multiple('node', $lastdocs);
- $contents["#lastdocs_items"] = $lastdocs;
- // dsm($contents["#lastdocs_items"], "#lastdocs_items");
- // agenda
- $now = new DrupalDateTime('now');
- $now->setTimezone(new \DateTimeZone(DATETIME_STORAGE_TIMEZONE));
- $query = \Drupal::entityQuery('node')
- ->condition('status', 1)
- ->condition('type', 'evenement')
- ->condition('field_date', $now->format(DATETIME_DATETIME_STORAGE_FORMAT), '>=')
- ->range(0,5)
- ->sort('field_date');
- $events = $query->execute();
- $contents['#agenda_items'] = entity_load_multiple('node', $events);
- return $contents;
- }
- /**
- * Display home mobile as a page.
- *
- * @return renderable array
- */
- public function toMobileHomeRenderable() {
- // $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
- $renderable = array("#theme"=>'edlp_home');
- // first get static pages
- $query = \Drupal::entityQuery('node')
- ->condition('status', 1)
- ->condition('field_afficher_en_page_d_acceuil', 1)
- ->condition('type', 'static');
- $promoted_nids = $query->execute();
- $renderable["#promoted_statics"] = entity_load_multiple('node', $promoted_nids);
- // then get production pages
- $query = \Drupal::entityQuery('node')
- ->condition('status', 1)
- ->condition('field_afficher_en_page_d_acceuil', 1)
- ->condition('type', 'page');
- $promoted_nids = $query->execute();
- $renderable["#promoted_prods"] = entity_load_multiple('node', $promoted_nids);
- // last fil
- // $query = \Drupal::entityQuery('node')
- // ->condition('status', 1)
- // ->condition('type', 'fil')
- // ->sort('created', 'DESC')
- // ->range(0,1);
- //
- // $fil = $query->execute();
- // $renderable["#last_fil_node"] = entity_load('node', array_pop($fil));
- // $renderable["#last_fil_node"] = array('#markup'=>'En développement.');
- // agenda
- /*
- $now = new DrupalDateTime('now');
- $now->setTimezone(new \DateTimeZone(DATETIME_STORAGE_TIMEZONE));
- $query = \Drupal::entityQuery('node')
- ->condition('status', 1)
- ->condition('type', 'evenement')
- ->condition('field_date', $now->format(DATETIME_DATETIME_STORAGE_FORMAT), '>=')
- ->range(0,5)
- ->sort('field_date');
- $events = $query->execute();
- $renderable['#agenda_items'] = entity_load_multiple('node', $events);
- */
- // Collection
- // TODO: get the link to mobile collection page
- $collection_url = Url::fromRoute('edlp_corpus.collection');
- $renderable['#collection_link'] = array(
- 'url' => $collection_url,
- 'internal_path' => $collection_url->getInternalPath(),
- );
- // Production
- $production_url = Url::fromRoute('edlp_productions.productions');
- $renderable['#production_link'] = array(
- 'url' => $production_url,
- 'internal_path' => $production_url->getInternalPath(),
- );
- // Agenda
- $agenda_url = Url::fromRoute('edlp_agenda.agenda');
- $renderable['#agenda_link'] = array(
- 'url' => $agenda_url,
- 'internal_path' => $agenda_url->getInternalPath(),
- );
- return $renderable;
- }
- public function home_mobile() {
- return $this->toMobileHomeRenderable();
- }
- /**
- * Get home mobile data as json through ajax.
- *
- * @return json
- */
- // NOT NEEDED ANY MORE
- public function home_mobilejson() {
- $renderable = $this->toMobileHomeRenderable();
- // $rendered = render($renderable);
- // We can't render directly the entity as it throw an exception with cachable data
- //http://blog.dcycle.com/blog/2018-01-24/caching-drupal-8-rest-resource/#the-dreaded-leaked-metadata-error
- $rendered = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($renderable) {
- return render($renderable);
- });
- $data = [
- 'rendered'=> $rendered,
- 'title'=>'Home Mobile',
- ];
- // translations links
- // use Drupal\Core\Url;
- // use Drupal\Core\Language\LanguageInterface;
- $route_name = 'edlp_home.home_mobile';
- $links = \Drupal::languageManager()->getLanguageSwitchLinks(LanguageInterface::TYPE_URL, Url::fromRoute($route_name));
- if (isset($links->links)) {
- $translations_build = [
- '#theme' => 'links__language_block',
- '#links' => $links->links,
- '#attributes' => ['class' => ["language-switcher-{$links->method_id}",],],
- '#set_active_class' => TRUE,
- ];
- $translations_rendered = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($translations_build) {return render($translations_build);});
- $data['translations_links'] = $translations_rendered;
- }
- $data['#cache'] = [
- 'max-age' => \Drupal\Core\Cache\Cache::PERMANENT,
- 'tags' => ['edlp-home-cache'],
- 'contexts' => [
- 'languages:language_content'
- ]
- ];
- // $response = new JsonResponse();
- // $response->setData($data);
- $response = new CacheableJsonResponse($data);
- $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data));
- $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($renderable));
- return $response;
- }
- }
|