function Exporter::exportTranslation

Exports a single translation of a content entity.

Any fields that are explicitly marked non-exportable (including computed properties by default) will not be exported.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $translation: The translation to export.

\Drupal\Core\DefaultContent\ExportMetadata $metadata: Any metadata about the entity being exported (e.g., dependencies).

callable[] $callbacks: Custom export functions for specific field types, keyed by field type.

array<string, bool> $allow_list: An array of booleans that indicate whether a specific field should be exported or not, even if it is computed. Keyed by field name.

Return value

array The exported translation.

1 call to Exporter::exportTranslation()
Exporter::export in core/lib/Drupal/Core/DefaultContent/Exporter.php
Exports a single content entity to a file.

File

core/lib/Drupal/Core/DefaultContent/Exporter.php, line 115

Class

Exporter
Handles exporting content entities.

Namespace

Drupal\Core\DefaultContent

Code

private function exportTranslation(ContentEntityInterface $translation, ExportMetadata $metadata, array $callbacks, array $allow_list) : array {
  $data = [];
  foreach ($translation->getFields() as $name => $items) {
    // Skip the field if it's empty, or it was explicitly disallowed, or is a
    // computed field that wasn't explicitly allowed.
    $allowed = $allow_list[$name] ?? NULL;
    if ($allowed === FALSE || $allowed === NULL && $items->getDataDefinition()
      ->isComputed() || $items->isEmpty()) {
      continue;
    }
    // Try to find a callback for this specific field, then for the field's
    // data type, and finally fall back to a generic callback.
    $data_type = $items->getFieldDefinition()
      ->getItemDefinition()
      ->getDataType();
    $callback = $callbacks[$name] ?? $callbacks[$data_type] ?? $this->exportFieldItem(...);
    /** @var \Drupal\Core\Field\FieldItemInterface $item */
    foreach ($items as $item) {
      $values = $callback($item, $metadata);
      // If the callback returns NULL, this item should not be exported.
      if (is_array($values)) {
        $data[$name][] = $values;
      }
    }
  }
  return $data;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.