Node.php in Freelinking 4.0.x
File
src/Plugin/freelinking/Node.php
View source
<?php
namespace Drupal\freelinking\Plugin\freelinking;
use Drupal\freelinking\Plugin\FreelinkingPluginBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Url;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Node extends FreelinkingPluginBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entityTypeManager;
}
public function getIndicator() {
return '/(n(id|ode)?)$/A';
}
public function getTip() {
return $this
->t('Click to view a local node');
}
public function buildLink(array $target) {
$link = [
'#theme' => 'freelink_error',
'#plugin' => 'nid',
];
$node = $this->entityTypeManager
->getStorage('node')
->load($target['dest']);
if (NULL !== $node && $node instanceof NodeInterface) {
if ($target['language']
->getId() !== $node
->language()
->getId()) {
$node = $node
->getTranslation($target['language']
->getId());
}
$link = [
'#type' => 'link',
'#title' => isset($target['text']) ? $target['text'] : $node
->label(),
'#url' => Url::fromRoute('entity.node.canonical', [
'node' => $node
->id(),
], [
'language' => $target['language'],
]),
'#attributes' => [
'title' => isset($target['tooltip']) ? $target['tooltip'] : $this
->getTip(),
],
];
}
else {
$link['#message'] = $this
->t('Invalid node ID @nid', [
'@nid' => $target['dest'],
]);
}
return $link;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'));
}
}
Classes
Name |
Description |
Node |
Node ID freelinking plugin. |