Version 1
[yaffs-website] / web / core / modules / node / src / Plugin / views / field / Path.php
1 <?php
2
3 namespace Drupal\node\Plugin\views\field;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\field\FieldPluginBase;
7 use Drupal\views\Plugin\views\display\DisplayPluginBase;
8 use Drupal\views\ResultRow;
9 use Drupal\views\ViewExecutable;
10
11 /**
12  * Field handler to present the path to the node.
13  *
14  * @ingroup views_field_handlers
15  *
16  * @ViewsField("node_path")
17  */
18 class Path extends FieldPluginBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
24     parent::init($view, $display, $options);
25
26     $this->additional_fields['nid'] = 'nid';
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function defineOptions() {
33     $options = parent::defineOptions();
34     $options['absolute'] = ['default' => FALSE];
35
36     return $options;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
43     parent::buildOptionsForm($form, $form_state);
44     $form['absolute'] = [
45       '#type' => 'checkbox',
46       '#title' => $this->t('Use absolute link (begins with "http://")'),
47       '#default_value' => $this->options['absolute'],
48       '#description' => $this->t('Enable this option to output an absolute link. Required if you want to use the path as a link destination (as in "output this field as a link" above).'),
49       '#fieldset' => 'alter',
50     ];
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function query() {
57     $this->ensureMyTable();
58     $this->addAdditionalFields();
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function render(ResultRow $values) {
65     $nid = $this->getValue($values, 'nid');
66     return [
67       '#markup' => \Drupal::url('entity.node.canonical', ['node' => $nid], ['absolute' => $this->options['absolute']]),
68     ];
69   }
70
71 }