Skip to content
Snippets Groups Projects

Issue #3446009 by andileco: Make column selector a select list based on the headers

Files
3
@@ -7,6 +7,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Drupal\views_csv_source\Plugin\views\query\ViewsCsvQuery;
/**
* Base field handler for views_csv_source.
@@ -66,13 +67,28 @@ class ViewsCsvField extends FieldPluginBase {
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['key'] = [
'#title' => $this->t('Column Selector'),
'#description' => $this->t('Choose a column'),
'#type' => 'textfield',
'#default_value' => $this->options['key'],
'#required' => TRUE,
];
// Get the query.
$query = $this->view->query;
assert($query instanceof ViewsCsvQuery, 'Query must be of type ViewsCsvQuery');
// Ensure the query is of the correct type.
if ($headers = $query->getCsvHeader()) {
$form['key']['#type'] = 'select';
$form['key']['#options'] = array_combine($headers, $headers);
}
else {
// Fall back to text value if CSV has not been selected.
$form['key']['#type'] = 'textfield';
}
$form['trusted_html'] = [
'#title' => $this->t('Trusted HTML'),
'#description' => $this->t('This field is from a trusted source and contains raw HTML markup to render here. Use with caution.'),
Loading