You are here

public function Webform::applyVariant in Webform 8.5

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

Apply webform variant.

Parameters

string $element_key: The variant element key.

string $instance_id: The variant instance id.

bool $force: Apply disabled variants. Defaults to FALSE.

Return value

bool Return TRUE is variant was applied.

1 call to Webform::applyVariant()
Webform::applyVariants in src/Entity/Webform.php
Apply webform variants based on a webform submission or parameter.

File

src/Entity/Webform.php, line 2991

Class

Webform
Defines the webform entity.

Namespace

Drupal\webform\Entity

Code

public function applyVariant($element_key, $instance_id, $force = FALSE) {
  $element = $this
    ->getElement($element_key);

  // Check that the webform has a variant instance.
  if (!$this
    ->getVariants()
    ->has($instance_id)) {
    $t_args = [
      '@title' => $element['#title'],
      '@key' => $element_key,
      '@instance_id' => $instance_id,
    ];

    // Log warning for missing variant instances.
    \Drupal::logger('webform')
      ->warning("The '@instance_id' variant id is missing for the '@title (@key)' variant type. <strong>No variant settings have been applied.</strong>", $t_args);

    // Display onscreen warning to users who can update the webform.
    if (\Drupal::currentUser()
      ->hasPermission('edit webform variants')) {
      \Drupal::messenger()
        ->addWarning($this
        ->t("The '@instance_id' variant id is missing for the '@title (@key)' variant type. <strong>No variant settings have been applied.</strong>", $t_args));
    }
    return FALSE;
  }
  $variant_plugin_id = $element['#variant'];
  $variant_plugin = $this
    ->getVariant($instance_id);

  // Check that the variant plugin id matches the element's variant plugin id.
  if ($variant_plugin_id !== $variant_plugin
    ->getPluginId()) {
    return FALSE;
  }

  // Check that the variant plugin instance is enabled.
  if (empty($force) && $variant_plugin
    ->isDisabled()) {
    return FALSE;
  }

  // Apply the variant.
  return $variant_plugin
    ->applyVariant();
}