reinstalled all contribs with composer
need to run : drush ev 'drupal_flush_all_caches();' drush cr and restart nginx and php-fpm before loading the website
This commit is contained in:
@@ -0,0 +1,340 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\edlp_ajax\Controller;
|
||||
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
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;
|
||||
use Drupal\Core\Cache\CacheableMetadata;
|
||||
use Drupal\core\render\RenderContext;
|
||||
|
||||
|
||||
class EdlpAjaxController extends ControllerBase {
|
||||
|
||||
private function query() {
|
||||
$this->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"=>"<h3>" . t("Upcoming 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, 'aside');
|
||||
}
|
||||
$aside['past_events'] = array(
|
||||
"#type"=>"container",
|
||||
"#attributes"=>array(
|
||||
"class"=>['past-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(){
|
||||
$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'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\edlp_ajax\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Core\Field\FieldItemInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter;
|
||||
/**
|
||||
* Plugin implementation of the 'entity_reference_label_formatter_ajax' formatter.
|
||||
*
|
||||
* @FieldFormatter(
|
||||
* id = "entity_reference_label_formatter_ajax",
|
||||
* label = @Translation("Label link ajax ready"),
|
||||
* field_types = {
|
||||
* "entity_reference"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class EntityReferenceLabelFormatterAjax extends EntityReferenceLabelFormatter {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return [
|
||||
// Implement default settings.
|
||||
] + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
return [
|
||||
// Implement settings form.
|
||||
] + parent::settingsForm($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
// public function settingsSummary() {
|
||||
// $summary = [];
|
||||
// // Implement settings summary.
|
||||
//
|
||||
// return $summary;
|
||||
// }
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items, $langcode) {
|
||||
$elements = [];
|
||||
$output_as_link = $this->getSetting('link');
|
||||
$current_langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
||||
|
||||
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
|
||||
|
||||
if ($entity->hasTranslation($current_langcode)) {
|
||||
$entity = $entity->getTranslation($current_langcode);
|
||||
}
|
||||
|
||||
$label = $entity->label();
|
||||
// If the link is to be displayed and the entity has a uri, display a
|
||||
// link.
|
||||
if ($output_as_link && !$entity->isNew()) {
|
||||
try {
|
||||
$uri = $entity->urlInfo();
|
||||
$options = $uri->getOptions();
|
||||
$options += array(
|
||||
'attributes' => array(
|
||||
'class' => ['ajax-link'],
|
||||
'nid'=>$entity->id(),
|
||||
'data-drupal-link-system-path' => $uri->getInternalPath()
|
||||
)
|
||||
);
|
||||
$uri->setOptions($options);
|
||||
}
|
||||
catch (UndefinedLinkTemplateException $e) {
|
||||
// This exception is thrown by \Drupal\Core\Entity\Entity::urlInfo()
|
||||
// and it means that the entity type doesn't have a link template nor
|
||||
// a valid "uri_callback", so don't bother trying to output a link for
|
||||
// the rest of the referenced entities.
|
||||
$output_as_link = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
if ($output_as_link && isset($uri) && !$entity->isNew()) {
|
||||
$elements[$delta] = [
|
||||
'#type' => 'link',
|
||||
'#title' => $label,
|
||||
'#url' => $uri,
|
||||
'#options' => $uri->getOptions(),
|
||||
];
|
||||
|
||||
if (!empty($items[$delta]->_attributes)) {
|
||||
$elements[$delta]['#options'] += ['attributes' => []];
|
||||
$elements[$delta]['#options']['attributes'] += $items[$delta]->_attributes;
|
||||
// Unset field item attributes since they have been included in the
|
||||
// formatter output and shouldn't be rendered in the field template.
|
||||
unset($items[$delta]->_attributes);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$elements[$delta] = ['#plain_text' => $label];
|
||||
}
|
||||
$elements[$delta]['#cache']['tags'] = $entity->getCacheTags();
|
||||
}
|
||||
|
||||
return $elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the output appropriate for one field item.
|
||||
*
|
||||
* @param \Drupal\Core\Field\FieldItemInterface $item
|
||||
* One field item.
|
||||
*
|
||||
* @return string
|
||||
* The textual output generated.
|
||||
*/
|
||||
protected function viewValue(FieldItemInterface $item) {
|
||||
// The text value has no text format assigned to it, so the user input
|
||||
// should equal the output, including newlines.
|
||||
return nl2br(Html::escape($item->value));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user