Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\system\Entity;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
|
Chris@0
|
6 use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
Chris@0
|
7 use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
|
Chris@0
|
8 use Drupal\system\ActionConfigEntityInterface;
|
Chris@0
|
9 use Drupal\Core\Action\ActionPluginCollection;
|
Chris@0
|
10 use Drupal\Component\Plugin\ConfigurablePluginInterface;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Defines the configured action entity.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @ConfigEntityType(
|
Chris@0
|
16 * id = "action",
|
Chris@0
|
17 * label = @Translation("Action"),
|
Chris@0
|
18 * admin_permission = "administer actions",
|
Chris@0
|
19 * entity_keys = {
|
Chris@0
|
20 * "id" = "id",
|
Chris@0
|
21 * "label" = "label"
|
Chris@0
|
22 * },
|
Chris@0
|
23 * config_export = {
|
Chris@0
|
24 * "id",
|
Chris@0
|
25 * "label",
|
Chris@0
|
26 * "type",
|
Chris@0
|
27 * "plugin",
|
Chris@0
|
28 * "configuration",
|
Chris@0
|
29 * }
|
Chris@0
|
30 * )
|
Chris@0
|
31 */
|
Chris@0
|
32 class Action extends ConfigEntityBase implements ActionConfigEntityInterface, EntityWithPluginCollectionInterface {
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * The name (plugin ID) of the action.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var string
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $id;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * The label of the action.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @var string
|
Chris@0
|
45 */
|
Chris@0
|
46 protected $label;
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * The action type.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @var string
|
Chris@0
|
52 */
|
Chris@0
|
53 protected $type;
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * The configuration of the action.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @var array
|
Chris@0
|
59 */
|
Chris@0
|
60 protected $configuration = [];
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * The plugin ID of the action.
|
Chris@0
|
64 *
|
Chris@0
|
65 * @var string
|
Chris@0
|
66 */
|
Chris@0
|
67 protected $plugin;
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * The plugin collection that stores action plugins.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @var \Drupal\Core\Action\ActionPluginCollection
|
Chris@0
|
73 */
|
Chris@0
|
74 protected $pluginCollection;
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * Encapsulates the creation of the action's LazyPluginCollection.
|
Chris@0
|
78 *
|
Chris@0
|
79 * @return \Drupal\Component\Plugin\LazyPluginCollection
|
Chris@0
|
80 * The action's plugin collection.
|
Chris@0
|
81 */
|
Chris@0
|
82 protected function getPluginCollection() {
|
Chris@0
|
83 if (!$this->pluginCollection) {
|
Chris@0
|
84 $this->pluginCollection = new ActionPluginCollection(\Drupal::service('plugin.manager.action'), $this->plugin, $this->configuration);
|
Chris@0
|
85 }
|
Chris@0
|
86 return $this->pluginCollection;
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * {@inheritdoc}
|
Chris@0
|
91 */
|
Chris@0
|
92 public function getPluginCollections() {
|
Chris@0
|
93 return ['configuration' => $this->getPluginCollection()];
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * {@inheritdoc}
|
Chris@0
|
98 */
|
Chris@0
|
99 public function getPlugin() {
|
Chris@0
|
100 return $this->getPluginCollection()->get($this->plugin);
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * {@inheritdoc}
|
Chris@0
|
105 */
|
Chris@0
|
106 public function setPlugin($plugin_id) {
|
Chris@0
|
107 $this->plugin = $plugin_id;
|
Chris@0
|
108 $this->getPluginCollection()->addInstanceId($plugin_id);
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 /**
|
Chris@0
|
112 * {@inheritdoc}
|
Chris@0
|
113 */
|
Chris@0
|
114 public function getPluginDefinition() {
|
Chris@0
|
115 return $this->getPlugin()->getPluginDefinition();
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * {@inheritdoc}
|
Chris@0
|
120 */
|
Chris@0
|
121 public function execute(array $entities) {
|
Chris@0
|
122 return $this->getPlugin()->executeMultiple($entities);
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * {@inheritdoc}
|
Chris@0
|
127 */
|
Chris@0
|
128 public function isConfigurable() {
|
Chris@0
|
129 return $this->getPlugin() instanceof ConfigurablePluginInterface;
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * {@inheritdoc}
|
Chris@0
|
134 */
|
Chris@0
|
135 public function getType() {
|
Chris@0
|
136 return $this->type;
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 /**
|
Chris@0
|
140 * {@inheritdoc}
|
Chris@0
|
141 */
|
Chris@0
|
142 public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
|
Chris@0
|
143 /** @var \Drupal\system\ActionConfigEntityInterface $a */
|
Chris@0
|
144 /** @var \Drupal\system\ActionConfigEntityInterface $b */
|
Chris@0
|
145 $a_type = $a->getType();
|
Chris@0
|
146 $b_type = $b->getType();
|
Chris@0
|
147 if ($a_type != $b_type) {
|
Chris@0
|
148 return strnatcasecmp($a_type, $b_type);
|
Chris@0
|
149 }
|
Chris@0
|
150 return parent::sort($a, $b);
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 }
|