View source
<?php
namespace Drupal\redirect_404\Plugin\views\field;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Redirect404Operations extends FieldPluginBase {
protected $entityTypeManager;
protected $renderer;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, AccountInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->renderer = $renderer;
$this->currentUser = $current_user;
}
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'), $container
->get('renderer'), $container
->get('current_user'));
}
public function clickSortable() {
return FALSE;
}
public function render(ResultRow $values) {
$links = [];
$query = [
'query' => [
'source' => ltrim($this
->getValue($values, 'path'), '/'),
'language' => $this
->getValue($values, 'langcode'),
'destination' => $this->view
->getPath(),
],
];
$links['add'] = [
'title' => $this
->t('Add redirect'),
'url' => Url::fromRoute('redirect.add', [], $query),
];
if ($this->currentUser
->hasPermission('administer redirect settings')) {
$links['ignore'] = [
'title' => $this
->t('Ignore'),
'url' => Url::fromRoute('redirect_404.ignore_404', [
'path' => $this
->getValue($values, 'path'),
'langcode' => $this
->getValue($values, 'langcode'),
]),
];
}
$operations['data'] = [
'#type' => 'operations',
'#links' => $links,
];
return $this->renderer
->render($operations);
}
public function access(AccountInterface $account) {
return $this->entityTypeManager
->getAccessControlHandler('redirect')
->createAccess();
}
}