3 namespace Drupal\views\Plugin\views\field;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Url as UrlObject;
10 * A abstract handler which provides a collection of links.
12 * @ingroup views_field_handlers
14 abstract class Links extends FieldPluginBase {
19 public function usesGroupBy() {
26 public function defineOptions() {
27 $options = parent::defineOptions();
29 $options['fields'] = ['default' => []];
30 $options['destination'] = ['default' => TRUE];
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();
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'],
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'],
58 * Gets the list of links used by this field.
61 * The links which are used by the render function.
63 protected function getLinks() {
65 foreach ($this->options['fields'] as $field) {
66 if (empty($this->view->field[$field]->last_render_text)) {
69 $title = $this->view->field[$field]->last_render_text;
72 if (!empty($this->view->field[$field]->options['alter']['path'])) {
73 $path = $this->view->field[$field]->options['alter']['path'];
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'];
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)));
83 'url' => $path ? UrlObject::fromUri('internal:/' . $path) : $url,
86 if (!empty($this->options['destination'])) {
87 $links[$field]['query'] = \Drupal::destination()->getAsArray();
97 public function query() {