You are here

protected function YamlFormEntityReferenceTrait::formatItems in YAML Form 8

Format an entity autocomplete targets as array of strings.

Parameters

array $element: An element.

array|mixed $value: A value.

array $options: An array of options.

Return value

array An entity autocomplete targets as array of strings

See also

\Drupal\yamlform\YamlFormSubmissionExporterInterface::formatRecordEntityAutocomplete

3 calls to YamlFormEntityReferenceTrait::formatItems()
YamlFormEntityReferenceTrait::buildExportRecord in src/Plugin/YamlFormElement/YamlFormEntityReferenceTrait.php
YamlFormEntityReferenceTrait::formatHtml in src/Plugin/YamlFormElement/YamlFormEntityReferenceTrait.php
YamlFormEntityReferenceTrait::formatText in src/Plugin/YamlFormElement/YamlFormEntityReferenceTrait.php

File

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

Class

YamlFormEntityReferenceTrait
Provides an 'entity_reference' trait.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

protected function formatItems(array &$element, $value, array $options) {
  list($entity_ids, $entities) = $this
    ->getTargetEntities($element, $value, $options);
  $format = $this
    ->getFormat($element);
  $items = [];
  foreach ($entity_ids as $entity_id) {
    $entity = isset($entities[$entity_id]) ? $entities[$entity_id] : NULL;
    switch ($format) {
      case 'id':
        $items[$entity_id] = $entity_id;
        break;
      case 'label':
        $items[$entity_id] = $entity ? $entity
          ->label() : $entity_id;
        break;
      case 'raw':
        $entity_type = $element['#target_type'];
        $items[$entity_id] = "{$entity_type}:{$entity_id}";
        break;
      case 'text':
      default:
        if ($entity) {

          // Use `sprintf` instead of FormattableMarkup because we really just
          // want a basic string.
          $items[$entity_id] = sprintf('%s (%s)', $entity
            ->label(), $entity
            ->id());
        }
        else {
          $items[$entity_id] = $entity_id;
        }
        break;
    }
  }
  return $items;
}