3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Provide a Views argument handler for filtering assets by group.

This commit is contained in:
Michael Stenta 2021-06-04 07:40:18 -04:00
parent b04d5eba6b
commit 390d69fe3e
2 changed files with 49 additions and 0 deletions

View file

@ -41,6 +41,9 @@ function farm_group_views_data_alter(array &$data) {
'id' => 'asset_group',
'field_name' => 'group',
],
'argument' => [
'id' => 'asset_group',
],
];
}

View file

@ -0,0 +1,46 @@
<?php
namespace Drupal\farm_group\Plugin\views\argument;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
/**
* An argument for filtering assets by their current group.
*
* @ingroup views_argument_handlers
*
* @ViewsArgument("asset_group")
*/
class AssetGroup extends ArgumentPluginBase {
/**
* {@inheritdoc}
*
* @see \Drupal\farm_location\Plugin\views\argument\AssetLocation
*/
public function query($group_by = FALSE) {
// First query for a list of asset IDs in the group, then use this list to
// filter the current View.
// We do this in two separate queries for a few reasons:
// 1. The Drupal and Views query APIs do not support the kind of compound
// JOIN that we use in the group.membership service's getMembers().
// 2. We need to allow other modules to override the group.membership
// service to provide their own location logic. They shouldn't have to
// override this Views argument handler as well.
// 3. It keeps this Views argument handler's query modifications very
// simple. It only needs the condition: "WHERE asset.id IN (:asset_ids)".
// See https://www.drupal.org/project/farm/issues/3217184 for more info.
$group = \Drupal::entityTypeManager()->getStorage('asset')->load($this->argument);
$assets = \Drupal::service('group.membership')->getGroupMembers($group);
$asset_ids = [];
foreach ($assets as $asset) {
$asset_ids[] = $asset->id();
}
// Filter to only include assets with those IDs.
$this->ensureMyTable();
$this->query->addWhere(0, "$this->tableAlias.id", $asset_ids, 'IN');
}
}