You are here

protected function ContentEntityStorage::getDataToSave in MongoDB 8

Collect the translations of an entity for saving.

Parameters

ContentEntityInterface $entity:

Return value

array

1 call to ContentEntityStorage::getDataToSave()
ContentEntityStorage::doSave in src/Entity/ContentEntityStorage.php
Performs storage-specific saving of the entity.

File

src/Entity/ContentEntityStorage.php, line 236
Contains Drupal\mongodb\Entity\ContentEntityStorage.

Class

ContentEntityStorage

Namespace

Drupal\mongodb\Entity

Code

protected function getDataToSave(ContentEntityInterface $entity) {
  $default_langcode = $entity
    ->getUntranslated()
    ->language()
    ->getId();
  $values = array();
  $langcodes = [];
  $languages = $entity
    ->getTranslationLanguages();
  foreach ($languages as $langcode => $language) {
    $translation = $entity
      ->getTranslation($langcode);
    $translated_values = [];
    $langcodes[] = $langcode;

    /** @var \Drupal\Core\Field\FieldItemListInterface $items */
    foreach ($translation as $field_name => $items) {
      $field_storage_definition = $items
        ->getFieldDefinition()
        ->getFieldStorageDefinition();
      $columns = $field_storage_definition
        ->getSchema()['columns'];
      $cardinality = $field_storage_definition
        ->getCardinality();

      /** @var \Drupal\Core\Field\FieldItemListInterface $items */
      if (!$items
        ->isEmpty()) {
        foreach ($items as $delta => $item) {
          if ($delta == $cardinality) {
            break;
          }

          /** @var \Drupal\Core\Field\FieldItemInterface $item */
          foreach ($item
            ->toArray() as $column => $value) {
            if (isset($columns[$column])) {
              $translated_values[$field_name][$delta][$column] = MongoCollectionFactory::castValue($columns[$column]['type'], $value);
            }
          }
        }
      }
    }
    if ($default_langcode == $langcode) {
      array_unshift($values, $this
        ->denormalize($translated_values));
    }
    else {
      $values[] = $this
        ->denormalize($translated_values);
    }
  }
  $data = array(
    'bundle' => $entity
      ->bundle(),
    'translations' => $langcodes,
    'values' => $values,
  );
  $this
    ->entityToData($entity, $data);
  return $data;
}