trait VariantCollectionTrait

Same name in other branches
  1. 4.0.x src/Plugin/VariantCollectionTrait.php \Drupal\ctools\Plugin\VariantCollectionTrait

Provides methods for VariantCollectionInterface.

Hierarchy

1 file declares its use of VariantCollectionTrait
VariantCollectionTraitTest.php in tests/src/Unit/VariantCollectionTraitTest.php

File

src/Plugin/VariantCollectionTrait.php, line 8

Namespace

Drupal\ctools\Plugin
View source
trait VariantCollectionTrait {
    
    /**
     * The plugin collection that holds the variants.
     *
     * @var \Drupal\ctools\Plugin\VariantPluginCollection
     */
    protected $variantCollection;
    
    /**
     * @see \Drupal\ctools\Plugin\VariantCollectionInterface::addVariant()
     */
    public function addVariant(array $configuration) {
        $configuration['uuid'] = $this->uuidGenerator()
            ->generate();
        $this->getVariants()
            ->addInstanceId($configuration['uuid'], $configuration);
        return $configuration['uuid'];
    }
    
    /**
     * @see \Drupal\ctools\Plugin\VariantCollectionInterface::getVariant()
     */
    public function getVariant($variant_id) {
        return $this->getVariants()
            ->get($variant_id);
    }
    
    /**
     * @see \Drupal\ctools\Plugin\VariantCollectionInterface::removeVariant()
     */
    public function removeVariant($variant_id) {
        $this->getVariants()
            ->removeInstanceId($variant_id);
        return $this;
    }
    
    /**
     * @see \Drupal\ctools\Plugin\VariantCollectionInterface::getVariants()
     */
    public function getVariants() {
        if (!$this->variantCollection) {
            $this->variantCollection = new VariantPluginCollection(\Drupal::service('plugin.manager.display_variant'), $this->getVariantConfig());
            $this->variantCollection
                ->sort();
        }
        return $this->variantCollection;
    }
    
    /**
     * Returns the configuration for stored variants.
     *
     * @return array
     *   An array of variant configuration, keyed by the unique variant ID.
     */
    protected abstract function getVariantConfig();
    
    /**
     * Returns the UUID generator.
     *
     * @return \Drupal\Component\Uuid\UuidInterface
     */
    protected abstract function uuidGenerator();

}

Members

Title Sort descending Modifiers Object type Summary Overrides
VariantCollectionTrait::$variantCollection protected property The plugin collection that holds the variants.
VariantCollectionTrait::addVariant public function
VariantCollectionTrait::getVariant public function
VariantCollectionTrait::getVariantConfig abstract protected function Returns the configuration for stored variants. 1
VariantCollectionTrait::getVariants public function
VariantCollectionTrait::removeVariant public function
VariantCollectionTrait::uuidGenerator abstract protected function Returns the UUID generator. 1