You are here

public function Webform::getVariants in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Entity/Webform.php \Drupal\webform\Entity\Webform::getVariants()

Returns the webform variants for this webform.

Parameters

string $plugin_id: (optional) Plugin id used to return specific plugin instances (i.e. variants).

bool $status: (optional) Status used to return enabled or disabled plugin instances (i.e. variants).

bool $element_key: (optional) Element key used to return enabled or disabled plugin instances (i.e. variants).

Return value

\Drupal\webform\Plugin\WebformVariantPluginCollection|\Drupal\webform\Plugin\WebformVariantInterface[] The webform variant plugin collection.

Overrides WebformInterface::getVariants

3 calls to Webform::getVariants()
Webform::deleteElement in src/Entity/Webform.php
Remove an element.
Webform::getPluginCollections in src/Entity/Webform.php
Gets the plugin collections used by this object.
Webform::onDependencyRemoval in src/Entity/Webform.php
Informs the entity that entities it depends on will be deleted.

File

src/Entity/Webform.php, line 2839

Class

Webform
Defines the webform entity.

Namespace

Drupal\webform\Entity

Code

public function getVariants($plugin_id = NULL, $status = NULL, $element_key = NULL) {
  if (!$this->variantsCollection) {
    $this->variantsCollection = new WebformVariantPluginCollection($this
      ->getWebformVariantPluginManager(), $this->variants);

    /** @var \Drupal\webform\Plugin\WebformVariantBase $variant */
    foreach ($this->variantsCollection as $variant) {

      // Initialize the variant and pass in the webform.
      $variant
        ->setWebform($this);
    }
    $this->variantsCollection
      ->sort();
  }

  /** @var \Drupal\webform\Plugin\WebformVariantPluginCollection $variants */
  $variants = $this->variantsCollection;

  // Clone the variants if they are being filtered.
  if (isset($plugin_id) || isset($status) || isset($element_key)) {

    /** @var \Drupal\webform\Plugin\WebformVariantPluginCollection $variants */
    $variants = clone $this->variantsCollection;
  }

  // Filter the variants by plugin id.
  // This is used to limit track and enforce a variants cardinality.
  if (isset($plugin_id)) {
    foreach ($variants as $instance_id => $variant) {
      if ($variant
        ->getPluginId() !== $plugin_id) {
        $variants
          ->removeInstanceId($instance_id);
      }
    }
  }

  // Filter the variants by status.
  // This is used to limit track and enforce a variants cardinality.
  if (isset($status)) {
    foreach ($variants as $instance_id => $variant) {
      if ($variant
        ->getStatus() !== $status) {
        $variants
          ->removeInstanceId($instance_id);
      }
    }
  }

  // Filter the variants by element key.
  if (isset($element_key)) {
    foreach ($variants as $instance_id => $variant) {
      if ($variant
        ->getElementKey() !== $element_key) {
        $variants
          ->removeInstanceId($instance_id);
      }
    }
  }
  return $variants;
}