You are here

public function WebformEntityReferenceTrait::buildExportRecord in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/WebformEntityReferenceTrait.php \Drupal\webform\Plugin\WebformElement\WebformEntityReferenceTrait::buildExportRecord()

File

src/Plugin/WebformElement/WebformEntityReferenceTrait.php, line 316

Class

WebformEntityReferenceTrait
Provides an 'entity_reference' trait.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function buildExportRecord(array $element, WebformSubmissionInterface $webform_submission, array $export_options) {
  $value = $this
    ->getValue($element, $webform_submission);
  $default_options = $this
    ->getExportDefaultOptions();
  $entity_reference_items = isset($export_options['entity_reference_items']) ? $export_options['entity_reference_items'] : $default_options['entity_reference_items'];
  if (!$this
    ->hasMultipleValues($element)) {
    $entity_type = $this
      ->getTargetType($element);
    $entity_storage = $this->entityTypeManager
      ->getStorage($entity_type);
    $entity_id = $value;
    $record = [];
    if ($entity_id && ($entity = $entity_storage
      ->load($entity_id))) {
      foreach ($entity_reference_items as $column) {
        switch ($column) {
          case 'id':
            $record[] = $entity
              ->id();
            break;
          case 'title':
            $record[] = $entity
              ->label();
            break;
          case 'url':
            if ($entity
              ->hasLinkTemplate('canonical')) {
              $record[] = $entity
                ->toUrl('canonical', [
                'absolute' => TRUE,
              ])
                ->toString();
            }
            else {
              $record[] = '';
            }
            break;
        }
      }
    }
    else {
      foreach ($entity_reference_items as $column) {
        switch ($column) {
          case 'id':
            $record[] = $entity_id;
            break;
          case 'title':
            $record[] = '';
            break;
          case 'url':
            $record[] = '';
            break;
        }
      }
    }
    return $record;
  }
  else {
    if ($entity_reference_items === [
      'id',
    ]) {
      $element['#format'] = 'raw';
    }
    return parent::buildExportRecord($element, $webform_submission, $export_options);
  }
}