index general, fixe #541

This commit is contained in:
2019-11-13 13:21:37 +01:00
parent 28f4ba143a
commit b72c6da623
19 changed files with 443 additions and 136 deletions
@@ -55,6 +55,7 @@ function edlp_ajax_page_attachments(array &$attachments) {
|| preg_match('/^\/?agenda/', $current_path)
|| preg_match('/^\/?studio/', $current_path)
|| preg_match('/^\/?search/', $current_path)
|| preg_match('/^\/?docsindex/', $current_path)
|| preg_match('/^\/?lastdocs/', $current_path)
|| preg_match('/^\/?articles/', $current_path)){
$redirect = true;
@@ -25,9 +25,17 @@ function edlp_corpus_theme($existing, $type, $theme, $path) {
// 'render element' => '',
'file' => 'includes/blockentrees.inc',
'variables' => array(
'docsindex_link' => null,
'entrees_items' => array(),
),
),
'edlp_corpus_docsindex' => array(
// 'render element' => '',
'file' => 'includes/edlp_corpus_docsindex.inc',
'variables' => array(
'docsindex_nodes' => NULL,
),
),
'edlp_corpus_lastdocs' => array(
// 'render element' => '',
'file' => 'includes/edlp_corpus_lastdocs.inc',
@@ -15,6 +15,23 @@ edlp_corpus.corpusjson:
_permission: 'access content'
edlp_corpus.docsindex:
path: '/docsindex'
defaults:
_controller: '\Drupal\edlp_corpus\Controller\CorpusController::docsindex'
_title: 'Docs Index'
requirements:
_permission: 'access content'
edlp_corpus.docsindexajax:
path: '/docsindex/ajax'
defaults:
_controller: '\Drupal\edlp_corpus\Controller\CorpusController::docsindexjson'
_title: 'Docs Index'
requirements:
_permission: 'access content'
edlp_corpus.lastdocs:
path: '/lastdocs'
defaults:
@@ -8,6 +8,11 @@ function template_preprocess_blockentrees(&$vars){
'#items' => array(),
);
$docsindex = $vars['docsindex_link']->toRenderable();
$docsindex['#prefix'] = '<span class="oblique-wrapper">';
$docsindex['#suffix'] = "</span>";
$entrees['#items'][] = $docsindex;
foreach ($vars['entrees_items'] as $entree) {
$tid = $entree['tid'];
@@ -0,0 +1,30 @@
<?php
// use Drupal\Core\Url;
function template_preprocess_edlp_corpus_docsindex(&$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['docsindex_nodes'])){
$docsindex_list = array (
'#theme' => 'item_list',
'#items' => [],
);
foreach($vars['docsindex_nodes'] as $node){
$docsindex_list['#items'][] = $view_builder->view($node, 'docsindex');
}
$vars['docsindex'] = array(
"#type"=>"container",
"#attributes"=>array(
"class"=>['docsindex']
),
"#markup"=>"<h3>".t("Documents Index")."</h3>",
"docsindex"=>$docsindex_list
);
}
}
@@ -150,6 +150,133 @@ class CorpusController extends ControllerBase {
}
// ___ ___ _
// | \ ___ __ __|_ _|_ _ __| |_____ __
// | |) / _ \/ _(_-<| || ' \/ _` / -_) \ /
// |___/\___/\__/__/___|_||_\__,_\___/_\_\
private function docsindexQuery() {
// $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$query = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'enregistrement')
->sort('title', 'ASC');
// ->range(0,50);
$nids = $query->execute();
$nodes = entity_load_multiple('node', $nids);
// $current_langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
$this->docsindex_nodes = [];
$this->docsindex_nids = [];
foreach ($nodes as $node) {
// remove masqué
$sid = WorkflowManager::getCurrentStateId($node, 'field_workflow');
if($sid != 'corpus_documents_publie') continue;
$this->docsindex_nodes[] = $node;
// record an array of nids for corpus map filtering
$this->docsindex_nids[] = $node->get('nid')->getString();
// // translate the node
// $node = \Drupal::service('entity.repository')->getTranslationFromContext($node, $language);
// $title = $this->stripAccent($node->getTitle());
// $ordered_trans_nodes[$title] = $node;
}
// // TODO: sort by title
// ksort($ordered_trans_nodes);
//
// foreach ($ordered_trans_nodes as $title => $node) {
// $this->docsindex_nodes[] = $node;
// // record an array of nids for corpus map filtering
// $this->docsindex_nids[] = $node->get('nid')->getString();
// }
}
// private function stripAccent($str){
// return strtr(
// utf8_decode($str),
// utf8_decode('àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ'),
// 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
// }
private function docsindexToRenderable(){
$this->docsindexQuery();
return array(
"#theme"=>'edlp_corpus_docsindex',
'#docsindex_nodes' => $this->docsindex_nodes
);
}
/**
* Display lastdocs as a page.
*
* @return renderable array
*/
public function docsindex() {
return $this->docsindexToRenderable();
}
/**
* Get lastdocs data as json through ajax.
*
* @return json
*/
public function docsindexjson() {
$renderable = $this->docsindexToRenderable();
// $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'=>t('Documents Index'),
'documents_lies' => $this->docsindex_nids,
];
// translations links
$route_name = 'edlp_corpus.docsindex';
$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-docsindex-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;
}
// _ _ _
// | | __ _ __| |_ __| |___ __ ___
// | |__/ _` (_-< _/ _` / _ \/ _(_-<
@@ -101,8 +101,19 @@ class BlockEntrees extends BlockBase {
$entrees[] = $entree;
}
// add general docs index link
$url = Url::fromRoute('edlp_corpus.docsindex');
$url->setOptions(array(
'attributes' => array(
'class' => ['docsindex-link', 'ajax-link'],
'data-drupal-link-system-path' => $url->getInternalPath()
)
));
$docsindex_link = Link::fromTextAndUrl('Index', $url);
return array (
'#theme' => 'blockentrees',
'#docsindex_link' => $docsindex_link,
'#entrees_items' => $entrees,
'#attached'=>array(
'library' => array('edlp_corpus/corpus'),
@@ -1,2 +1 @@
{{ entrees }}
@@ -0,0 +1 @@
{{ docsindex }}