You are here

public function OptionsBase::buildExportRecord in YAML Form 8

Build an element's export row.

Parameters

array $element: An element.

array $export_options: An associative array of export options.

Return value

array An array containing the element's export row.

Overrides YamlFormElementBase::buildExportRecord

See also

\Drupal\yamlform\YamlFormSubmissionExporterInterface::getDefaultExportOptions

File

src/Plugin/YamlFormElement/OptionsBase.php, line 367

Class

OptionsBase
Provides a base 'options' element.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

public function buildExportRecord(array $element, $value, array $export_options) {
  $element_options = $element['#options'];
  $record = [];
  if ($export_options['options_format'] == 'separate') {

    // Combine the values so that isset can be used instead of in_array().
    // http://stackoverflow.com/questions/13483219/what-is-faster-in-array-or-isset
    $deltas = FALSE;
    if (is_array($value)) {
      $value = array_combine($value, $value);
      $deltas = $this->exportDelta ? array_flip(array_values($value)) : FALSE;
    }

    // Separate multiple values (ie options).
    foreach ($element_options as $option_value => $option_text) {
      if (is_array($value) && isset($value[$option_value]) || $value == $option_value) {
        $record[] = $deltas ? $deltas[$option_value] + 1 : 'X';
      }
      else {
        $record[] = '';
      }
    }
  }
  else {

    // Handle multiple values with options.
    if (is_array($value)) {
      if ($export_options['options_item_format'] == 'label') {
        $value = YamlFormOptionsHelper::getOptionsText($value, $element_options);
      }
      $record[] = implode(',', $value);
    }
    elseif ($export_options['options_item_format'] == 'label') {
      $record[] = YamlFormOptionsHelper::getOptionText($value, $element_options);
    }
    else {
      $record[] = $value;
    }
  }
  return $record;
}