langcode = \Drupal::languageManager()->getCurrentLanguage()->getId(); $entity = entity_load($this->entity_type, $this->id); if ($entity->hasTranslation($this->langcode)){ $this->entity = $entity->getTranslation($this->langcode); }else{ $this->entity = $entity; } if($this->entity){ switch($this->entity_type){ case 'node': $this->bundle = $this->entity->getType(); $this->title = $this->entity->getTitle(); break; case 'taxonomy_term': $this->bundle = $this->entity->bundle(); $this->title = $this->entity->getName(); break; default: $this->bundle = NULL; $this->title = ''; } } } 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', "#attributes"=>array( "class"=>['agenda'] ) ); $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, 'aside'); } $aside['future_events'] = array( "#type"=>"container", "#attributes"=>array( "class"=>['future-events'] ), "#markup"=>"

" . t("Upcoming events") . "

", "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, 'aside'); } $aside['past_events'] = array( "#type"=>"container", "#attributes"=>array( "class"=>['past-events'] ), "#markup"=>"

" . t("Past events") . "

", "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(){ $this->query(); if($this->entity){ return array( "#theme"=>'edlp_ajax', "#entity_type" => $this->entity_type, '#entity' => $this->entity, '#view_mode' => $this->viewmode, '#aside' => $this->getAside(), '#bundle' => $this->bundle, ); }else{ return array( '#markup'=>'404! '.$this->entity_type.' '.$this->id." not found!" ); } } /** * Get entity as json through ajax. * * @return json */ public function entityjson($entity_type, $id, $viewmode) { $this->entity_type = $entity_type; $this->id = $id; $this->viewmode = $viewmode; $renderable = $this->toRenderable(); // $rendered = render($renderable); // We can't render directly the entity as it throw an exception with cachable data // see 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); }); $title = $this->title; $title .= $this->viewmode != '' && $this->viewmode != 'default' ? ' | ' . $this->viewmode : ''; $data = [ 'date' => time(), 'id' => $this->id, 'title' => $title, 'view_mode' => $this->viewmode, 'bundle' => $this->bundle, 'entity_type' => $this->entity_type, 'rendered'=> $rendered, 'language' => $this->langcode ]; // if content type page (productions) : // -- get the menu items and linked dates // -- get the linked documents for corpus map 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'); $menu_parents = array(); foreach ($links as $link) { $parents = $menuLinkManager->getParentIds($link->getPluginId()); // dpm($parents, 'parents'); foreach ($parents as $parent_id) { $parent = $menuLinkManager->getInstance(array('id'=>$parent_id)); // dpm($parent, 'parent'); $url = $parent->getUrlObject(); $sys_path = $url->getInternalPath(); // dpm($sys_path); $menu_parents[] = $sys_path; } } $data['menu_parents'] = $menu_parents; // Documents liƩs $documents_lies = $this->entity->get('field_documents_lies')->getValue(); if(count($documents_lies)){ foreach ($documents_lies as $key => $field) { $data['documents_lies'][] = $field['target_id']; } } } // translations links $links = \Drupal::languageManager()->getLanguageSwitchLinks(LanguageInterface::TYPE_URL, $this->entity->toUrl()); 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);}); // dpm($links); $data['translations_links'] = $translations_rendered; } $data['#cache'] = [ 'max-age' => \Drupal\Core\Cache\Cache::PERMANENT, 'tags' => [ 'edlp-ajax-cache', // $this->entity_type.':'.$this->id // not necessary as we add $renderable as CacheableMetadata to the response ], 'contexts' => [ 'languages:language_content', 'user' ] ]; // $response = new JsonResponse(); // $response->setData($data); $response = new CacheableJsonResponse($data); $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($renderable)); $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data)); return $response; // dpm($response); // return array( // '#markup'=>'hello' // ); } /** * Get all visisble blocks from edlptheme as json through ajax. * * @return json */ // NOT USED (YET) public function blocksjson($blockid) { $block_viewbuilder = \Drupal::entityTypeManager()->getViewBuilder('block'); // $blocksids = []; if(!$blockid){ // TODO: get all blocks definition from edlptheme $block_storage = \Drupal::entityTypeManager()->getStorage('block'); // dpm($block_storage, '$block_storage'); $allblocks = $block_storage->loadMultiple(); // dpm($blocks, 'blocks'); foreach ($allblocks as $block) { // dpm($block->id(), 'block'); // dpm($block->getTheme(), 'theme'); // $visibility = $block->getVisibility(); if($block->getTheme() == "edlptheme"){ // dpm($block, $block->id()); $blocksids[] = $block->id(); } } // $block_listbuilder = \Drupal::entityTypeManager()->getListBuilder('block'); // dpm($block_listbuilder, '$block_listbuilder'); // // foreach ($blocks as $key => $value) { // // $block = \Drupal\block\Entity\Block::load('productions'); // // } } else{ $blocksids[] = $blockid; } // dpm($blocksids, 'blockids'); // $blocks = []; foreach ($blocksids as $id) { $block = Block::load($id); $block_render = $block_viewbuilder->view($block); $rendered = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($block_render) { return render($block_render); }); $blocks[$id] = array( 'rendered'=> $rendered, 'region'=> str_replace('_', '-', $block->getRegion()), 'id'=> preg_replace('/^[^:]+:(.+)$/', 'block-$1', $block->getPluginId()) ); } // dpm($blocks, 'blocks'); // $data = [ 'blockid'=>$blockid, 'blocks' => $blocks, '#cache' => [ 'max-age' => \Drupal\Core\Cache\Cache::PERMANENT, 'tags' => [ 'edlp-ajax-blocks-cache', // $this->entity_type.':'.$this->id // not necessary as we add $renderable as CacheableMetadata to the response ] ] ]; // dpm($data); // $response = new CacheableJsonResponse($data); // $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($renderable)); $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data)); // return $response; // dpm($response); // return array( '#markup'=>'hello' ); } }