|
@@ -7,6 +7,7 @@ use Drupal\Core\Language\LanguageInterface;
|
|
|
use Drupal\workflow\Entity\WorkflowManager;
|
|
|
use Drupal\Core\Url;
|
|
|
use Drupal\File\Entity\File;
|
|
|
+use Drupal\taxonomy\Entity\Term;
|
|
|
// use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
use Drupal\Core\Cache\CacheableJsonResponse;
|
|
|
use Drupal\Core\Cache\CacheableMetadata;
|
|
@@ -251,107 +252,199 @@ class CorpusController extends ControllerBase {
|
|
|
// /_/ \_\_| \__|_\__|_\___/__/ |___|_||_\__,_\___/_\_\
|
|
|
|
|
|
|
|
|
- private function articlesQuery() {
|
|
|
- $query = \Drupal::entityQuery('node')
|
|
|
- ->condition('status', 1)
|
|
|
- ->condition('type', 'enregistrement')
|
|
|
- ->condition('body', '', "<>")
|
|
|
- ->sort('created', 'DESC');
|
|
|
- // ->range(0,20);
|
|
|
+ private function articlesQuery() {
|
|
|
+ $query = \Drupal::entityQuery('node')
|
|
|
+ ->condition('status', 1)
|
|
|
+ ->condition('type', 'enregistrement')
|
|
|
+ ->condition('body', '', "<>")
|
|
|
+ ->sort('created', 'DESC');
|
|
|
+ // ->range(0,20);
|
|
|
+
|
|
|
+ $nids = $query->execute();
|
|
|
+ $nodes = entity_load_multiple('node', $nids);
|
|
|
+
|
|
|
+ $current_langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
|
|
|
|
|
- $nids = $query->execute();
|
|
|
- $nodes = entity_load_multiple('node', $nids);
|
|
|
+ $this->articles_nodes = [];
|
|
|
+ $this->articles_nids = [];
|
|
|
+ foreach ($nodes as $node) {
|
|
|
+ // remove masqué
|
|
|
+ $sid = WorkflowManager::getCurrentStateId($node, 'field_workflow');
|
|
|
+ if($sid != 'corpus_documents_publie') continue;
|
|
|
|
|
|
- $current_langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
|
|
+ // TODO: check if article is translated
|
|
|
+ if ($node->getTranslation($current_langcode)->body->isEmpty()) continue;
|
|
|
|
|
|
- $this->articles_nodes = [];
|
|
|
- $this->articles_nids = [];
|
|
|
- foreach ($nodes as $node) {
|
|
|
- // remove masqué
|
|
|
- $sid = WorkflowManager::getCurrentStateId($node, 'field_workflow');
|
|
|
- if($sid != 'corpus_documents_publie') continue;
|
|
|
+ $this->articles_nodes[] = $node;
|
|
|
+ // record an array of nids for corpus map filtering
|
|
|
+ $this->articles_nids[] = $node->get('nid')->getString();
|
|
|
+ }
|
|
|
|
|
|
- // TODO: check if article is translated
|
|
|
- if ($node->getTranslation($current_langcode)->body->isEmpty()) continue;
|
|
|
+ }
|
|
|
|
|
|
- $this->articles_nodes[] = $node;
|
|
|
- // record an array of nids for corpus map filtering
|
|
|
- $this->articles_nids[] = $node->get('nid')->getString();
|
|
|
- }
|
|
|
+ private function articlesToRenderable(){
|
|
|
+ $this->articlesQuery();
|
|
|
+ // dpm($this->next_event_node);
|
|
|
+
|
|
|
+ return array(
|
|
|
+ "#theme"=>'edlp_corpus_articlesindex',
|
|
|
+ '#articles_nodes' => $this->articles_nodes
|
|
|
+ );
|
|
|
+
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * Display lastdocs as a page.
|
|
|
+ *
|
|
|
+ * @return renderable array
|
|
|
+ */
|
|
|
+ public function articlesindex() {
|
|
|
+ return $this->articlesToRenderable();
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * Get lastdocs data as json through ajax.
|
|
|
+ *
|
|
|
+ * @return json
|
|
|
+ */
|
|
|
+ public function articlesindexjson() {
|
|
|
+
|
|
|
+ $renderable = $this->articlesToRenderable();
|
|
|
+ // $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'=>'Articles',
|
|
|
+ 'articles' => $this->articles_nids,
|
|
|
+ 'documents_lies' => $this->articles_nids,
|
|
|
+ ];
|
|
|
+
|
|
|
+ // translations links
|
|
|
+ // use Drupal\Core\Url;
|
|
|
+ // use Drupal\Core\Language\LanguageInterface;
|
|
|
+ $route_name = 'edlp_corpus.articlesindex';
|
|
|
+ $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;
|
|
|
}
|
|
|
|
|
|
- private function articlesToRenderable(){
|
|
|
- $this->articlesQuery();
|
|
|
- // dpm($this->next_event_node);
|
|
|
-
|
|
|
- return array(
|
|
|
- "#theme"=>'edlp_corpus_articlesindex',
|
|
|
- '#articles_nodes' => $this->articles_nodes
|
|
|
- );
|
|
|
-
|
|
|
- }
|
|
|
- /**
|
|
|
- * Display lastdocs as a page.
|
|
|
- *
|
|
|
- * @return renderable array
|
|
|
- */
|
|
|
- public function articlesindex() {
|
|
|
- return $this->articlesToRenderable();
|
|
|
+ $data['#cache'] = [
|
|
|
+ 'max-age' => \Drupal\Core\Cache\Cache::PERMANENT,
|
|
|
+ 'tags' => ['edlp-articlesindex-cache']
|
|
|
+ ];
|
|
|
+
|
|
|
+ // $response = new JsonResponse();
|
|
|
+ // $response->setData($data);
|
|
|
+ $response = new CacheableJsonResponse($data);
|
|
|
+ $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data));
|
|
|
+ $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($renderable));
|
|
|
+
|
|
|
+ return $response;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// ___ _ _ _ _
|
|
|
+// / __|___| | |___ __| |_(_)___ _ _
|
|
|
+// | (__/ _ \ | / -_) _| _| / _ \ ' \
|
|
|
+// \___\___/_|_\___\__|\__|_\___/_||_|
|
|
|
+
|
|
|
+ private function collectionQuery(){
|
|
|
+ $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
|
|
+
|
|
|
+ $query = \Drupal::entityQuery('taxonomy_term')
|
|
|
+ // ->sort('weight', 'DESC')
|
|
|
+ // ->sort('name', 'DESC')
|
|
|
+ ->condition('vid', 'entrees');
|
|
|
+
|
|
|
+ $tids = $query->execute();
|
|
|
+ // $terms = entity_load_multiple('taxonomy_term', $tids);
|
|
|
+ // $terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadMultiple($terms);
|
|
|
+ $terms = Term::loadMultiple($tids);
|
|
|
+
|
|
|
+ $ordered_terms = [];
|
|
|
+ foreach ($terms as $term) {
|
|
|
+ // remove masqué
|
|
|
+ $sid = WorkflowManager::getCurrentStateId($term, 'field_workflow');
|
|
|
+ if($sid == 'generique_masque') continue;
|
|
|
+ // translate the term
|
|
|
+ $term = \Drupal::service('entity.repository')->getTranslationFromContext($term, $language);
|
|
|
+ $name = $term->getName();
|
|
|
+ $ordered_trans_terms[$name] = $term;
|
|
|
}
|
|
|
+ ksort($ordered_trans_terms);
|
|
|
+ $this->entrees_terms = $ordered_trans_terms;
|
|
|
|
|
|
- /**
|
|
|
- * Get lastdocs data as json through ajax.
|
|
|
- *
|
|
|
- * @return json
|
|
|
- */
|
|
|
- public function articlesindexjson() {
|
|
|
-
|
|
|
- $renderable = $this->articlesToRenderable();
|
|
|
- // $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'=>'Articles',
|
|
|
- 'articles' => $this->articles_nids,
|
|
|
- 'documents_lies' => $this->articles_nids,
|
|
|
- ];
|
|
|
+ private function collectionToRenderable(){
|
|
|
+ $this->collectionQuery();
|
|
|
|
|
|
- // translations links
|
|
|
- // use Drupal\Core\Url;
|
|
|
- // use Drupal\Core\Language\LanguageInterface;
|
|
|
- $route_name = 'edlp_corpus.articlesindex';
|
|
|
- $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;
|
|
|
- }
|
|
|
+ return array(
|
|
|
+ "#theme"=>'edlp_corpus_collection',
|
|
|
+ '#entrees_terms' => $this->entrees_terms
|
|
|
+ );
|
|
|
+ }
|
|
|
|
|
|
- $data['#cache'] = [
|
|
|
- 'max-age' => \Drupal\Core\Cache\Cache::PERMANENT,
|
|
|
- 'tags' => ['edlp-articlesindex-cache']
|
|
|
- ];
|
|
|
+ public function collection(){
|
|
|
+ return $this->collectionToRenderable();
|
|
|
+ }
|
|
|
|
|
|
- // $response = new JsonResponse();
|
|
|
- // $response->setData($data);
|
|
|
- $response = new CacheableJsonResponse($data);
|
|
|
- $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data));
|
|
|
- $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($renderable));
|
|
|
+ public function collectionjson(){
|
|
|
+ $renderable = $this->collectionToRenderable();
|
|
|
+ // $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'=>'Collection'
|
|
|
+ ];
|
|
|
|
|
|
- return $response;
|
|
|
+ // translations links
|
|
|
+ // use Drupal\Core\Url;
|
|
|
+ // use Drupal\Core\Language\LanguageInterface;
|
|
|
+ $route_name = 'edlp_corpus.collection';
|
|
|
+ $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-articlesindex-cache']
|
|
|
+ ];
|
|
|
+
|
|
|
+ // $response = new JsonResponse();
|
|
|
+ // $response->setData($data);
|
|
|
+ $response = new CacheableJsonResponse($data);
|
|
|
+ $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data));
|
|
|
+ $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($renderable));
|
|
|
+
|
|
|
+ return $response;
|
|
|
+ }
|
|
|
|
|
|
}
|