You are here

public function ContactStorageExportService::serialize in Contact Storage Export 8

Returns a serialized message.

Parameters

\Drupal\contact\MessageInterface $message: The message to serialize.

string[] $settings: (optional) A settings array containing:

  • columns (A list of columns/fields to include)
  • date_format (Format to use with date fields)
  • labels (A list of columns labels)

Return value

array The serialized contact message object data.

1 call to ContactStorageExportService::serialize()
ContactStorageExportService::encode in src/ContactStorageExportService.php
Returns the encoded contact messages.

File

src/ContactStorageExportService.php, line 73

Class

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

Namespace

Drupal\contact_storage_export

Code

public function serialize(MessageInterface $message, array $settings = []) {
  $labels = isset($settings['labels']) ? $settings['labels'] : $this
    ->getLabels($message);
  $all_keys = array_keys($labels);
  $selected_keys = isset($settings['columns']) ? array_keys($settings['columns']) : $all_keys;
  $excluded_keys = array_diff($all_keys, $selected_keys);

  // Exclude UUID field.
  $excluded_keys[] = 'uuid';
  $values = [];
  foreach ($message
    ->getFields() as $field_name => $definition) {

    // Exclude marked columns.
    if (in_array($field_name, $excluded_keys)) {
      continue;
    }

    // Set the keys to readable labels and format data for CSV serialization.
    $values[$labels[$field_name]] = $this
      ->getFormattedValue($message, $field_name, $settings);
  }
  return $values;
}