Skip to content
Snippets Groups Projects
Commit b672ad3e authored by baldwinlouie's avatar baldwinlouie
Browse files

git commit -m 'Issue #2998227 by baldwinlouie: Add a views filtering plugin...

git commit -m 'Issue #2998227 by baldwinlouie: Add a views filtering plugin that supports dropdown filtering' --author="baldwinlouie <baldwinlouie@129875.no-reply.drupal.org>"
parent b35bdef9
No related branches found
No related tags found
No related merge requests found
Showing
with 291 additions and 29 deletions
<?php
namespace Drupal\aws_cloud\Entity\Ec2;
use Drupal\views\EntityViewsData;
use Drupal\views\EntityViewsDataInterface;
class Ec2BaseViewsData extends EntityViewsData implements EntityViewsDataInterface {
/**
* Function that loops through a table definition and adds a select dropdown
* to certain fields
* @param $data
* Data array from getViewsData();
* @param $table_name
* The entity table name
* @param $fields
* A list of fields for a particular entity type
* @param $selectable
* An array of selectable fields
*/
protected function addDropdownSelector(&$data, $table_name, $fields, $selectable) {
foreach ($fields as $key => $field) {
/* @var \Drupal\Core\Field\BaseFieldDefinition $field */
if (in_array($key, $selectable)) {
$data[$table_name][$key . '_fs'] = [
'title' => t('@label (selector)', [
'@label' => $field->getLabel(),
]),
'help' => t('Provides a dropdown option for text filtering. If there are no results, the filter will not be shown.'),
'filter' => [
'field' => $key,
'table' => $table_name,
'id' => 'texttoselect',
'additional fields' => [],
'field_name' => $field->getLabel(),
'allow empty' => TRUE,
],
];
}
}
}
}
\ No newline at end of file
......@@ -5,19 +5,30 @@
// created by yas 2016/05/20.
namespace Drupal\aws_cloud\Entity\Ec2;
use Drupal\views\EntityViewsData;
use Drupal\views\EntityViewsDataInterface;
/**
* Provides the views data for the CloudScripting entity type.
*/
class ElasticIpViewsData extends EntityViewsData implements EntityViewsDataInterface {
class ElasticIpViewsData extends Ec2BaseViewsData {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
$table_name = $this->storage->getEntityTypeId();
$fields = $this->entityManager->getFieldStorageDefinitions($table_name);
// the following is a list of fields to turn from text search to
// select list. This list can be expanded through hook_views_data_alter().
$selectable = [
'domain',
'scope',
'network_interface_id',
'allocation_id',
'association_id',
];
$this->addDropdownSelector($data, $table_name, $fields, $selectable);
return $data;
}
......
......@@ -5,26 +5,41 @@
// created by yas 2016/05/20.
namespace Drupal\aws_cloud\Entity\Ec2;
use Drupal\views\EntityViewsData;
use Drupal\views\EntityViewsDataInterface;
/**
* Provides the views data for the CloudScripting entity type.
*/
class ImageViewsData extends EntityViewsData implements EntityViewsDataInterface {
class ImageViewsData extends Ec2BaseViewsData {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
$table_name = $this->storage->getEntityTypeId();
$fields = $this->entityManager->getFieldStorageDefinitions($table_name);
// the following is a list of fields to turn from text search to
// select list. This list can be expanded through hook_views_data_alter().
$selectable = [
'image_id',
'source',
'architecture',
'virtualization_type',
'image_type',
'root_device_type',
'kernel_id',
'owner',
'visibility',
];
$data['aws_cloud_image']['table']['base'] = [
'field' => 'id',
'title' => t('AWS Cloud Image'),
'help' => t('The AWC Cloud Image entity ID.'),
'help' => t('The AWS Cloud Image entity ID.'),
];
$this->addDropdownSelector($data, $table_name, $fields, $selectable);
return $data;
}
......
......@@ -6,26 +6,40 @@
// created by yas 2016/05/20.
namespace Drupal\aws_cloud\Entity\Ec2;
use Drupal\views\EntityViewsData;
use Drupal\views\EntityViewsDataInterface;
/**
* Provides the views data for the CloudScripting entity type.
*/
class InstanceViewsData extends EntityViewsData implements EntityViewsDataInterface {
class InstanceViewsData extends Ec2BaseViewsData {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
$table_name = $this->storage->getEntityTypeId();
$fields = $this->entityManager->getFieldStorageDefinitions($table_name);
// the following is a list of fields to turn from text search to
// select list. This list can be expanded through hook_views_data_alter().
$selectable = [
'instance_state',
'instance_type',
'availability_zone',
'key_pair_name',
'security_groups',
'vpc_id',
'subnet_id',
'image_id',
];
$data['aws_cloud_instance']['table']['base'] = [
$data[$table_name]['table']['base'] = [
'field' => 'id',
'title' => t('AWS Cloud Instance'),
'help' => t('The AWC Cloud Instance entity ID.'),
];
$this->addDropdownSelector($data, $table_name, $fields, $selectable);
return $data;
}
......
......@@ -5,19 +5,36 @@
// created by yas 2016/05/20.
namespace Drupal\aws_cloud\Entity\Ec2;
use Drupal\views\EntityViewsData;
use Drupal\views\EntityViewsDataInterface;
/**
* Provides the views data for the NetworkInterface entity type.
*/
class NetworkInterfaceViewsData extends EntityViewsData implements EntityViewsDataInterface {
class NetworkInterfaceViewsData extends Ec2BaseViewsData {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
$table_name = $this->storage->getEntityTypeId();
$fields = $this->entityManager->getFieldStorageDefinitions($table_name);
// the following is a list of fields to turn from text search to
// select list. This list can be expanded through hook_views_data_alter().
$selectable = [
'vpc_id',
'status',
'security_groups',
'attachment_id',
'attachment_owner',
'attachment_status',
'owner_id',
'association_id',
'subnet_id',
'availability_zone',
'allocation_id',
];
$this->addDropdownSelector($data, $table_name, $fields, $selectable);
return $data;
}
......
......@@ -31,7 +31,7 @@ use Drupal\Core\Field\BaseFieldDefinition;
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\aws_cloud\Controller\Ec2\SecurityGroupListBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* "views_data" = "Drupal\aws_cloud\Entity\Ec2\SecurityGroupViewsData",
* "form" = {
* "default" = "Drupal\aws_cloud\Form\Ec2\SecurityGroupEditForm",
* "add" = "Drupal\aws_cloud\Form\Ec2\SecurityGroupCreateForm",
......
......@@ -15,13 +15,29 @@ use Drupal\views\EntityViewsDataInterface;
/**
* Provides the views data for the CloudScripting entity type.
*/
class SecurityGroupViewsData extends EntityViewsData implements EntityViewsDataInterface {
class SecurityGroupViewsData extends Ec2BaseViewsData {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
$table_name = $this->storage->getEntityTypeId();
$fields = $this->entityManager->getFieldStorageDefinitions($table_name);
// the following is a list of fields to turn from text search to
// select list. This list can be expanded through hook_views_data_alter().
$selectable = [
'vpc_id',
];
$data['$table_name']['table']['base'] = [
'field' => 'id',
'title' => t('AWS Security Group'),
'help' => t('The AWS Security Group entity'),
];
$this->addDropdownSelector($data, $table_name, $fields, $selectable);
return $data;
}
......
......@@ -5,13 +5,10 @@
// created by yas 2016/05/20.
namespace Drupal\aws_cloud\Entity\Ec2;
use Drupal\views\EntityViewsData;
use Drupal\views\EntityViewsDataInterface;
/**
* Provides the views data for the CloudScripting entity type.
*/
class SnapshotViewsData extends EntityViewsData implements EntityViewsDataInterface {
class SnapshotViewsData extends Ec2BaseViewsData {
/**
* {@inheritdoc}
......@@ -25,6 +22,21 @@ class SnapshotViewsData extends EntityViewsData implements EntityViewsDataInterf
'help' => t('The AWC Cloud Snapshot entity ID.'),
];
$table_name = $this->storage->getEntityTypeId();
$fields = $this->entityManager->getFieldStorageDefinitions($table_name);
// the following is a list of fields to turn from text search to
// select list. This list can be expanded through hook_views_data_alter().
$selectable = [
'size',
'volume_id',
'owner',
'encrypted',
'capacity',
];
$this->addDropdownSelector($data, $table_name, $fields, $selectable);
return $data;
}
......
......@@ -5,19 +5,32 @@
// created by yas 2016/05/20.
namespace Drupal\aws_cloud\Entity\Ec2;
use Drupal\views\EntityViewsData;
use Drupal\views\EntityViewsDataInterface;
/**
* Provides the views data for the AWS Cloud Volume entity type.
*/
class VolumeViewsData extends EntityViewsData implements EntityViewsDataInterface {
class VolumeViewsData extends Ec2BaseViewsData {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
$table_name = $this->storage->getEntityTypeId();
$fields = $this->entityManager->getFieldStorageDefinitions($table_name);
// the following is a list of fields to turn from text search to
// select list. This list can be expanded through hook_views_data_alter().
$selectable = [
'size',
'state',
'volume_status',
'volume_type',
'iops',
'availability_zone',
'encrypted',
];
$this->addDropdownSelector($data, $table_name, $fields, $selectable);
return $data;
}
......
<?php
namespace Drupal\aws_cloud\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\views\Plugin\views\filter\InOperator;
/**
* Filter handler which turns a text field filter into a select filter.
* This class is adapted from the following project.
* https://www.drupal.org/project/views_field_select_filter
* The main difference is that this filter takes cloud_context into account
* if that is available in the url.
*
* @ingroup views_field_handlers
*
* @ViewsFilter("texttoselect")
*/
class TextToSelectFilter extends InOperator implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
// default to true. This filter is not useful
// if not exposed to the user
$options['exposed'] = ['default' => TRUE];
$options['expose']['contains']['sort_values'] = ['default' => 0];
return $options;
}
/**
* {@inheritdoc}
*/
public function defaultExposeOptions() {
parent::defaultExposeOptions();
$this->options['expose']['sort_values'] = 1;
}
/**
* {@inheritdoc}
*/
public function buildExposeForm(&$form, FormStateInterface $form_state) {
parent::buildExposeForm($form, $form_state);
$form['expose']['sort_values'] = [
'#type' => 'radios',
'#title' => t('Value order'),
'#description' => t('How to sort the values in the exposed form'),
'#options' => [0 => $this->t('ASC'), 1 => $this->t('DESC')],
'#default_value' => $this->options['expose']['sort_values'],
'#weight' => 1,
];
// setup the default_value. we are forcing exposed = true, ajax
// does not have a chance to update the default values.
$form['expose']['label']['#default_value'] = $this->definition['title'];
$form['expose']['identifier']['#default_value'] = $this->options['id'];
}
/**
* No operator options, it's just IN
*
* @return array
*/
public function operatorOptions($wich = 'in') {
return [];
}
/**
* {@inheritdoc}
*/
public function getValueOptions() {
if (isset($this->valueOptions)) {
return $this->valueOptions;
}
// search for cloud_context from the route. If found, include that
// in the query
$cloud_context = \Drupal::routeMatch()->getParameter('cloud_context');
$allFilters = $this->displayHandler->getOption('filters');
$connection = \Drupal::database();
/** @var \Drupal\Core\Database\Query\Select $query */
$query = $connection->select($this->table, 'ft')
->fields('ft', [$this->realField])
->distinct(TRUE);
$query->orderBy($this->realField, ($this->options['expose']['sort_values'] == '1') ? 'desc' : 'asc');
if (isset($allFilters['type'])) {
$types = array_keys($allFilters['type']['value']);
$query->condition('bundle', $types, 'in');
}
if (isset($cloud_context)) {
$query->condition('cloud_context', $cloud_context);
}
// filter out null values
$query->isNotNull($this->realField);
$values = $query->execute()->fetchAllKeyed(0, 0);
$this->valueOptions = $values;
}
/**
* Only show the value form in the exposed form, for now there is no support
* for limiting the number of selectable options.
*
* {@inheritdoc}
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
protected function valueForm(&$form, FormStateInterface $form_state) {
$form['value'] = [];
$this->getValueOptions();
$options = $this->valueOptions;
if (count($options) && $form_state->get('exposed')) {
parent::valueForm($form, $form_state);
}
}
}
\ No newline at end of file
......@@ -1075,7 +1075,7 @@ class AwsEc2Service implements AwsEc2ServiceInterface {
'status' => $snapshot['State'],
'volume_id' => $snapshot['VolumeId'],
'progress' => $snapshot['Progress'],
'encrypted' => $snapshot['Encrypted'],
'encrypted' => $snapshot['Encrypted'] == FALSE ? 'Not Encrypted' : 'Encrypted',
'kms_key_id' => $snapshot['KmsKeyId'],
'owner_id' => $snapshot['OwnerId'],
'owner_aliases' => $snapshot['OwnerAlias'],
......
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