Commit 8c82db74 authored by catch's avatar catch

Issue #1891214 by damiankloip: Introduce a generic entity bundle filter handler.

parent 66f4183f
<?php
/**
* @file
* Definition of Drupal\node\Plugin\views\filter\Type.
*/
namespace Drupal\node\Plugin\views\filter;
use Drupal\views\Plugin\views\filter\InOperator;
use Drupal\Core\Annotation\Plugin;
/**
* Filter by node type.
*
* @ingroup views_filter_handlers
*
* @Plugin(
* id = "node_type",
* module = "node"
* )
*/
class Type extends InOperator {
function get_value_options() {
if (!isset($this->value_options)) {
$this->value_title = t('Content types');
$types = node_type_get_types();
$options = array();
foreach ($types as $type => $info) {
$options[$type] = t($info->name);
}
asort($options);
$this->value_options = $options;
}
}
}
......@@ -125,7 +125,7 @@ function node_views_data() {
'id' => 'standard',
),
'filter' => array(
'id' => 'node_type',
'id' => 'bundle',
),
'argument' => array(
'id' => 'node_type',
......
<?php
/**
* @file
* Definition of Drupal\taxonomy\Plugin\views\filter\VocabularyVid.
*/
namespace Drupal\taxonomy\Plugin\views\filter;
use Drupal\Core\Annotation\Plugin;
use Drupal\views\Plugin\views\filter\InOperator;
/**
* Filter by vocabulary id.
*
* @ingroup views_filter_handlers
*
* @Plugin(
* id = "vocabulary_vid",
* module = "taxonomy"
* )
*/
class VocabularyVid extends InOperator {
function get_value_options() {
if (isset($this->value_options)) {
return;
}
$this->value_options = array();
$vocabularies = entity_load_multiple('taxonomy_vocabulary');
foreach ($vocabularies as $voc) {
$this->value_options[$voc->id()] = $voc->label();
}
}
}
......@@ -145,7 +145,7 @@ function taxonomy_views_data() {
'title' => t('Vocabulary'),
'help' => t('Filter the results of "Taxonomy: Term" to a particular vocabulary.'),
'filter' => array(
'id' => 'vocabulary_vid',
'id' => 'bundle',
),
);
......
<?php
/**
* @file
* Definition of \Drupal\views\Plugin\views\filter\Bundle.
*/
namespace Drupal\views\Plugin\views\filter;
use Drupal\Core\Annotation\Plugin;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
/**
* Filter class which allows filtering by entity bundles.
*
* @ingroup views_filter_handlers
*
* @Plugin(
* id = "bundle"
* )
*/
class Bundle extends InOperator {
/**
* The entity type for the filter.
*
* @var string
*/
protected $entityType;
/**
* The entity info for the entity type.
*
* @var array
*/
protected $entityInfo;
/**
* Overrides \Drupal\views\Plugin\views\filter\InOperator::init().
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->entityType = $this->getEntityType();
$this->entityInfo = entity_get_info($this->entityType);
$this->real_field = $this->entityInfo['entity_keys']['bundle'];
}
/**
* Overrides \Drupal\views\Plugin\views\filter\InOperator::get_value_options().
*/
public function get_value_options() {
if (!isset($this->value_options)) {
$types = entity_get_bundles($this->entityType);
$this->value_title = t('@entity types', array('@entity' => $this->entityInfo['label']));
$options = array();
foreach ($types as $type => $info) {
$options[$type] = $info['label'];
}
asort($options);
$this->value_options = $options;
}
return $this->value_options;
}
/**
* Overrides \Drupal\views\Plugin\views\filter\InOperator::query().
*/
public function query() {
// Make sure that the entity base table is in the query.
$this->ensureMyTable();
parent::query();
}
}
<?php
/**
* @file
* Contains \Drupal\views\Tests\Entity\FilterEntityBundleTest.
*/
namespace Drupal\views\Tests\Entity;
use Drupal\views\Tests\ViewTestBase;
/**
* Tests the EntityType generic filter handler.
*/
class FilterEntityBundleTest extends ViewTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_entity_type_filter');
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node');
/**
* Entity info data.
*
* @var array
*/
protected $entityInfo;
/**
* Entity bundle data.
*
* @var array
*/
protected $entityBundles;
/**
* An array of entities.
*
* @var array
*/
protected $entities = array();
public static function getInfo() {
return array(
'name' => 'Filter: Entity bundle',
'description' => 'Tests the generic entity bundle filter.',
'group' => 'Views Handlers',
);
}
public function setUp() {
parent::setUp();
$this->drupalCreateContentType(array('type' => 'test_bundle'));
$this->drupalCreateContentType(array('type' => 'test_bundle_2'));
$this->entityBundles = entity_get_bundles('node');
$this->entities['count'] = 0;
foreach ($this->entityBundles as $key => $info) {
for ($i = 0; $i < 5; $i++) {
$entity = entity_create('node', array('label' => $this->randomName(), 'uid' => 1, 'type' => $key));
$entity->save();
$this->entities[$key][$entity->id()] = $entity;
$this->entities['count']++;
}
}
}
/**
* Tests the generic bundle filter.
*/
public function testFilterEntity() {
$view = views_get_view('test_entity_type_filter');
$this->executeView($view);
// Test we have all the results, with all types selected.
$this->assertEqual(count($view->result), $this->entities['count']);
// Test the value_options of the filter handler.
$expected = array();
foreach ($this->entityBundles as $key => $info) {
$expected[$key] = $info['label'];
}
$this->assertIdentical($view->filter['type']->get_value_options(), $expected);
$view->destroy();
// Test each bundle type.
foreach ($this->entityBundles as $key => $info) {
// Test each bundle type.
$view->initDisplay();
$filters = $view->display_handler->getOption('filters');
$filters['type']['value'] = drupal_map_assoc(array($key));
$view->display_handler->setOption('filters', $filters);
$this->executeView($view);
$this->assertEqual(count($view->result), count($this->entities[$key]));
$view->destroy();
}
// Test an invalid bundle type to make sure we have no results.
$view->initDisplay();
$filters = $view->display_handler->getOption('filters');
$filters['type']['value'] = drupal_map_assoc(array('type_3'));
$view->display_handler->setOption('filters', $filters);
$this->executeView($view);
$this->assertEqual(count($view->result), 0);
}
}
base_table: node
core: '8'
description: ''
disabled: '0'
display:
default:
display_plugin: default
id: default
display_title: Master
position: '0'
display_options:
fields:
nid:
id: nid
table: node
field: nid
relationship: none
type:
id: type
table: node
field: type
relationship: none
group_type: group
admin_label: ''
label: Type
exclude: '0'
alter:
alter_text: '0'
text: ''
make_link: '0'
path: ''
absolute: '0'
external: '0'
replace_spaces: '0'
path_case: none
trim_whitespace: '0'
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: '0'
max_length: ''
word_boundary: '1'
ellipsis: '1'
more_link: '0'
more_link_text: ''
more_link_path: ''
strip_tags: '0'
trim: '0'
preserve_tags: ''
html: '0'
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: '1'
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: '1'
empty: ''
hide_empty: '0'
empty_zero: '0'
hide_alter_empty: '1'
defaults:
fields: '0'
filters: '0'
filters:
type:
id: type
table: node
field: type
relationship: none
value:
all: all
test_bundle: test_bundle
test_bundle_2: test_bundle_2
human_name: ''
id: test_entity_type_filter
tag: ''
base_field: nid
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