Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / contextual / src / Plugin / views / field / ContextualLinks.php
1 <?php
2
3 namespace Drupal\contextual\Plugin\views\field;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Component\Utility\Html;
7 use Drupal\Component\Utility\UrlHelper;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Routing\RedirectDestinationTrait;
10 use Drupal\Core\Url;
11 use Drupal\views\Plugin\views\field\FieldPluginBase;
12 use Drupal\views\ResultRow;
13
14 /**
15  * Provides a handler that adds contextual links.
16  *
17  * @ingroup views_field_handlers
18  *
19  * @ViewsField("contextual_links")
20  */
21 class ContextualLinks extends FieldPluginBase {
22
23   use RedirectDestinationTrait;
24
25   /**
26    * {@inheritdoc}
27    */
28   public function usesGroupBy() {
29     return FALSE;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function defineOptions() {
36     $options = parent::defineOptions();
37
38     $options['fields'] = ['default' => []];
39     $options['destination'] = ['default' => 1];
40
41     return $options;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
48     $all_fields = $this->view->display_handler->getFieldLabels();
49     // Offer to include only those fields that follow this one.
50     $field_options = array_slice($all_fields, 0, array_search($this->options['id'], array_keys($all_fields)));
51     $form['fields'] = [
52       '#type' => 'checkboxes',
53       '#title' => $this->t('Fields'),
54       '#description' => $this->t('Fields to be included as contextual links.'),
55       '#options' => $field_options,
56       '#default_value' => $this->options['fields'],
57     ];
58     $form['destination'] = [
59       '#type' => 'select',
60       '#title' => $this->t('Include destination'),
61       '#description' => $this->t('Include a "destination" parameter in the link to return the user to the original view upon completing the contextual action.'),
62       '#options' => [
63         '0' => $this->t('No'),
64         '1' => $this->t('Yes'),
65       ],
66       '#default_value' => $this->options['destination'],
67     ];
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function preRender(&$values) {
74     // Add a row plugin css class for the contextual link.
75     $class = 'contextual-region';
76     if (!empty($this->view->style_plugin->options['row_class'])) {
77       $this->view->style_plugin->options['row_class'] .= " $class";
78     }
79     else {
80       $this->view->style_plugin->options['row_class'] = $class;
81     }
82   }
83
84   /**
85    * Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::render().
86    *
87    * Renders the contextual fields.
88    *
89    * @param \Drupal\views\ResultRow $values
90    *   The values retrieved from a single row of a view's query result.
91    *
92    * @see contextual_preprocess()
93    * @see contextual_contextual_links_view_alter()
94    */
95   public function render(ResultRow $values) {
96     $links = [];
97     foreach ($this->options['fields'] as $field) {
98       $rendered_field = $this->view->style_plugin->getField($values->index, $field);
99       if (empty($rendered_field)) {
100         continue;
101       }
102       $title = $this->view->field[$field]->last_render_text;
103       $path = '';
104       if (!empty($this->view->field[$field]->options['alter']['path'])) {
105         $path = $this->view->field[$field]->options['alter']['path'];
106       }
107       elseif (!empty($this->view->field[$field]->options['alter']['url']) && $this->view->field[$field]->options['alter']['url'] instanceof Url) {
108         $path = $this->view->field[$field]->options['alter']['url']->toString();
109       }
110       if (!empty($title) && !empty($path)) {
111         // Make sure that tokens are replaced for this paths as well.
112         $tokens = $this->getRenderTokens([]);
113         $path = strip_tags(Html::decodeEntities(strtr($path, $tokens)));
114
115         $links[$field] = [
116           'href' => $path,
117           'title' => $title,
118         ];
119         if (!empty($this->options['destination'])) {
120           $links[$field]['query'] = $this->getDestinationArray();
121         }
122       }
123     }
124
125     // Renders a contextual links placeholder.
126     if (!empty($links)) {
127       $contextual_links = [
128         'contextual' => [
129           '',
130           [],
131           [
132             'contextual-views-field-links' => UrlHelper::encodePath(Json::encode($links)),
133           ]
134         ]
135       ];
136
137       $element = [
138         '#type' => 'contextual_links_placeholder',
139         '#id' => _contextual_links_to_id($contextual_links),
140       ];
141       return \Drupal::service('renderer')->render($element);
142     }
143     else {
144       return '';
145     }
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   public function query() {}
152
153 }