b1c0e6da574cc7fd77f3e7ad365404adc788d3f6
[yaffs-website] / web / modules / contrib / redirect / modules / redirect_404 / src / Plugin / views / field / Redirect404Operations.php
1 <?php
2
3 namespace Drupal\redirect_404\Plugin\views\field;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Render\RendererInterface;
7 use Drupal\Core\Session\AccountInterface;
8 use Drupal\Core\Url;
9 use Drupal\views\Plugin\views\field\FieldPluginBase;
10 use Drupal\views\ResultRow;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Provides a views field for the redirect operation buttons.
15  *
16  * @ingroup views_field_handlers
17  *
18  * @ViewsField("redirect_404_operations")
19  */
20 class Redirect404Operations extends FieldPluginBase {
21
22   /**
23    * The entity type manager.
24    *
25    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
26    */
27   protected $entityTypeManager;
28
29   /**
30    * The renderer service.
31    *
32    * @var \Drupal\Core\Render\RendererInterface
33    */
34   protected $renderer;
35
36   /**
37    * Constructor for the redirect operations view field.
38    *
39    * @param array $configuration
40    *   A configuration array containing information about the plugin instance.
41    * @param string $plugin_id
42    *   The plugin_id for the plugin instance.
43    * @param mixed $plugin_definition
44    *   The plugin implementation definition.
45    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
46    *   The entity type manager.
47    * @param \Drupal\Core\Render\RendererInterface $renderer
48    *   The renderer service.
49    * @param \Drupal\Core\Session\AccountInterface $current_user
50    *   The current user.
51    */
52   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, AccountInterface $current_user) {
53     parent::__construct($configuration, $plugin_id, $plugin_definition);
54     $this->entityTypeManager = $entity_type_manager;
55     $this->renderer = $renderer;
56     $this->currentUser = $current_user;
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
63     return new static(
64       $configuration,
65       $plugin_id,
66       $plugin_definition,
67       $container->get('entity_type.manager'),
68       $container->get('renderer'),
69       $container->get('current_user')
70     );
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function clickSortable() {
77     return FALSE;
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function render(ResultRow $values) {
84     $links = [];
85
86     $query = [
87       'query' => [
88         'source' => ltrim($this->getValue($values, 'path'), '/'),
89         'language' => $this->getValue($values, 'langcode'),
90         'destination' => $this->view->getPath(),
91       ],
92     ];
93     $links['add'] = [
94       'title' => $this->t('Add redirect'),
95       'url' => Url::fromRoute('redirect.add', [], $query),
96     ];
97
98     if ($this->currentUser->hasPermission('administer redirect settings')) {
99       $links['ignore'] = [
100         'title' => $this->t('Ignore'),
101         'url' => Url::fromRoute('redirect_404.ignore_404', [
102           'path' => $this->getValue($values, 'path'),
103           'langcode' => $this->getValue($values, 'langcode'),
104         ]),
105       ];
106     }
107
108     $operations['data'] = [
109       '#type' => 'operations',
110       '#links' => $links,
111     ];
112
113     return $this->renderer->render($operations);
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function access(AccountInterface $account) {
120     return $this->entityTypeManager->getAccessControlHandler('redirect')->createAccess();
121   }
122
123 }