You are here

public function YamlFormEntityReferenceTrait::buildExportRecord in YAML Form 8

File

src/Plugin/YamlFormElement/YamlFormEntityReferenceTrait.php, line 177

Class

YamlFormEntityReferenceTrait
Provides an 'entity_reference' trait.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

public function buildExportRecord(array $element, $value, array $options) {
  if ($this
    ->hasMultipleValues($element)) {
    $element = [
      '#format' => 'text',
    ] + $element;
    $items = $this
      ->formatItems($element, $value, $options);
    return [
      implode(', ', $items),
    ];
  }
  if ($options['entity_reference_format'] == 'link') {
    $entity_type = $element['#target_type'];
    $entity_storage = $this->entityTypeManager
      ->getStorage($entity_type);
    $entity_id = $value;
    $record = [];
    if ($entity_id && ($entity = $entity_storage
      ->load($entity_id))) {
      $record[] = $entity
        ->id();
      $record[] = $entity
        ->label();
      $record[] = $entity
        ->toUrl('canonical', [
        'absolute' => TRUE,
      ])
        ->toString();
    }
    else {
      $record[] = "{$entity_type}:{$entity_id}";
      $record[] = '';
      $record[] = '';
    }
    return $record;
  }
  else {
    return parent::buildExportRecord($element, $value, $options);
  }
}