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