Mapping.php in Drupal 10
File
core/modules/views/src/Plugin/views/style/Mapping.php
View source
<?php
namespace Drupal\views\Plugin\views\style;
use Drupal\Core\Form\FormStateInterface;
abstract class Mapping extends StylePluginBase {
protected $usesGrouping = FALSE;
protected $usesFields = TRUE;
protected abstract function defineMapping();
protected function defineOptions() {
$options = parent::defineOptions();
foreach ($this
->defineMapping() as $key => $value) {
$default = !empty($value['#multiple']) ? [] : '';
$options['mapping']['contains'][$key] = [
'default' => $value['#default_value'] ?? $default,
];
if (!empty($value['#toggle'])) {
$options['mapping']['contains']["toggle_{$key}"] = [
'default' => FALSE,
];
}
}
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$mapping = $this
->defineMapping();
$options = array_intersect_key($this->options['mapping'], $mapping);
$field_labels = $this->displayHandler
->getFieldLabels();
$defaults = [
'#type' => 'select',
'#required' => FALSE,
'#multiple' => FALSE,
];
foreach ($options as $key => $value) {
$field_options = [];
$required = !empty($mapping[$key]['#required']);
if (!$required && empty($mapping[$key]['#multiple'])) {
$field_options = [
'' => $this
->t('- None -'),
];
}
$field_options += $field_labels;
if (isset($mapping[$key]['#filter'])) {
$this->view
->initHandlers();
$filter = $mapping[$key]['#filter'];
$this::$filter($field_options);
unset($mapping[$key]['#filter']);
}
$overrides = [
'#options' => $field_options,
'#default_value' => $options[$key],
];
if (!empty($mapping[$key]['#toggle'])) {
$form['mapping']["toggle_{$key}"] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use a custom %field_name', [
'%field_name' => strtolower($mapping[$key]['#title']),
]),
'#default_value' => $this->options['mapping']["toggle_{$key}"],
];
$overrides['#states']['visible'][':input[name="style_options[mapping][' . "toggle_{$key}" . ']"]'] = [
'checked' => TRUE,
];
}
$form['mapping'][$key] = $overrides + $mapping[$key] + $defaults;
}
}
public function render() {
return [
'#theme' => $this
->themeFunctions(),
'#view' => $this->view,
'#options' => $this->options,
'#rows' => $this->view->result,
'#mapping' => $this
->defineMapping(),
];
}
}
Classes
Name |
Description |
Mapping |
Allows fields to be mapped to specific use cases. |