You are here

public function ContactStorageExportService::getFormattedValue in Contact Storage Export 8

Formats values of the given field.

Parameters

\Drupal\contact\MessageInterface $message: A message entity.

string $field: A field name.

array $settings: A settings array.

Return value

array Formatted value.

1 call to ContactStorageExportService::getFormattedValue()
ContactStorageExportService::serialize in src/ContactStorageExportService.php
Returns a serialized message.

File

src/ContactStorageExportService.php, line 110

Class

ContactStorageExportService
Provides a set of methods to export contact messages in CSV format.

Namespace

Drupal\contact_storage_export

Code

public function getFormattedValue(MessageInterface $message, $field, array $settings) {

  // Get the field storage definition and the field type.
  $storage_definition = $message
    ->getFieldDefinition($field)
    ->getFieldStorageDefinition();
  $type = $storage_definition
    ->getType();
  $values = [];

  // Iterate over field items and format its values.

  /** @var \Drupal\Core\Field\FieldItemInterface $item */
  foreach ($message->{$field} as $item) {
    switch ($type) {
      case 'link':

        /** @var \Drupal\link\LinkItemInterface $item */
        $values[] = $this
          ->formatLink($item);
        break;
      case 'created':
      case 'daterange':
      case 'timestamp':
      case 'datetime':
        $date_format = isset($settings['date_format']) ? $settings['date_format'] : NULL;
        $values[] = $this
          ->formatDateTime($item, $date_format);
        break;
      case 'entity_reference':

        /** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item */
        $values[] = $this
          ->formatEntityReference($item, $storage_definition
          ->getMainPropertyName());
        break;
      default:

        // Display a field item value using default field formatter.
        $render_array = $item
          ->view();
        $values[] = $this
          ->renderAsString($render_array);
    }
  }
  return $values;
}