Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
views_csv_source-3446009
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Issue forks
views_csv_source-3446009
Commits
1e1a7675
Commit
1e1a7675
authored
1 year ago
by
Daniel Cothran
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3446009
by andileco: Make column selector a select list based on the headers
parent
99858f12
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plugin/views/field/ViewsCsvField.php
+38
-7
38 additions, 7 deletions
src/Plugin/views/field/ViewsCsvField.php
with
38 additions
and
7 deletions
src/Plugin/views/field/ViewsCsvField.php
+
38
−
7
View file @
1e1a7675
...
...
@@ -7,6 +7,8 @@ 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
;
use
League\Csv\Reader
;
/**
* Base field handler for views_csv_source.
...
...
@@ -66,13 +68,42 @@ 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 connection service.
$connection
=
\Drupal
::
service
(
'views_csv_source.connection'
);
// Get the query.
$query
=
$this
->
view
->
query
;
// Ensure the query is of the correct type.
if
(
$query
instanceof
ViewsCsvQuery
)
{
// Access the options array from the query object.
$csv_uri
=
$query
->
options
[
'csv_file'
];
$csv
=
Reader
::
createFromString
(
$connection
->
fetchContent
(
$csv_uri
));
// Get headers from the CSV file.
$headers
=
$csv
->
nth
(
0
)
??
[];
$options
=
[];
foreach
(
$headers
as
$header
)
{
$options
[
$header
]
=
$header
;
}
$form
[
'key'
]
=
[
'#title'
=>
$this
->
t
(
'Column Selector'
),
'#description'
=>
$this
->
t
(
'Choose a column'
),
'#type'
=>
'select'
,
'#options'
=>
$options
,
'#default_value'
=>
$this
->
options
[
'key'
],
'#required'
=>
TRUE
,
];
}
else
{
// Fall back to text value if CSV has not been selected.
$form
[
'key'
]
=
[
'#title'
=>
$this
->
t
(
'Column Selector'
),
'#description'
=>
$this
->
t
(
'Choose a column'
),
'#type'
=>
'textfield'
,
'#default_value'
=>
$this
->
options
[
'key'
],
'#required'
=>
TRUE
,
];
}
$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.'
),
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment