reinstaled drupal core using composer-drupal/drupal-project
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
# @Author: Bachir Soussi Chiadmi <bach>
|
||||
# @Email: bachir@figureslibres.io
|
||||
# @Filename: edlp_ajax.info.yml
|
||||
# @License: GPL-V3
|
||||
|
||||
|
||||
name: Edlp Ajax
|
||||
type: module
|
||||
description: Manage ajax entity loading for edlp d8.
|
||||
core: 8.x
|
||||
package: Edlp
|
||||
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
# @Author: Bachir Soussi Chiadmi <bach>
|
||||
# @Email: bachir@figureslibres.io
|
||||
# @Filename: edlp_ajax.module
|
||||
# @License: GPL-V3
|
||||
|
||||
use Drupal\Core\Url;
|
||||
|
||||
|
||||
/**
|
||||
* Implements hook_page_attachments().
|
||||
* @param array $attachments
|
||||
*/
|
||||
function edlp_ajax_page_attachments(array &$attachments) {
|
||||
$url = Url::fromRoute('edlp_ajax.entityjson');
|
||||
$attachments['#attached']['drupalSettings']['edlp_ajax']['entityjson_path'] = $url->getInternalPath();
|
||||
$url = Url::fromRoute('edlp_ajax.blocksjson');
|
||||
$attachments['#attached']['drupalSettings']['edlp_ajax']['blocksjson_path'] = $url->getInternalPath();
|
||||
|
||||
$redirect = false;
|
||||
|
||||
$current_path = \Drupal::service('path.current')->getPath();
|
||||
$current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
||||
$is_front = \Drupal::service('path.matcher')->isFrontPage();
|
||||
|
||||
$entity_type = null;
|
||||
$entity_bundle = null;
|
||||
$entity_id = null;
|
||||
$audio_url = null;
|
||||
foreach (['node', 'taxonomy_term'] as $type) {
|
||||
$entity = \Drupal::routeMatch()->getParameter($type);
|
||||
if($entity){
|
||||
$entity_type = $type;
|
||||
$entity_bundle = $entity->bundle();
|
||||
$entity_id = $entity->id();
|
||||
if($entity_bundle == 'enregistrement'){
|
||||
$field_son_values = $entity->get('field_son')->getValue();
|
||||
$audio_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : null;
|
||||
if($audio_fid){
|
||||
$audio_file = \Drupal\file\Entity\File::load($audio_fid);
|
||||
$son_uri = $audio_file->getFileUri();
|
||||
$audio_url = file_create_url($son_uri);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// do not redirect if not node, term, or custom routing
|
||||
// FIXME: check with routes instead of path as path can change !!!
|
||||
if(preg_match('/^\/?node\/\d+/', $current_path)
|
||||
|| preg_match('/^\/?taxonomy\/term\/\d+/', $current_path)
|
||||
|| preg_match('/^\/?productions/', $current_path)
|
||||
|| preg_match('/^\/?agenda/', $current_path)
|
||||
|| preg_match('/^\/?studio/', $current_path)
|
||||
|| preg_match('/^\/?search/', $current_path)
|
||||
|| preg_match('/^\/?lastdocs/', $current_path)
|
||||
|| preg_match('/^\/?articles/', $current_path)){
|
||||
$redirect = true;
|
||||
}
|
||||
|
||||
// $redirect = false;
|
||||
$js_str = "var edlp = {\n
|
||||
sys_path:'".$current_path."',\n
|
||||
is_front:".($is_front ? 'true':'false').",\n
|
||||
redirect:".($redirect ? 'true':'false').",\n
|
||||
lang_code:'".$current_language."',\n
|
||||
entity_type:'".$entity_type."',\n
|
||||
entity_bundle:'".$entity_bundle."',\n
|
||||
entity_id:'".$entity_id."',\n
|
||||
audio_url:'".$audio_url."',\n
|
||||
};";
|
||||
|
||||
$attachments['#attached']['html_head'][] = [
|
||||
[
|
||||
'#type' => 'html_tag',
|
||||
'#tag' => 'script',
|
||||
'#value' => $js_str,
|
||||
'#weight' => -999,
|
||||
'#group' => 'edlp'
|
||||
],
|
||||
// A key, to make it possible to recognize this HTML element when altering.
|
||||
'edlp',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implements hook_theme().
|
||||
*/
|
||||
function edlp_ajax_theme($existing, $type, $theme, $path) {
|
||||
// @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
|
||||
return array(
|
||||
'edlp_ajax' => array(
|
||||
'file' => 'includes/edlp_ajax.inc',
|
||||
'variables' => array(
|
||||
'entity_type' => null,
|
||||
'bundle' => null,
|
||||
'entity' => null,
|
||||
'view_mode' => 'default',
|
||||
'aside' => array(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_theme_suggestions_HOOK().
|
||||
*/
|
||||
function edlp_ajax_theme_suggestions_edlp_ajax(array $vars) {
|
||||
// dpm($vars);
|
||||
$suggestions = [];
|
||||
// $node = $variables['elements']['#node'];
|
||||
$sanitized_view_mode = strtr($vars['view_mode'], '.', '_');
|
||||
// $entity = $vars['entity'];
|
||||
//
|
||||
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'];
|
||||
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $sanitized_view_mode;
|
||||
// $suggestions[] = 'node__' . $node->bundle() . '__' . $sanitized_view_mode;
|
||||
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id();
|
||||
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['entity']->id() . '__' . $sanitized_view_mode;
|
||||
|
||||
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['bundle'];
|
||||
$suggestions[] = 'edlp_ajax__' . $vars['entity_type'] . '__' . $vars['bundle'] . '__' . $sanitized_view_mode;
|
||||
|
||||
return $suggestions;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
# @Author: Bachir Soussi Chiadmi <bach>
|
||||
# @Date: 13-12-2017
|
||||
# @Email: bachir@figureslibres.io
|
||||
# @Filename: edlp_corpus.routing.yml
|
||||
# @Last modified by: bach
|
||||
# @Last modified time: 20-12-2017
|
||||
# @License: GPL-V3
|
||||
|
||||
edlp_ajax.entityjson:
|
||||
path: '/edlp/ajax/entity/json/{entity_type}/{id}/{viewmode}'
|
||||
defaults:
|
||||
_controller: '\Drupal\edlp_ajax\Controller\EdlpAjaxController::entityjson'
|
||||
_title: 'EdlpAjaxEntityJson'
|
||||
entity_type : null
|
||||
id: null
|
||||
viewmode: 'default'
|
||||
requirements:
|
||||
_permission: 'access content'
|
||||
|
||||
# NOT USED (YET)
|
||||
edlp_ajax.blocksjson:
|
||||
path: '/edlp/ajax/blocks/json/{blockid}'
|
||||
defaults:
|
||||
_controller: '\Drupal\edlp_ajax\Controller\EdlpAjaxController::blocksjson'
|
||||
_title: 'EdlpAjaxBlockJson'
|
||||
blockid: null
|
||||
requirements:
|
||||
_permission: 'access content'
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
// use Drupal\Core\Url;
|
||||
|
||||
function template_preprocess_edlp_ajax(&$vars){
|
||||
// dpm($vars);
|
||||
/*
|
||||
@see https://www.drupal8.ovh/index.php/en/tutoriels/339/render-a-node-or-an-entity
|
||||
*/
|
||||
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($vars['entity_type']);
|
||||
$vars['content'] = $view_builder->view($vars['entity'], $vars['view_mode']);
|
||||
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
{{ content }}
|
||||
{{ aside }}
|
||||
Reference in New Issue
Block a user