Links.php in Drupal 8
File
core/modules/views/src/Plugin/views/field/Links.php
View source
<?php
namespace Drupal\views\Plugin\views\field;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url as UrlObject;
abstract class Links extends FieldPluginBase {
public function usesGroupBy() {
return FALSE;
}
public function defineOptions() {
$options = parent::defineOptions();
$options['fields'] = [
'default' => [],
];
$options['destination'] = [
'default' => TRUE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$field_options = $this
->getPreviousFieldLabels();
$form['fields'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Fields'),
'#description' => $this
->t('Fields to be included as links.'),
'#options' => $field_options,
'#default_value' => $this->options['fields'],
];
$form['destination'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Include destination'),
'#description' => $this
->t('Include a "destination" parameter in the link to return the user to the original view upon completing the link action.'),
'#default_value' => $this->options['destination'],
];
}
protected function getLinks() {
$links = [];
foreach ($this->options['fields'] as $field) {
if (empty($this->view->field[$field]->last_render_text)) {
continue;
}
$title = $this->view->field[$field]->last_render_text;
$path = '';
$url = NULL;
if (!empty($this->view->field[$field]->options['alter']['path'])) {
$path = $this->view->field[$field]->options['alter']['path'];
}
elseif (!empty($this->view->field[$field]->options['alter']['url']) && $this->view->field[$field]->options['alter']['url'] instanceof UrlObject) {
$url = $this->view->field[$field]->options['alter']['url'];
}
$tokens = $this
->getRenderTokens([]);
$path = strip_tags(Html::decodeEntities($this
->viewsTokenReplace($path, $tokens)));
$links[$field] = [
'url' => $path ? UrlObject::fromUri('internal:/' . $path) : $url,
'title' => $title,
];
if (!empty($this->options['destination'])) {
$links[$field]['query'] = \Drupal::destination()
->getAsArray();
}
}
return $links;
}
public function query() {
}
}
Classes
Name |
Description |
Links |
A abstract handler which provides a collection of links. |