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_nodes"] = 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_nodes"] += 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_nodes"] = 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_nodes"] += 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(), ); 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; } }