You are here

public function WebformSubmissionGenerate::getTestValue in Webform 8.5

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

Get test value for a webform element.

Parameters

\Drupal\webform\WebformInterface $webform: A webform.

string $name: The name of the element.

array $element: The FAPI element.

array $options: Options used to generate a test value.

Return value

array|int|null An array containing multiple values or a single value.

Overrides WebformSubmissionGenerateInterface::getTestValue

1 call to WebformSubmissionGenerate::getTestValue()
WebformSubmissionGenerate::getData in src/WebformSubmissionGenerate.php
Generate webform submission data.

File

src/WebformSubmissionGenerate.php, line 91

Class

WebformSubmissionGenerate
Webform submission generator.

Namespace

Drupal\webform

Code

public function getTestValue(WebformInterface $webform, $name, array $element, array $options = []) {

  // Set default options.
  $options += [
    // Return random test value(s).
    'random' => TRUE,
  ];

  /** @var \Drupal\webform\Plugin\WebformElementInterface $element_plugin */
  $plugin_id = $this->elementManager
    ->getElementPluginId($element);
  $element_plugin = $this->elementManager
    ->createInstance($plugin_id);

  // Exit if element does not have a value.
  if (!$element_plugin
    ->isInput($element)) {
    return NULL;
  }

  // Exit if test values are null or an empty array.
  $values = $this
    ->getTestValues($webform, $name, $element, $options);
  if ($values === NULL || is_array($values) && empty($values)) {
    return NULL;
  }

  // Make sure value is an array.
  if (!is_array($values)) {
    $values = [
      $values,
    ];
  }

  // Apply #maxlength to values.
  // @see \Drupal\webform\Plugin\WebformElement\TextBase
  if (!empty($element['#maxlength'])) {
    $maxlength = $element['#maxlength'];
  }
  elseif (!empty($element['#counter_type']) && !empty($element['#counter_maximum']) && $element['#counter_type'] === 'character') {
    $maxlength = $element['#counter_maximum'];
  }
  else {
    $maxlength = NULL;
  }
  if ($maxlength) {
    foreach ($values as $index => $value) {
      $values[$index] = mb_substr($value, 0, $maxlength);
    }
  }

  // $values = $this->tokenManager->replace($values, $webform);.
  // Elements that use multiple values require an array as the
  // default value.
  if ($element_plugin
    ->hasMultipleValues($element)) {
    if ($options['random']) {
      shuffle($values);
    }
    $limit = 3;
    if (isset($element['#multiple'])) {

      // #multiple: FALSE is only applicable to webform_custom_composite element.
      // @see \Drupal\webform\Plugin\WebformElement\WebformComposite
      if ($element['#multiple'] === FALSE) {
        $limit = 1;
      }
      elseif ($element['#multiple'] > 1 && $element['#multiple'] < 3) {
        $limit = $element['#multiple'];
      }
    }
    return array_slice($values, 0, $limit);
  }
  else {
    return $options['random'] ? $values[array_rand($values)] : reset($values);
  }
}