You are here

public function OverrideWebformVariant::applyVariant in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformVariant/OverrideWebformVariant.php \Drupal\webform\Plugin\WebformVariant\OverrideWebformVariant::applyVariant()

Apply variant to the webform.

Return value

bool TRUE if this variant was applied to the webform.

Overrides WebformVariantBase::applyVariant

File

src/Plugin/WebformVariant/OverrideWebformVariant.php, line 184

Class

OverrideWebformVariant
Webform override variant.

Namespace

Drupal\webform\Plugin\WebformVariant

Code

public function applyVariant() {
  $webform = $this
    ->getWebform();

  // Override settings.
  if ($this->configuration['settings']) {
    $settings = $webform
      ->getSettings();
    foreach ($this->configuration['settings'] as $setting_name => $setting_value) {
      if (isset($settings[$setting_name])) {
        $settings[$setting_name] = $setting_value;
      }
    }
    $webform
      ->setSettings($settings);
  }

  // Override elements.
  $elements = Yaml::decode($this->configuration['elements']) ?: [];
  if ($elements) {
    foreach ($elements as $element_key => $element_properties) {
      if (WebformElementHelper::property($element_key)) {

        // Set custom form property.
        $webform
          ->setElements([
          $element_key => $element_properties,
        ] + $webform
          ->getElementsDecoded());
      }
      else {
        $element = $webform
          ->getElement($element_key);
        if (!$element) {
          continue;
        }
        $webform
          ->setElementProperties($element_key, $element_properties + $element);
      }
    }
  }

  // Override handlers.
  if ($this->configuration['handlers']) {
    foreach ($this->configuration['handlers'] as $handler_id => $handler_configuration) {
      if (!$webform
        ->getHandlers()
        ->has($handler_id)) {
        continue;
      }
      $handler = $webform
        ->getHandler($handler_id);
      $configuration = $handler
        ->getConfiguration();
      foreach ($handler_configuration as $configuration_key => $configuration_value) {
        if (!isset($configuration[$configuration_key])) {
          continue;
        }
        if ($configuration_key === 'settings') {
          $configuration[$configuration_key] = $configuration_value + $configuration[$configuration_key];
        }
        else {
          $configuration[$configuration_key] = $configuration_value;
        }
      }
      $handler
        ->setConfiguration($configuration);
    }
  }

  // Debug.
  $this
    ->debug();
  return TRUE;
}