View source
<?php
namespace Drupal\views\Plugin\views\field;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\views\Entity\Render\EntityTranslationRenderTrait;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class LinkBase extends FieldPluginBase {
use RedirectDestinationTrait;
use EntityTranslationRenderTrait;
protected $accessManager;
protected $currentUser;
protected $languageManager;
protected $entityTypeManager;
protected $entityRepository;
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccessManagerInterface $access_manager, EntityTypeManagerInterface $entity_type_manager = NULL, EntityRepositoryInterface $entity_repository = NULL, LanguageManagerInterface $language_manager = NULL) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->accessManager = $access_manager;
if (!$entity_type_manager) {
@trigger_error('Passing the entity type manager service to \\Drupal\\views\\Plugin\\views\\field\\LinkBase::__construct is required since 8.7.0, see https://www.drupal.org/node/3023427.', E_USER_DEPRECATED);
$entity_type_manager = \Drupal::service('entity_type.manager');
}
$this->entityTypeManager = $entity_type_manager;
if (!$entity_repository) {
@trigger_error('Passing the entity repository service to \\Drupal\\views\\Plugin\\views\\field\\LinkBase::__construct is required since 8.7.0, see https://www.drupal.org/node/3023427.', E_USER_DEPRECATED);
$entity_repository = \Drupal::service('entity.repository');
}
$this->entityRepository = $entity_repository;
if (!$language_manager) {
@trigger_error('Passing the language manager service to \\Drupal\\views\\Plugin\\views\\field\\LinkBase::__construct is required since 8.7.0, see https://www.drupal.org/node/3023427.', E_USER_DEPRECATED);
$language_manager = \Drupal::service('language_manager');
}
$this->languageManager = $language_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('access_manager'), $container
->get('entity_type.manager'), $container
->get('entity.repository'), $container
->get('language_manager'));
}
protected function currentUser() {
if (!$this->currentUser) {
$this->currentUser = \Drupal::currentUser();
}
return $this->currentUser;
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['text'] = [
'default' => $this
->getDefaultLabel(),
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['text'] = [
'#type' => 'textfield',
'#title' => $this
->t('Text to display'),
'#default_value' => $this->options['text'],
];
parent::buildOptionsForm($form, $form_state);
$form['alter'] += [
'path' => [],
'query' => [],
'external' => [],
];
$form['alter']['path'] += [
'#access' => FALSE,
];
$form['alter']['query'] += [
'#access' => FALSE,
];
$form['alter']['external'] += [
'#access' => FALSE,
];
}
public function usesGroupBy() {
return FALSE;
}
public function query() {
if ($this->languageManager
->isMultilingual()) {
$this
->getEntityTranslationRenderer()
->query($this->query, $this->relationship);
}
$this
->addAdditionalFields();
}
public function render(ResultRow $row) {
$access = $this
->checkUrlAccess($row);
$build = [
'#markup' => $access
->isAllowed() ? $this
->renderLink($row) : '',
];
BubbleableMetadata::createFromObject($access)
->applyTo($build);
return $build;
}
protected function checkUrlAccess(ResultRow $row) {
$url = $this
->getUrlInfo($row);
return $this->accessManager
->checkNamedRoute($url
->getRouteName(), $url
->getRouteParameters(), $this
->currentUser(), TRUE);
}
protected abstract function getUrlInfo(ResultRow $row);
protected function renderLink(ResultRow $row) {
$this->options['alter']['make_link'] = TRUE;
$this->options['alter']['url'] = $this
->getUrlInfo($row);
$text = !empty($this->options['text']) ? $this
->sanitizeValue($this->options['text']) : $this
->getDefaultLabel();
$this
->addLangcode($row);
return $text;
}
protected function addLangcode(ResultRow $row) {
$entity = $this
->getEntity($row);
if ($this->languageManager
->isMultilingual()) {
$this->options['alter']['language'] = $this
->getEntityTranslation($entity, $row)
->language();
}
}
protected function getDefaultLabel() {
return $this
->t('link');
}
protected function getEntityTypeId() {
return $this
->getEntityType();
}
protected function getEntityTypeManager() {
return $this->entityTypeManager;
}
protected function getEntityRepository() {
return $this->entityRepository;
}
protected function getLanguageManager() {
return $this->languageManager;
}
protected function getView() {
return $this->view;
}
}