mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
Use dependency injection in views argument plugins.
This commit is contained in:
parent
937001a13a
commit
50d7f643c2
2 changed files with 105 additions and 4 deletions
|
@ -2,7 +2,10 @@
|
||||||
|
|
||||||
namespace Drupal\farm_group\Plugin\views\argument;
|
namespace Drupal\farm_group\Plugin\views\argument;
|
||||||
|
|
||||||
|
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||||
|
use Drupal\farm_group\GroupMembershipInterface;
|
||||||
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
|
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An argument for filtering assets by their current group.
|
* An argument for filtering assets by their current group.
|
||||||
|
@ -13,6 +16,53 @@ use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
|
||||||
*/
|
*/
|
||||||
class AssetGroup extends ArgumentPluginBase {
|
class AssetGroup extends ArgumentPluginBase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The entity type manager service.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
||||||
|
*/
|
||||||
|
protected $entityTypeManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The group membership service.
|
||||||
|
*
|
||||||
|
* @var \Drupal\farm_group\GroupMembershipInterface
|
||||||
|
*/
|
||||||
|
protected $groupMembership;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an AssetGroup object.
|
||||||
|
*
|
||||||
|
* @param array $configuration
|
||||||
|
* A configuration array containing information about the plugin instance.
|
||||||
|
* @param string $plugin_id
|
||||||
|
* The plugin_id for the plugin instance.
|
||||||
|
* @param mixed $plugin_definition
|
||||||
|
* The plugin implementation definition.
|
||||||
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
||||||
|
* The entity type manager service.
|
||||||
|
* @param \Drupal\farm_group\GroupMembershipInterface $group_membership
|
||||||
|
* The group membership service.
|
||||||
|
*/
|
||||||
|
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, GroupMembershipInterface $group_membership) {
|
||||||
|
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||||
|
$this->entityTypeManager = $entity_type_manager;
|
||||||
|
$this->groupMembership = $group_membership;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||||
|
return new static(
|
||||||
|
$configuration,
|
||||||
|
$plugin_id,
|
||||||
|
$plugin_definition,
|
||||||
|
$container->get('entity_type.manager'),
|
||||||
|
$container->get('group.membership'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*
|
*
|
||||||
|
@ -31,8 +81,8 @@ class AssetGroup extends ArgumentPluginBase {
|
||||||
// 3. It keeps this Views argument handler's query modifications very
|
// 3. It keeps this Views argument handler's query modifications very
|
||||||
// simple. It only needs the condition: "WHERE asset.id IN (:asset_ids)".
|
// simple. It only needs the condition: "WHERE asset.id IN (:asset_ids)".
|
||||||
// See https://www.drupal.org/project/farm/issues/3217184 for more info.
|
// See https://www.drupal.org/project/farm/issues/3217184 for more info.
|
||||||
$group = \Drupal::entityTypeManager()->getStorage('asset')->load($this->argument);
|
$group = $this->entityTypeManager->getStorage('asset')->load($this->argument);
|
||||||
$assets = \Drupal::service('group.membership')->getGroupMembers([$group]);
|
$assets = $this->groupMembership->getGroupMembers([$group]);
|
||||||
$asset_ids = [];
|
$asset_ids = [];
|
||||||
foreach ($assets as $asset) {
|
foreach ($assets as $asset) {
|
||||||
$asset_ids[] = $asset->id();
|
$asset_ids[] = $asset->id();
|
||||||
|
|
|
@ -2,7 +2,10 @@
|
||||||
|
|
||||||
namespace Drupal\farm_location\Plugin\views\argument;
|
namespace Drupal\farm_location\Plugin\views\argument;
|
||||||
|
|
||||||
|
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||||
|
use Drupal\farm_location\AssetLocationInterface;
|
||||||
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
|
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An argument for filtering assets by their current location.
|
* An argument for filtering assets by their current location.
|
||||||
|
@ -13,6 +16,53 @@ use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
|
||||||
*/
|
*/
|
||||||
class AssetLocation extends ArgumentPluginBase {
|
class AssetLocation extends ArgumentPluginBase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The entity type manager service.
|
||||||
|
*
|
||||||
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
||||||
|
*/
|
||||||
|
protected $entityTypeManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The asset location service.
|
||||||
|
*
|
||||||
|
* @var \Drupal\farm_location\AssetLocationInterface
|
||||||
|
*/
|
||||||
|
protected $assetLocation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an AssetLocation object.
|
||||||
|
*
|
||||||
|
* @param array $configuration
|
||||||
|
* A configuration array containing information about the plugin instance.
|
||||||
|
* @param string $plugin_id
|
||||||
|
* The plugin_id for the plugin instance.
|
||||||
|
* @param mixed $plugin_definition
|
||||||
|
* The plugin implementation definition.
|
||||||
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
||||||
|
* The entity type manager service.
|
||||||
|
* @param \Drupal\farm_location\AssetLocationInterface $asset_location
|
||||||
|
* The asset location service.
|
||||||
|
*/
|
||||||
|
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, AssetLocationInterface $asset_location) {
|
||||||
|
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||||
|
$this->entityTypeManager = $entity_type_manager;
|
||||||
|
$this->assetLocation = $asset_location;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||||
|
return new static(
|
||||||
|
$configuration,
|
||||||
|
$plugin_id,
|
||||||
|
$plugin_definition,
|
||||||
|
$container->get('entity_type.manager'),
|
||||||
|
$container->get('asset.location'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
@ -29,8 +79,9 @@ class AssetLocation extends ArgumentPluginBase {
|
||||||
// 3. It keeps this Views argument handler's query modifications very
|
// 3. It keeps this Views argument handler's query modifications very
|
||||||
// simple. It only needs the condition: "WHERE asset.id IN (:asset_ids)".
|
// simple. It only needs the condition: "WHERE asset.id IN (:asset_ids)".
|
||||||
// See https://www.drupal.org/project/farm/issues/3217168 for more info.
|
// See https://www.drupal.org/project/farm/issues/3217168 for more info.
|
||||||
$location = \Drupal::entityTypeManager()->getStorage('asset')->load($this->argument);
|
/** @var \Drupal\asset\Entity\AssetInterface $location */
|
||||||
$assets = \Drupal::service('asset.location')->getAssetsByLocation($location);
|
$location = $this->entityTypeManager->getStorage('asset')->load($this->argument);
|
||||||
|
$assets = $this->assetLocation->getAssetsByLocation($location);
|
||||||
$asset_ids = [];
|
$asset_ids = [];
|
||||||
foreach ($assets as $asset) {
|
foreach ($assets as $asset) {
|
||||||
$asset_ids[] = $asset->id();
|
$asset_ids[] = $asset->id();
|
||||||
|
|
Loading…
Reference in a new issue