added aside (related dates for prod) to edlp_ajax controller

This commit is contained in:
2018-04-15 17:11:54 +02:00
parent e27d1f1e5a
commit 4967111a0e
5 changed files with 117 additions and 14 deletions
@@ -95,7 +95,8 @@ function edlp_ajax_theme($existing, $type, $theme, $path) {
'variables' => array(
'entity_type' => 'node',
'entity' => NULL,
'view_mode' => 'default'
'view_mode' => 'default',
'aside' => array(),
),
),
);
@@ -9,4 +9,5 @@ function template_preprocess_edlp_ajax(&$vars){
*/
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($vars['entity_type']);
$vars['content'] = $view_builder->view($vars['entity'], $vars['view_mode']);
}
@@ -7,6 +7,8 @@ use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Url;
use Drupal\Core\Language\LanguageInterface;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\Core\Datetime\DrupalDateTime;
// use Symfony\Component\HttpFoundation\JsonResponse;
use \Drupal\block\Entity\Block;
use Drupal\Core\Cache\CacheableJsonResponse;
@@ -18,6 +20,104 @@ class EdlpAjaxController extends ControllerBase {
private function query() {
$this->entity = entity_load($this->entity_type, $this->id);
if($this->entity){
switch($this->entity_type){
case 'node':
$this->bundle = $this->entity->getType();
break;
case 'taxonomy_term':
$this->bundle = $this->entity->bundle();
break;
default:
$this->bundle = NULL;
}
}
}
private function getProductionDatesAside(){
$now = new DrupalDateTime('now');
$now->setTimezone(new \DateTimeZone(DATETIME_STORAGE_TIMEZONE));
$future_dates_query = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'evenement')
->condition('field_page_liee.target_id', $this->entity->id(), 'IN')
->condition('field_date', $now->format(DATETIME_DATETIME_STORAGE_FORMAT), '>=')
->sort('field_date');
$future_nids = $future_dates_query->execute();
$past_dates_query = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'evenement')
->condition('field_page_liee.target_id', $this->entity->id(), 'IN')
->condition('field_date', $now->format(DATETIME_DATETIME_STORAGE_FORMAT), '<')
->sort('field_date', 'DESC');
$past_nids = $past_dates_query->execute();
if(count($future_nids) || count($past_nids)){
$aside = array(
'#type'=>'container'
);
$node_view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
if(count($future_nids)){
$future_nodes = entity_load_multiple('node', $future_nids);
$future_list = array (
'#theme' => 'item_list',
'#items' => [],
);
foreach($future_nodes as $node){
$future_list['#items'][] = $node_view_builder->view($node, 'teaser');
}
$aside['future_events'] = array(
"#type"=>"container",
"#attributes"=>array(
"class"=>['future-events']
),
"#markup"=>"<h3>" . t("Future events") . "</h3>",
"future_events"=>$future_list
);
}
if(count($past_nids)){
$past_nodes = entity_load_multiple('node', $past_nids);
$past_list = array (
'#theme' => 'item_list',
'#items' => [],
);
foreach($past_nodes as $node){
$past_list['#items'][] = $node_view_builder->view($node, 'teaser');
}
$aside['past_events'] = array(
"#type"=>"container",
"#attributes"=>array(
"class"=>['future-events']
),
"#markup"=>"<h3>" . t("Past events") . "</h3>",
"past_events"=>$past_list
);
}
}else{
$aside = null;
}
return $aside;
}
/**
*
* return a renderable array
*/
private function getAside() {
if($this->bundle == 'page'){
$aside = $this->getProductionDatesAside();
}else{
$aside = null;
}
return $aside;
}
private function toRenderable(){
@@ -29,6 +129,7 @@ class EdlpAjaxController extends ControllerBase {
"#entity_type" => $this->entity_type,
'#entity' => $this->entity,
'#view_mode' => $this->viewmode,
'#aside' => $this->getAside(),
);
}else{
return array(
@@ -55,28 +156,18 @@ class EdlpAjaxController extends ControllerBase {
return render($renderable);
});
switch($this->entity_type){
case 'node':
$bundle = $this->entity->getType();
break;
case 'taxonomy_term':
$bundle = $this->entity->bundle();
break;
default:
$bundle = NULL;
}
$data = [
'date' => time(),
'id' => $this->id,
'view_mode' => $this->viewmode,
'bundle' => $bundle,
'bundle' => $this->bundle,
'entity_type' => $this->entity_type,
'rendered'=> $rendered,
];
// if content type page (productions) get the menu items
if($bundle == "page"){
// if content type page (productions) get the menu items and linked dates
if($this->bundle == "page"){
$menuLinkManager = \Drupal::service('plugin.manager.menu.link');
$links = $menuLinkManager->loadLinksByRoute('entity.node.canonical', array('node' => $this->id), 'productions');
// dpm($links, 'links');
@@ -1 +1,2 @@
{{ content }}
{{ aside }}
@@ -5,3 +5,12 @@
{#</div>#}
</div>
</div>
{% if aside %}
<div class="col small-col-12 med-col-6 large-col-6 aside">
<div class="wrapper">
{#<div class="os-scroll">#}
{{ aside }}
{#</div>#}
</div>
</div>
{% endif %}