3 namespace Drupal\ctools\Plugin;
6 * Provides methods for VariantCollectionInterface.
8 trait VariantCollectionTrait {
11 * The plugin collection that holds the variants.
13 * @var \Drupal\ctools\Plugin\VariantPluginCollection
15 protected $variantCollection;
18 * @see \Drupal\ctools\Plugin\VariantCollectionInterface::addVariant()
20 public function addVariant(array $configuration) {
21 $configuration['uuid'] = $this->uuidGenerator()->generate();
22 $this->getVariants()->addInstanceId($configuration['uuid'], $configuration);
23 return $configuration['uuid'];
27 * @see \Drupal\ctools\Plugin\VariantCollectionInterface::getVariant()
29 public function getVariant($variant_id) {
30 return $this->getVariants()->get($variant_id);
34 * @see \Drupal\ctools\Plugin\VariantCollectionInterface::removeVariant()
36 public function removeVariant($variant_id) {
37 $this->getVariants()->removeInstanceId($variant_id);
42 * @see \Drupal\ctools\Plugin\VariantCollectionInterface::getVariants()
44 public function getVariants() {
45 if (!$this->variantCollection) {
46 $this->variantCollection = new VariantPluginCollection(\Drupal::service('plugin.manager.display_variant'), $this->getVariantConfig());
47 $this->variantCollection->sort();
49 return $this->variantCollection;
53 * Returns the configuration for stored variants.
56 * An array of variant configuration, keyed by the unique variant ID.
58 abstract protected function getVariantConfig();
61 * Returns the UUID generator.
63 * @return \Drupal\Component\Uuid\UuidInterface
65 abstract protected function uuidGenerator();