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