Skip to content
Snippets Groups Projects
Commit ac21e1bf authored by Roshni Bedmutha's avatar Roshni Bedmutha Committed by Jacques R. Blier
Browse files

Resolve #3372626 "Fix the warningserrors"

parent 535e8df0
No related branches found
Tags 1.0.0-alpha3
1 merge request!2Resolve #3372626 "Fix the warningserrors"
......@@ -11,4 +11,3 @@ This extension will automatically build a list of parents when an Entity Referen
## Requirements
- An Entity Reference field added to a node.
- Access to a node twig file that the themer can edit.
......@@ -4,12 +4,46 @@ namespace Drupal\twig_backlink\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a form that configures the label used by the Twig Backlink module.
*/
class TwigBacklinkConfigurationForm extends ConfigFormBase {
/**
* The Messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs a new twig backline class object.
*
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The Messenger service.
*/
public function __construct(MessengerInterface $messenger) {
$this->messenger = $messenger;
}
/**
* Creates an instance of this class.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The container to resolve services.
*
* @return static
* The instance of this class.
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('messenger')
);
}
/**
* {@inheritdoc}
*/
......@@ -39,7 +73,7 @@ class TwigBacklinkConfigurationForm extends ConfigFormBase {
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#default_value' => $default_value,
//'#required' => TRUE,
// '#required' => TRUE,
];
return parent::buildForm($form, $form_state);
}
......@@ -52,7 +86,7 @@ class TwigBacklinkConfigurationForm extends ConfigFormBase {
$this->config('twig_backlink.settings')
->set('label', $values['label'])
->save();
\Drupal::messenger()->addStatus('Twig Backlink label saved.');
$this->messenger->addStatus('Twig Backlink label saved.');
}
}
<?php
//#Drush auto-generate all file
// #Drush auto-generate all file
namespace Drupal\twig_backlink;
use Collator;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* This twig extension adds 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 TwigBacklinkTwigExtension extends AbstractExtension {
/**
* The Language Manager service.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The current route match.
......@@ -42,6 +47,8 @@ class TwigBacklinkTwigExtension extends AbstractExtension {
/**
* Constructs a new TwigBacklinkTwigExtension object.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
......@@ -49,10 +56,30 @@ class TwigBacklinkTwigExtension extends AbstractExtension {
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(RouteMatchInterface $route_match, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
public function __construct(LanguageManagerInterface $language_manager, RouteMatchInterface $route_match, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
$this->routeMatch = $route_match;
$this->configFactory = $config_factory;
$this->entityTypeManager = $entity_type_manager;
$this->languageManager = $language_manager;
}
/**
* Creates an instance of this class.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The container to resolve services.
*
* @return static
* The instance of this class.
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('language_manager'),
$container->get('current_route_match'),
$container->get('entity_type.manager'),
$container->get('config.factory')
);
}
/**
......@@ -82,7 +109,7 @@ class TwigBacklinkTwigExtension extends AbstractExtension {
*/
public function twigBacklink($field_name): ?array {
$current_nid = $this->routeMatch->getParameter('node');
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
$langcode = $this->languageManager->getCurrentLanguage()->getId();
if (!is_null($current_nid)) {
$nid = $current_nid->nid->value;
}
......@@ -102,14 +129,14 @@ class TwigBacklinkTwigExtension extends AbstractExtension {
if ($nids) {
foreach ($nids as $nid) {
$node = Node::load($nid);
$node = $this->entityTypeManager->getStorage('node')->load($nid);
$translation = $node->getTranslation($langcode);
$options = ['attributes' => ['class' => 'backlink-link']];
$links['backlink_links'][] = [
'#title' => ($translation->label() ? $translation->label() : $node->label()),
'#type' => 'link',
'#url' => Url::fromRoute('entity.node.canonical',
['node' => $nid], $options)
['node' => $nid], $options),
];
}
}
......@@ -137,25 +164,25 @@ class TwigBacklinkTwigExtension extends AbstractExtension {
$query = $this->entityTypeManager->getStorage('node')->getQuery();
return $query
// Referenced ID
// Referenced ID.
->accessCheck(FALSE)
->condition($field_name, $nid)
->condition('status', 1)
->execute();
}
}
}
/**
* Local sort function.
*
* @return array
*/
function _twig_backlink_array_sort($array, $on, $order=SORT_ASC): array {
$new_array = array();
$sortable_array = array();
$lang_code = \Drupal::languageManager()->getCurrentLanguage()->getId();
$collator = new Collator($lang_code);
function _twig_backlink_array_sort($array, $on, $order = SORT_ASC): array {
$new_array = [];
$sortable_array = [];
$lang_code = $this->languageManager->getCurrentLanguage()->getId();
$collator = new \Collator($lang_code);
if (count($array) > 0) {
foreach ($array as $k => $v) {
......@@ -165,7 +192,8 @@ function _twig_backlink_array_sort($array, $on, $order=SORT_ASC): array {
$sortable_array[$k] = $v2;
}
}
} else {
}
else {
$sortable_array[$k] = $v;
}
}
......
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