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