created lastdocs home block and corresponfing page

This commit is contained in:
2018-09-14 16:04:48 +02:00
parent 03ab172f42
commit 6ca52c635e
23 changed files with 479 additions and 19 deletions
@@ -4,7 +4,7 @@
function template_preprocess_edlp_agenda(&$vars){
// dpm($vars);
/*
/*
@see https://www.drupal8.ovh/index.php/en/tutoriels/339/render-a-node-or-an-entity
*/
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
@@ -54,7 +54,8 @@ function edlp_ajax_page_attachments(array &$attachments) {
|| preg_match('/^\/?productions/', $current_path)
|| preg_match('/^\/?agenda/', $current_path)
|| preg_match('/^\/?studio/', $current_path)
|| preg_match('/^\/?search/', $current_path)){
|| preg_match('/^\/?search/', $current_path)
|| preg_match('/^\/?lastdocs/', $current_path)){
$redirect = true;
}
@@ -23,11 +23,18 @@ function edlp_corpus_theme($existing, $type, $theme, $path) {
return array(
'blockentrees' => array(
// 'render element' => '',
'file' => 'blockentrees.inc',
'file' => 'includes/blockentrees.inc',
'variables' => array(
'entrees_items' => array(),
),
),
'edlp_corpus_lastdocs' => array(
// 'render element' => '',
'file' => 'includes/edlp_corpus_lastdocs.inc',
'variables' => array(
'lastdocs_nodes' => NULL,
),
),
);
}
@@ -13,3 +13,20 @@ edlp_corpus.corpusjson:
_title: 'Corpus'
requirements:
_permission: 'access content'
edlp_corpus.lastdocs:
path: '/lastdocs'
defaults:
_controller: '\Drupal\edlp_corpus\Controller\CorpusController::lastdocs'
_title: 'Last Docs'
requirements:
_permission: 'access content'
edlp_corpus.lastdocsajax:
path: '/lastdocs/ajax'
defaults:
_controller: '\Drupal\edlp_corpus\Controller\CorpusController::lastdocsjson'
_title: 'Last docs'
requirements:
_permission: 'access content'
@@ -0,0 +1,30 @@
<?php
// use Drupal\Core\Url;
function template_preprocess_edlp_corpus_lastdocs(&$vars){
// dpm($vars);
/*
@see https://www.drupal8.ovh/index.php/en/tutoriels/339/render-a-node-or-an-entity
*/
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
if(count($vars['lastdocs_nodes'])){
$lastdocs_list = array (
'#theme' => 'item_list',
'#items' => [],
);
foreach($vars['lastdocs_nodes'] as $node){
$lastdocs_list['#items'][] = $view_builder->view($node, 'lastdocs');
}
$vars['lastdocs'] = array(
"#type"=>"container",
"#attributes"=>array(
"class"=>['lastdocs']
),
"#markup"=>"<h3>".t("Last Documents")."</h3>",
"lastdocs"=>$lastdocs_list
);
}
}
@@ -148,4 +148,99 @@ class CorpusController extends ControllerBase {
);
}
// _ _ _
// | | __ _ __| |_ __| |___ __ ___
// | |__/ _` (_-< _/ _` / _ \/ _(_-<
// |____\__,_/__/\__\__,_\___/\__/__/
private function query() {
$query = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'enregistrement')
->sort('created', 'DESC')
->range(0,20);
$nids = $query->execute();
$this->lastdocs_nodes = entity_load_multiple('node', $nids);
// record an array of nids for corpus map filtering
$this->lastdocs_nids = [];
foreach($nids as $key => $nid){
$this->lastdocs_nids[] = $nid;
}
}
private function toRenderable(){
$this->query();
// dpm($this->next_event_node);
return array(
"#theme"=>'edlp_corpus_lastdocs',
'#lastdocs_nodes' => $this->lastdocs_nodes
);
}
/**
* Display lastdocs as a page.
*
* @return renderable array
*/
public function lastdocs() {
return $this->toRenderable();
}
/**
* Get lastdocs data as json through ajax.
*
* @return json
*/
public function lastdocsjson() {
$renderable = $this->toRenderable();
// $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'=>'Last Documents',
'documents_lies' => $this->lastdocs_nids,
];
// translations links
// use Drupal\Core\Url;
// use Drupal\Core\Language\LanguageInterface;
$route_name = 'edlp_corpus.lastdocs';
$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-lastdocs-cache']
];
// $response = new JsonResponse();
// $response->setData($data);
$response = new CacheableJsonResponse($data);
$response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data));
$response->addCacheableDependency(CacheableMetadata::createFromRenderArray($renderable));
return $response;
}
}
@@ -0,0 +1 @@
{{ lastdocs }}
@@ -23,6 +23,7 @@ function edlp_home_theme($existing, $type, $theme, $path) {
// 'last_fil_node' => NULL,
// 'last_production_node' => NULL,
'promoted_nodes' => array(),
'lastdocs_items' => NULL,
'agenda_items' => NULL,
'entrees_items' => NULL,
),
@@ -93,6 +93,34 @@ function template_preprocess_edlp_home(&$vars){
// )
// );
// render the lasts documents of collection as list
$lastdocs_url = Url::fromRoute('edlp_corpus.lastdocs');
$lastdocs = array(
'#type'=>"container",
'title'=>array(
'#prefix'=> '<h3>',
'#title' => t("Last documents"),
'#suffix' => '</h3>',
'#type' => 'link',
'#url' => $lastdocs_url,
'#options'=>array(
'attributes' => array(
'data-drupal-link-system-path' => $lastdocs_url->getInternalPath(),
'class' => array('ajax-link'),
)
)
),
'list'=> array(
'#theme' => 'item_list',
'#items' => [],
),
);
foreach($vars['lastdocs_items'] as $node){
$lastdocs['list']['#items'][] = $node_view_builder->view($node, 'search_index');
}
$vars['lastdocs'] = render($lastdocs);
// render the next events of agenda as list
$agenda_url = Url::fromRoute('edlp_agenda.agenda');
$agenda = array(
@@ -72,6 +72,17 @@ class HomeController extends ControllerBase {
// $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,10);
$lastdocs = $query->execute();
$contents["#lastdocs_items"] = entity_load_multiple('node', $lastdocs);
// dsm($contents["#lastdocs_items"], "#lastdocs_items");
// agenda
$now = new DrupalDateTime('now');
$now->setTimezone(new \DateTimeZone(DATETIME_STORAGE_TIMEZONE));
@@ -5,6 +5,8 @@
{{ node.build }}
{% endfor %}
{{ lastdocs }}
{{ agenda }}
{% if entrees %}