View source
<?php
namespace Drupal\rest\Plugin\views\row;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\row\RowPluginBase;
class DataFieldRow extends RowPluginBase {
protected $usesFields = TRUE;
protected $replacementAliases = [];
protected $rawOutputOptions = [];
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
if (!empty($this->options['field_options'])) {
$options = (array) $this->options['field_options'];
$aliases = static::extractFromOptionsArray('alias', $options);
$this->replacementAliases = array_filter(array_map('trim', $aliases));
$this->rawOutputOptions = static::extractFromOptionsArray('raw_output', $options);
}
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['field_options'] = [
'default' => [],
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['field_options'] = [
'#type' => 'table',
'#header' => [
$this
->t('Field'),
$this
->t('Alias'),
$this
->t('Raw output'),
],
'#empty' => $this
->t('You have no fields. Add some to your view.'),
'#tree' => TRUE,
];
$options = $this->options['field_options'];
if ($fields = $this->view->display_handler
->getOption('fields')) {
foreach ($fields as $id => $field) {
if (!empty($field['exclude'])) {
continue;
}
$form['field_options'][$id]['field'] = [
'#markup' => $id,
];
$form['field_options'][$id]['alias'] = [
'#title' => $this
->t('Alias for @id', [
'@id' => $id,
]),
'#title_display' => 'invisible',
'#type' => 'textfield',
'#default_value' => $options[$id]['alias'] ?? '',
'#element_validate' => [
[
$this,
'validateAliasName',
],
],
];
$form['field_options'][$id]['raw_output'] = [
'#title' => $this
->t('Raw output for @id', [
'@id' => $id,
]),
'#title_display' => 'invisible',
'#type' => 'checkbox',
'#default_value' => $options[$id]['raw_output'] ?? '',
];
}
}
}
public function validateAliasName($element, FormStateInterface $form_state) {
if (preg_match('@[^A-Za-z0-9_-]+@', $element['#value'])) {
$form_state
->setError($element, $this
->t('The machine-readable name must contain only letters, numbers, dashes and underscores.'));
}
}
public function validateOptionsForm(&$form, FormStateInterface $form_state) {
$aliases = static::extractFromOptionsArray('alias', $form_state
->getValue([
'row_options',
'field_options',
]));
if (($filtered = array_filter($aliases)) && array_unique($filtered) !== $filtered) {
$form_state
->setErrorByName('aliases', $this
->t('All field aliases must be unique'));
}
}
public function render($row) {
$output = [];
foreach ($this->view->field as $id => $field) {
if (!empty($this->rawOutputOptions[$id])) {
$value = $field
->getValue($row);
}
else {
$markup = $field
->advancedRender($row);
$field
->postRender($row, $markup);
$value = $field->last_render;
}
if (empty($field->options['exclude'])) {
$output[$this
->getFieldKeyAlias($id)] = $value;
}
}
return $output;
}
public function getFieldKeyAlias($id) {
if (isset($this->replacementAliases[$id])) {
return $this->replacementAliases[$id];
}
return $id;
}
protected static function extractFromOptionsArray($key, $options) {
return array_map(function ($item) use ($key) {
return $item[$key] ?? NULL;
}, $options);
}
}