Commit 32e40223 authored by larowlan's avatar larowlan

Issue #2905000 by amateescu, timmillwood: Add a default filter on the...

Issue #2905000 by amateescu, timmillwood: Add a default filter on the 'published' field in the base views wizard plugin for publishable entity types
parent 9271178d
......@@ -28,14 +28,6 @@ class Comment extends WizardPluginBase {
* Set default values for the filters.
*/
protected $filters = [
'status' => [
'value' => TRUE,
'table' => 'comment_field_data',
'field' => 'status',
'plugin_id' => 'boolean',
'entity_type' => 'comment',
'entity_field' => 'status',
],
'status_node' => [
'value' => TRUE,
'table' => 'node_field_data',
......
......@@ -22,22 +22,6 @@ class Media extends WizardPluginBase {
*/
protected $createdColumn = 'media_field_data-created';
/**
* Set default values for the filters.
*
* @var array
*/
protected $filters = [
'status' => [
'value' => '1',
'table' => 'media_field_data',
'field' => 'status',
'plugin_id' => 'boolean',
'entity_type' => 'media',
'entity_field' => 'status',
],
];
/**
* {@inheritdoc}
*/
......
......@@ -22,22 +22,6 @@ class MediaRevision extends WizardPluginBase {
*/
protected $createdColumn = 'media_field_revision-created';
/**
* Set default values for the filters.
*
* @var array
*/
protected $filters = [
'status' => [
'value' => '1',
'table' => 'media_field_revision',
'field' => 'status',
'plugin_id' => 'boolean',
'entity_type' => 'media',
'entity_field' => 'status',
],
];
/**
* {@inheritdoc}
*/
......
......@@ -25,20 +25,6 @@ class Node extends WizardPluginBase {
*/
protected $createdColumn = 'node_field_data-created';
/**
* Set default values for the filters.
*/
protected $filters = [
'status' => [
'value' => TRUE,
'table' => 'node_field_data',
'field' => 'status',
'plugin_id' => 'boolean',
'entity_type' => 'node',
'entity_field' => 'status',
]
];
/**
* Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::getAvailableSorts().
*
......
......@@ -24,20 +24,6 @@ class NodeRevision extends WizardPluginBase {
*/
protected $createdColumn = 'changed';
/**
* Set default values for the filters.
*/
protected $filters = [
'status' => [
'value' => TRUE,
'table' => 'node_field_revision',
'field' => 'status',
'plugin_id' => 'boolean',
'entity_type' => 'node',
'entity_field' => 'status',
]
];
/**
* Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::rowStyleOptions().
*
......
......@@ -45,16 +45,19 @@ public function testViewAdd() {
$view['show[wizard_key]'] = 'node_revision';
$this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));
$view_storage_controller = \Drupal::entityManager()->getStorage('view');
/** @var \Drupal\views\Entity\View $view */
$view = $view_storage_controller->load($view['id']);
$view = Views::getView($view['id']);
$view->initHandlers();
$this->assertEqual($view->get('base_table'), 'node_field_revision');
$this->assertEqual($view->getBaseTables(), ['node_field_revision' => TRUE, '#global' => TRUE]);
$executable = Views::executableFactory()->get($view);
$this->executeView($executable);
// Check for the default filters.
$this->assertEqual($view->filter['status']->table, 'node_field_revision');
$this->assertEqual($view->filter['status']->field, 'status');
$this->assertTrue($view->filter['status']->value);
$this->assertIdenticalResultset($executable, [['vid' => 1], ['vid' => 3], ['vid' => 2], ['vid' => 4]],
$this->executeView($view);
$this->assertIdenticalResultset($view, [['vid' => 1], ['vid' => 3], ['vid' => 2], ['vid' => 4]],
['vid' => 'vid']);
}
......
......@@ -3,6 +3,7 @@
namespace Drupal\views\Plugin\views\wizard;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\UrlGeneratorTrait;
......@@ -140,7 +141,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
$entity_types = \Drupal::entityManager()->getDefinitions();
foreach ($entity_types as $entity_type_id => $entity_type) {
if ($this->base_table == $entity_type->getBaseTable() || $this->base_table == $entity_type->getDataTable()) {
if (in_array($this->base_table, [$entity_type->getBaseTable(), $entity_type->getDataTable(), $entity_type->getRevisionTable(), $entity_type->getRevisionDataTable()], TRUE)) {
$this->entityType = $entity_type;
$this->entityTypeId = $entity_type_id;
}
......@@ -165,6 +166,21 @@ public function getCreatedColumn() {
public function getFilters() {
$filters = [];
// Add a default filter on the publishing status field, if available.
if ($this->entityType && is_subclass_of($this->entityType->getClass(), EntityPublishedInterface::class)) {
$field_name = $this->entityType->getKey('published');
$this->filters = [
$field_name => [
'value' => TRUE,
'table' => $this->base_table,
'field' => $field_name,
'plugin_id' => 'boolean',
'entity_type' => $this->entityTypeId,
'entity_field' => $field_name,
]
] + $this->filters;
}
$default = $this->filter_defaults;
foreach ($this->filters as $name => $info) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment