Version 1
[yaffs-website] / web / core / modules / views / src / Plugin / views / field / Links.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\field;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Url as UrlObject;
8
9 /**
10  * A abstract handler which provides a collection of links.
11  *
12  * @ingroup views_field_handlers
13  */
14 abstract class Links extends FieldPluginBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public function usesGroupBy() {
20     return FALSE;
21   }
22
23   /**
24    * {@inheritdoc}
25    */
26   public function defineOptions() {
27     $options = parent::defineOptions();
28
29     $options['fields'] = ['default' => []];
30     $options['destination'] = ['default' => TRUE];
31
32     return $options;
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
39     parent::buildOptionsForm($form, $form_state);
40     // Only show fields that precede this one.
41     $field_options = $this->getPreviousFieldLabels();
42     $form['fields'] = [
43       '#type' => 'checkboxes',
44       '#title' => $this->t('Fields'),
45       '#description' => $this->t('Fields to be included as links.'),
46       '#options' => $field_options,
47       '#default_value' => $this->options['fields'],
48     ];
49     $form['destination'] = [
50       '#type' => 'checkbox',
51       '#title' => $this->t('Include destination'),
52       '#description' => $this->t('Include a "destination" parameter in the link to return the user to the original view upon completing the link action.'),
53       '#default_value' => $this->options['destination'],
54     ];
55   }
56
57   /**
58    * Gets the list of links used by this field.
59    *
60    * @return array
61    *   The links which are used by the render function.
62    */
63   protected function getLinks() {
64     $links = [];
65     foreach ($this->options['fields'] as $field) {
66       if (empty($this->view->field[$field]->last_render_text)) {
67         continue;
68       }
69       $title = $this->view->field[$field]->last_render_text;
70       $path = '';
71       $url = NULL;
72       if (!empty($this->view->field[$field]->options['alter']['path'])) {
73         $path = $this->view->field[$field]->options['alter']['path'];
74       }
75       elseif (!empty($this->view->field[$field]->options['alter']['url']) && $this->view->field[$field]->options['alter']['url'] instanceof UrlObject) {
76         $url = $this->view->field[$field]->options['alter']['url'];
77       }
78       // Make sure that tokens are replaced for this paths as well.
79       $tokens = $this->getRenderTokens([]);
80       $path = strip_tags(Html::decodeEntities($this->viewsTokenReplace($path, $tokens)));
81
82       $links[$field] = [
83         'url' => $path ? UrlObject::fromUri('internal:/' . $path) : $url,
84         'title' => $title,
85       ];
86       if (!empty($this->options['destination'])) {
87         $links[$field]['query'] = \Drupal::destination()->getAsArray();
88       }
89     }
90
91     return $links;
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function query() {
98   }
99
100 }