mapToStorageRecord example

/** * {@inheritdoc} */
  public function restore(EntityInterface $entity) {
    try {
      $transaction = $this->database->startTransaction();
      // Insert the entity data in the base and data tables only for default       // revisions.       /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      if ($entity->isDefaultRevision()) {
        $record = $this->mapToStorageRecord($entity->getUntranslated()$this->baseTable);
        $this->database
          ->insert($this->baseTable)
          ->fields((array) $record)
          ->execute();

        if ($this->dataTable) {
          $this->saveToSharedTables($entity);
        }
      }

      // Insert the entity data in the revision and revision data tables.
$config_name = $prefix . $entity->id();
    if ($id !== $entity->id()) {
      // Renaming a config object needs to cater for:       // - Storage needs to access the original object.       // - The object needs to be renamed/copied in ConfigFactory and reloaded.       // - All instances of the object need to be renamed.       $this->configFactory->rename($prefix . $id$config_name);
    }
    $config = $this->configFactory->getEditable($config_name);

    // Retrieve the desired properties and set them in config.     $config->setData($this->mapToStorageRecord($entity));
    $config->save($entity->hasTrustedData());

    // Update the entity with the values stored in configuration. It is possible     // that configuration schema has casted some of the values.     if (!$entity->hasTrustedData()) {
      $data = $this->mapFromStorageRecords([$config->get()]);
      $updated_entity = current($data);

      foreach (array_keys($config->get()) as $property) {
        $value = $updated_entity->get($property);
        $entity->set($property$value);
      }

class LayoutBuilderEntityViewDisplayStorage extends ConfigEntityStorage {

  /** * {@inheritdoc} */
  protected function mapToStorageRecord(EntityInterface $entity) {
    $record = parent::mapToStorageRecord($entity);

    if (!empty($record['third_party_settings']['layout_builder']['sections'])) {
      $record['third_party_settings']['layout_builder']['sections'] = array_map(function DSection $section) {
        return $section->toArray();
      }$record['third_party_settings']['layout_builder']['sections']);
    }
    return $record;
  }

  /** * {@inheritdoc} */
throw new PluginNotFoundException($record['type'], "Unable to determine class for field type '{$record['type']}' found in the '$config_id' configuration", $e->getCode()$e);
      }
      $record['settings'] = $class::storageSettingsFromConfigData($record['settings']);
    }
    return parent::mapFromStorageRecords($records);
  }

  /** * {@inheritdoc} */
  protected function mapToStorageRecord(EntityInterface $entity) {
    $record = parent::mapToStorageRecord($entity);
    $class = $this->fieldTypeManager->getPluginClass($record['type']);
    $record['settings'] = $class::storageSettingsToConfigData($record['settings']);
    return $record;
  }

}
throw new PluginNotFoundException($record['field_type'], "Unable to determine class for field type '{$record['field_type']}' found in the '$config_id' configuration", $e->getCode()$e);
      }
      $record['settings'] = $class::fieldSettingsFromConfigData($record['settings']);
    }
    return parent::mapFromStorageRecords($records);
  }

  /** * {@inheritdoc} */
  protected function mapToStorageRecord(EntityInterface $entity) {
    $record = parent::mapToStorageRecord($entity);
    $class = $this->fieldTypeManager->getPluginClass($record['field_type']);
    $record['settings'] = $class::fieldSettingsToConfigData($record['settings']);
    return $record;
  }

}
Home | Imprint | This part of the site doesn't use cookies.