98a6f9a60ea4042885acb484e63632bbc90735a8
[yaffs-website] / Plugin / VariantCollectionTrait.php
1 <?php
2
3 namespace Drupal\ctools\Plugin;
4
5 /**
6  * Provides methods for VariantCollectionInterface.
7  */
8 trait VariantCollectionTrait {
9
10   /**
11    * The plugin collection that holds the variants.
12    *
13    * @var \Drupal\ctools\Plugin\VariantPluginCollection
14    */
15   protected $variantCollection;
16
17   /**
18    * @see \Drupal\ctools\Plugin\VariantCollectionInterface::addVariant()
19    */
20   public function addVariant(array $configuration) {
21     $configuration['uuid'] = $this->uuidGenerator()->generate();
22     $this->getVariants()->addInstanceId($configuration['uuid'], $configuration);
23     return $configuration['uuid'];
24   }
25
26   /**
27    * @see \Drupal\ctools\Plugin\VariantCollectionInterface::getVariant()
28    */
29   public function getVariant($variant_id) {
30     return $this->getVariants()->get($variant_id);
31   }
32
33   /**
34    * @see \Drupal\ctools\Plugin\VariantCollectionInterface::removeVariant()
35    */
36   public function removeVariant($variant_id) {
37     $this->getVariants()->removeInstanceId($variant_id);
38     return $this;
39   }
40
41   /**
42    * @see \Drupal\ctools\Plugin\VariantCollectionInterface::getVariants()
43    */
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();
48     }
49     return $this->variantCollection;
50   }
51
52   /**
53    * Returns the configuration for stored variants.
54    *
55    * @return array
56    *   An array of variant configuration, keyed by the unique variant ID.
57    */
58   abstract protected function getVariantConfig();
59
60   /**
61    * Returns the UUID generator.
62    *
63    * @return \Drupal\Component\Uuid\UuidInterface
64    */
65   abstract protected function uuidGenerator();
66
67 }