Skip to content
Snippets Groups Projects
Commit 6efed104 authored by Jacques R. Blier's avatar Jacques R. Blier
Browse files

Merge branch '16-version-drupal-10-meta' into '2.0.x'

Resolve "Version Drupal 10 [meta]"

Closes #16

See merge request jrblier/twig-backlink!1
parents a1b18c15 89780cb9
No related branches found
No related tags found
No related merge requests found
<?php
//#Drush auto-generate all file
namespace Drupal\twig_backlink;
use Collator;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\node\Entity\Node;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* Twig extension add a new twig_backlink function to build a list of links of
* the parent entities.
* This twig extension adds a new twig_backlink function to build a list of
* links of the parent entities.
*/
class TwigExtension extends \Twig_Extension {
class TwigBacklinkTwigExtension extends AbstractExtension {
/**
* @var CurrentRouteMatch
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
private $routeMatch;
protected $routeMatch;
/**
* @var ConfigFactory
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
private $configFactory;
protected $configFactory;
/**
* @var EntityTypeManager
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
private $entityTypeManager;
protected $entityTypeManager;
/**
* TwigExtension constructor.
* Constructs a new TwigBacklinkTwigExtension object.
*
* @param CurrentRouteMatch $routeMatch
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(CurrentRouteMatch $routeMatch, ConfigFactory $configFactory, EntityTypeManager $entityTypeManager) {
$this->routeMatch = $routeMatch;
$this->configFactory = $configFactory;
$this->entityTypeManager = $entityTypeManager;
public function __construct(RouteMatchInterface $route_match, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
$this->routeMatch = $route_match;
$this->configFactory = $config_factory;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public function getFunctions() {
return [
new \Twig\TwigFunction('twig_backlink', [$this, 'twigBacklink']),
new TwigFunction('twig_backlink', [$this, 'twigBacklink']),
];
}
......@@ -65,12 +80,13 @@ class TwigExtension extends \Twig_Extension {
* @return null|array
* A render array for the field or NULL if the value does not exist.
*/
public function twigBacklink($field_name) {
public function twigBacklink($field_name): ?array {
$current_nid = $this->routeMatch->getParameter('node');
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
if(!is_null($current_nid)) {
if (!is_null($current_nid)) {
$nid = $current_nid->nid->value;
} else {
}
else {
$nid = '';
}
......@@ -83,11 +99,12 @@ class TwigExtension extends \Twig_Extension {
$config = $this->configFactory->get('twig_backlink.settings');
$label = $config->get('label');
if($nids) {
foreach($nids as $nid) {
if ($nids) {
foreach ($nids as $nid) {
$node = Node::load($nid);
$translation = $node->getTranslation($langcode);
$options = array('attributes' => array('class' => 'backlink-link'));
$options = ['attributes' => ['class' => 'backlink-link']];
$links['backlink_links'][] = [
'#title' => $translation->label(),
'#type' => 'link',
......@@ -116,23 +133,25 @@ class TwigExtension extends \Twig_Extension {
* @return array|int
*/
public function getNids($nid, $field_name) {
$query = $this->entityTypeManager->getStorage('node')->getQuery();
$result = $query
return $query
// Referenced ID
->accessCheck(FALSE)
->condition($field_name, $nid)
->condition('status', 1)
->execute();
return $result;
}
}
/**
* Local sort function.
*
* @return array
*/
function _twig_backlink_array_sort($array, $on, $order=SORT_ASC) {
function _twig_backlink_array_sort($array, $on, $order=SORT_ASC): array {
$new_array = array();
$sortable_array = array();
$lang_code = \Drupal::languageManager()->getCurrentLanguage()->getId();
......@@ -151,10 +170,8 @@ function _twig_backlink_array_sort($array, $on, $order=SORT_ASC) {
}
}
switch ($order) {
case SORT_ASC:
$collator->asort($sortable_array);
break;
if ($order == SORT_ASC) {
$collator->asort($sortable_array);
}
foreach ($sortable_array as $k => $v) {
......
......@@ -2,5 +2,5 @@ name: Twig Backlink
type: module
description: Twig extension that adds a new twig_backlink function.
configure: twig_backlink.admin_overview
core_version_requirement: ^8 || ^9
core_version_requirement: ^9 || ^10
core: 8.x
services:
twig_backlink.twig_extension:
class: Drupal\twig_backlink\TwigExtension
class: Drupal\twig_backlink\TwigBacklinkTwigExtension
arguments: ['@current_route_match', '@config.factory', '@entity_type.manager']
tags:
- { name: twig.extension }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment