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 location.

This commit is contained in:
Michael Stenta 2021-06-01 20:05:16 -04:00
parent fda3f325ac
commit 2ac1a0d41e
2 changed files with 47 additions and 0 deletions

View file

@ -68,6 +68,9 @@ function farm_location_views_data_alter(array &$data) {
'id' => 'asset_location',
'field_name' => 'location',
],
'argument' => [
'id' => 'asset_location',
],
];
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace Drupal\farm_location\Plugin\views\argument;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
/**
* An argument for filtering assets by their current location.
*
* @ingroup views_argument_handlers
*
* @ViewsArgument("asset_location")
*/
class AssetLocation extends ArgumentPluginBase {
/**
* {@inheritdoc}
*/
public function query($group_by = FALSE) {
// First query for a list of asset IDs in the location, 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 asset.location service's getAssetsByLocation().
// 2. We need to allow other modules to override the asset.location service
// to provide their own location logic (eg: the Group asset module). 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/3217168 for more info.
$location = \Drupal::entityTypeManager()->getStorage('asset')->load($this->argument);
$assets = \Drupal::service('asset.location')->getAssetsByLocation($location);
$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');
}
}