Skip to content
Snippets Groups Projects
Commit 90b122c8 authored by Nia Kathoni's avatar Nia Kathoni
Browse files

Issue #3483432 by nikathone, andileco: Full pager cause a PHP error

parent e0baaf35
No related branches found
No related tags found
1 merge request!17Full pager cause a PHP error
Pipeline #323289 passed with warnings
......@@ -26,6 +26,9 @@ views.filter.views_csv_source_filter_select:
key:
type: string
label: 'Column name key'
empty_cell_behavior:
type: string
label: 'Empty cell behavior'
operator:
type: string
label: 'Operator'
......
......@@ -9,6 +9,17 @@ use Drupal\views_csv_source\Plugin\views\query\ViewsCsvQuery;
*/
trait ColumnSelectorTrait {
/**
* {@inheritdoc}
*/
public function adminSummary () {
$summary = parent::adminSummary();
if (empty($this->options['key'])) {
return $summary;
}
return $summary ? $summary . " [Column: {$this->options['key']}]" : "[Column: {$this->options['key']}]";
}
/**
* Build the key option element.
*
......
......@@ -19,13 +19,43 @@ use Drupal\views_csv_source\Plugin\views\query\ViewsCsvQuery;
class ViewsCsvFilterSelect extends InOperator {
use ColumnSelectorTrait;
use ViewsCsvFilterTrait;
/**
* {@inheritdoc}
*/
protected function defineOptions(): array {
$options = parent::defineOptions();
$options['key']['default'] = '';
$options['empty_cell_behavior']['default'] = 'none';
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state): void {
parent::buildOptionsForm($form, $form_state);
$form['empty_cell_behavior'] = [
'#type' => 'radios',
'#title' => $this->t('Empty cell behavior'),
'#options' => [
'none' => $this->t('None'),
'remove' => $this->t('Remove'),
'add_label' => $this->t('Add empty label'),
],
'#default_value' => $this->options['empty_cell_behavior'] ?? 'none',
'none' => [
'#description' => $this->t('When there is an empty cell, the select options list will display an option with an empty label as in the cell.'),
],
'remove' => [
'#description' => $this->t('This will remove the empty value from showing under the provided options. Can be useful if exposing the filter.'),
],
'add_label' => [
'#description' => $this->t('This option will keep the empty cell option as an option but provide an "- EMPTY -" label.'),
],
];
$form = $this->buildKeyOptionElement($form);
// Adding ajax behavior on the column key.
......@@ -155,15 +185,31 @@ class ViewsCsvFilterSelect extends InOperator {
}
$query = $this->view->getQuery();
// Ensure the query is of the correct type.
if ($query instanceof ViewsCsvQuery) {
$column_data = $query->getCsvColumnValues($column);
$options = array_combine($column_data, $column_data);
ksort($options);
return $options;
if (!$query instanceof ViewsCsvQuery) {
return [];
}
$column_data = $query->getCsvColumnValues($column);
$options = array_combine($column_data, $column_data);
ksort($options);
// Apply empty cell behavior if needed.
$empty_cell_behavior = $this->options['empty_cell_behavior'];
if ($empty_cell_behavior !== 'none') {
foreach ($options as $key => $label) {
if ($key === '' || $key === NULL) {
if ($empty_cell_behavior === 'remove') {
unset($options[$key]);
}
else {
$options[$key] = $this->t('- EMPTY -');
}
break;
}
}
}
return [];
return $options;
}
}
......@@ -223,6 +223,13 @@ class ViewsCsvQuery extends QueryPluginBase {
return $query;
}
/**
* {@inheritdoc}
*/
public function alter(ViewExecutable $view) {
\Drupal::moduleHandler()->invokeAll('views_query_alter', [$view, $this]);
}
/**
* Builds the necessary info to execute the query.
*
......
......@@ -110,14 +110,14 @@ class Select {
* @throws \League\Csv\Exception
* If the CSV reader cannot be built.
*/
public function execute(): array|\Iterator {
public function execute(): Select|\Iterator|array {
if ($this->hasExecuted()) {
return $this->records;
return $this->isCountQuery() ? $this : $this->records;
}
$this->executed = TRUE;
return $this->getRecords();
return $this->isCountQuery() ? $this : $this->getRecords();
}
/**
......@@ -147,7 +147,7 @@ class Select {
public function isCountQuery(): bool {
$is_count = FALSE;
if ($count_item = $this->getObjectItem('count')) {
$is_count = $count_item == 'true' || $count_item == 1;
$is_count = !empty($count_item[0]) && ($count_item[0] === 'true' || $count_item[0] == 1);
}
return $is_count;
}
......
......@@ -5,6 +5,7 @@ namespace Drupal\Tests\views_csv_source\Functional\Plugin;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\Tests\views_csv_source\Traits\ResourcePathRetrieverTrait;
use Drupal\file\Entity\File;
use Drupal\views\Views;
use Symfony\Component\HttpFoundation\Response;
/**
......@@ -74,6 +75,24 @@ class ViewsCsvSourceViewTest extends ViewTestBase {
$this->assertSession()->elementsCount('xpath', '//tbody/tr/td[starts-with(text(), "Hawaii")]', 1);
$this->assertSession()->elementsCount('xpath', '//tbody/tr/td[starts-with(text(), "Illinois")]', 1);
$this->assertSession()->elementsCount('xpath', '//tbody/tr/td[starts-with(text(), "Kansas")]', 1);
// Testing full pager.
$view = Views::getView('test_views_csv_source');
$display = &$view->storage->getDisplay('default');
$display['display_options']['pager']['options']['items_per_page'] = 3;
$display['display_options']['pager']['type'] = 'full';
$display['display_options']['pager']['options']['tags']['next'] = 'Next >';
$display['display_options']['pager']['options']['tags']['last'] = 'Last >>';
$view->save();
$this->drupalGet('test-views-csv-source');
$labels = [
'Next >',
'Last >>',
];
foreach ($labels as $label) {
$this->assertSession()->pageTextContains($label);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment