Action.php 3.44 KB
Newer Older
1 2
<?php

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 14

/**
 * Defines the configured action entity.
 *
15
 * @ConfigEntityType(
16 17
 *   id = "action",
 *   label = @Translation("Action"),
18 19 20 21 22 23 24
 *   label_collection = @Translation("Actions"),
 *   label_singular = @Translation("action"),
 *   label_plural = @Translation("actions"),
 *   label_count = @PluralTranslation(
 *     singular = "@count action",
 *     plural = "@count actions",
 *   ),
25
 *   admin_permission = "administer actions",
26 27
 *   entity_keys = {
 *     "id" = "id",
28
 *     "label" = "label"
29 30 31 32 33 34 35
 *   },
 *   config_export = {
 *     "id",
 *     "label",
 *     "type",
 *     "plugin",
 *     "configuration",
36 37 38
 *   }
 * )
 */
39
class Action extends ConfigEntityBase implements ActionConfigEntityInterface, EntityWithPluginCollectionInterface {
40 41 42 43 44 45

  /**
   * The name (plugin ID) of the action.
   *
   * @var string
   */
46
  protected $id;
47 48 49 50 51 52

  /**
   * The label of the action.
   *
   * @var string
   */
53
  protected $label;
54 55 56 57 58 59 60 61 62 63 64 65 66

  /**
   * The action type.
   *
   * @var string
   */
  protected $type;

  /**
   * The configuration of the action.
   *
   * @var array
   */
67
  protected $configuration = [];
68 69 70 71 72 73 74 75 76

  /**
   * The plugin ID of the action.
   *
   * @var string
   */
  protected $plugin;

  /**
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 87
   * @return \Drupal\Component\Plugin\LazyPluginCollection
   *   The action's plugin collection.
88
   */
89 90 91
  protected function getPluginCollection() {
    if (!$this->pluginCollection) {
      $this->pluginCollection = new ActionPluginCollection(\Drupal::service('plugin.manager.action'), $this->plugin, $this->configuration);
92
    }
93
    return $this->pluginCollection;
94 95
  }

96 97 98
  /**
   * {@inheritdoc}
   */
99
  public function getPluginCollections() {
100
    return ['configuration' => $this->getPluginCollection()];
101 102
  }

103 104 105 106
  /**
   * {@inheritdoc}
   */
  public function getPlugin() {
107
    return $this->getPluginCollection()->get($this->plugin);
108 109 110 111 112 113 114
  }

  /**
   * {@inheritdoc}
   */
  public function setPlugin($plugin_id) {
    $this->plugin = $plugin_id;
115
    $this->getPluginCollection()->addInstanceId($plugin_id);
116 117 118 119 120 121
  }

  /**
   * {@inheritdoc}
   */
  public function getPluginDefinition() {
122
    return $this->getPlugin()->getPluginDefinition();
123 124 125 126 127 128 129 130 131 132 133 134 135
  }

  /**
   * {@inheritdoc}
   */
  public function execute(array $entities) {
    return $this->getPlugin()->executeMultiple($entities);
  }

  /**
   * {@inheritdoc}
   */
  public function isConfigurable() {
136
    return $this->getPlugin() instanceof ConfigurablePluginInterface;
137 138 139 140 141 142 143 144 145 146 147 148
  }

  /**
   * {@inheritdoc}
   */
  public function getType() {
    return $this->type;
  }

  /**
   * {@inheritdoc}
   */
149 150 151
  public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
    /** @var \Drupal\system\ActionConfigEntityInterface $a */
    /** @var \Drupal\system\ActionConfigEntityInterface $b */
152 153 154 155 156 157 158 159 160
    $a_type = $a->getType();
    $b_type = $b->getType();
    if ($a_type != $b_type) {
      return strnatcasecmp($a_type, $b_type);
    }
    return parent::sort($a, $b);
  }

}