-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathEntityFetchByView.php
More file actions
106 lines (90 loc) · 3.33 KB
/
EntityFetchByView.php
File metadata and controls
106 lines (90 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace Drupal\rules\Plugin\RulesAction;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\rules\Core\RulesActionBase;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a generic 'Fetch entities by view' action.
*
* @RulesAction(
* id = "rules_entity_fetch_by_view",
* deriver = "Drupal\rules\Plugin\RulesAction\EntityFetchByViewDeriver",
* category = @Translation("Entity")
* )
*/
class EntityFetchByView extends RulesActionBase implements ContainerFactoryPluginInterface {
/**
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $viewStorage;
/**
* Constructs an EntityFetchByView 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.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->viewStorage = $entity_type_manager->getStorage('view');
}
/**
* {@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')
);
}
/**
* {@inheritdoc}
*/
public function execute() {
$view_id = $this->pluginDefinition['view_id'];
$display_id = $this->pluginDefinition['display_id'];
// Fetch the list of available contexts.
$contexts = $this->getContexts();
// Pull values out of contexts.
$contexts = array_map(function ($context) {
return $context->getContextData()->getValue();
}, $contexts);
// Convert entities into entity ids.
$contexts = array_map(function ($context) {
return $context instanceof EntityInterface ? $context->id() : $context;
}, $contexts);
// Request the views executable for the current display.
$view = $this->viewStorage->load($view_id)->getExecutable();
$view->setDisplay($display_id);
$arguments = [];
// Reverse- loop through the views contextual arguments and skip empty
// arguments until the first defined one.
foreach (array_reverse(array_keys($view->display_handler->getOption('arguments'))) as $arg) {
if ($contexts[$arg] == '' && count($arguments) == 0) {
continue;
}
$arguments[$arg] = $contexts[$arg];
}
// Execute the view and pass the result as provided value.
$view->setArguments($arguments);
$entities = $view->render($this->pluginDefinition['display_id']) ?: [];
$this->setProvidedValue('entity_fetched', $entities);
}
}