MachineName.php in Drupal 10
File
core/modules/views/src/Plugin/views/field/MachineName.php
View source
<?php
namespace Drupal\views\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ResultRow;
class MachineName extends FieldPluginBase {
protected $valueOptions;
public function getValueOptions() {
if (isset($this->valueOptions)) {
return;
}
if (isset($this->definition['options callback']) && is_callable($this->definition['options callback'])) {
if (isset($this->definition['options arguments']) && is_array($this->definition['options arguments'])) {
$this->valueOptions = call_user_func_array($this->definition['options callback'], $this->definition['options arguments']);
}
else {
$this->valueOptions = call_user_func($this->definition['options callback']);
}
}
else {
$this->valueOptions = [];
}
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['machine_name'] = [
'default' => FALSE,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['machine_name'] = [
'#title' => $this
->t('Output machine name'),
'#description' => $this
->t('Display field as machine name.'),
'#type' => 'checkbox',
'#default_value' => !empty($this->options['machine_name']),
];
}
public function preRender(&$values) {
$this
->getValueOptions();
}
public function render(ResultRow $values) {
$value = $values->{$this->field_alias};
if (!empty($this->options['machine_name']) || !isset($this->valueOptions[$value])) {
$result = $this
->sanitizeValue($value);
}
else {
$result = $this->valueOptions[$value];
}
return $result;
}
}
Classes
Name |
Description |
MachineName |
Field handler which allows to show machine name content as human name. |