Pull merge.
[yaffs-website] / web / core / modules / views / src / Plugin / views / field / MachineName.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\field;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\ResultRow;
7
8 /**
9  * Field handler which allows to show machine name content as human name.
10  * @ingroup views_field_handlers
11  *
12  * Definition items:
13  * - options callback: The function to call in order to generate the value options. If omitted, the options 'Yes' and 'No' will be used.
14  * - options arguments: An array of arguments to pass to the options callback.
15  *
16  * @ViewsField("machine_name")
17  */
18 class MachineName extends FieldPluginBase {
19
20   /**
21    * Stores the available options.
22    *
23    * @var array
24    */
25   protected $valueOptions;
26
27   public function getValueOptions() {
28     if (isset($this->valueOptions)) {
29       return;
30     }
31
32     if (isset($this->definition['options callback']) && is_callable($this->definition['options callback'])) {
33       if (isset($this->definition['options arguments']) && is_array($this->definition['options arguments'])) {
34         $this->valueOptions = call_user_func_array($this->definition['options callback'], $this->definition['options arguments']);
35       }
36       else {
37         $this->valueOptions = call_user_func($this->definition['options callback']);
38       }
39     }
40     else {
41       $this->valueOptions = [];
42     }
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   protected function defineOptions() {
49     $options = parent::defineOptions();
50     $options['machine_name'] = ['default' => FALSE];
51
52     return $options;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
59     parent::buildOptionsForm($form, $form_state);
60
61     $form['machine_name'] = [
62       '#title' => $this->t('Output machine name'),
63       '#description' => $this->t('Display field as machine name.'),
64       '#type' => 'checkbox',
65       '#default_value' => !empty($this->options['machine_name']),
66     ];
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function preRender(&$values) {
73     $this->getValueOptions();
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function render(ResultRow $values) {
80     $value = $values->{$this->field_alias};
81     if (!empty($this->options['machine_name']) || !isset($this->valueOptions[$value])) {
82       $result = $this->sanitizeValue($value);
83     }
84     else {
85       $result = $this->valueOptions[$value];
86     }
87
88     return $result;
89   }
90
91 }