Commit 3eae6fa5 authored by Samuel Mortenson's avatar Samuel Mortenson Committed by Sam Mortenson
Browse files

Issue #2980848 by samuel.mortenson, larowlan, bserem: Add YAML as an optional entity storage format

parent b2f2a681
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ in settings.php are:
 - tome_static_cache_exclude: An array of paths to always exclude from cache.
 - tome_static_path_exclude: An array of paths to exclude from static site
   generation. Useful for system paths.
 - tome_sync_encoder: The encoder to use when exporting content. Defaults to
   "json" but "yaml" is also available in an experimental state.

Config is exported to your config sync directory. It's recommended that you set
this to "../config" with a settings.php line like:
+35 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\tome_sync\Encoder;

use Symfony\Component\Serializer\Encoder\YamlEncoder as BaseYamlEncoder;

/**
 * Adds YAML support for serialization.
 *
 * @internal
 */
class YamlEncoder extends BaseYamlEncoder {

  /**
   * The formats that this Encoder supports.
   *
   * @var array
   */
  protected static $format = ['tome_sync_yaml'];

  /**
   * {@inheritdoc}
   */
  public function supportsEncoding($format) {
    return in_array($format, static::$format);
  }

  /**
   * {@inheritdoc}
   */
  public function supportsDecoding($format) {
    return in_array($format, static::$format);
  }

}
+10 −1
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ use Drupal\tome_sync\Event\TomeSyncEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Serializer\Serializer;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Site\Settings;

/**
 * Handles exporting of content and file entities.
@@ -76,6 +77,13 @@ class Exporter implements ExporterInterface {
    'content_moderation_state',
  ];

  /**
   * The encoder format (i.e. "json", "yaml").
   *
   * string
   */
  protected $encoderFormat;

  /**
   * Creates an Exporter object.
   *
@@ -102,6 +110,7 @@ class Exporter implements ExporterInterface {
    $this->accountSwitcher = $account_switcher;
    $this->fileSync = $file_sync;
    $this->fileSystem = $file_system;
    $this->encoderFormat = Settings::get('tome_sync_encoder', 'json');
  }

  /**
@@ -139,7 +148,7 @@ class Exporter implements ExporterInterface {
      return;
    }
    $this->switchToAdmin();
    $data = $this->serializer->normalize($entity, 'json');
    $data = $this->serializer->normalize($entity, $this->encoderFormat);
    $this->contentStorage->write(TomeSyncHelper::getContentName($entity), $data);
    $this->indexContent($entity);
    if ($entity instanceof FileInterface) {
+10 −1
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ use Drupal\tome_sync\Event\TomeSyncEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Serializer\Serializer;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Site\Settings;

/**
 * Handles importing of content and file entities.
@@ -74,6 +75,13 @@ class Importer implements ImporterInterface {
   */
  protected $fileSync;

  /**
   * The encoder format (i.e. "json", "yaml").
   *
   * string
   */
  protected $encoderFormat;

  /**
   * Creates an Importer object.
   *
@@ -101,6 +109,7 @@ class Importer implements ImporterInterface {
    $this->accountSwitcher = $account_switcher;
    $this->fileSync = $file_sync;
    $this->fileSystem = $file_system;
    $this->encoderFormat = Settings::get('tome_sync_encoder', 'json');
  }

  /**
@@ -243,7 +252,7 @@ class Importer implements ImporterInterface {
  protected function loadEntityFromStorage(EntityTypeInterface $entity_type, $uuid, $langcode = NULL) {
    $contents = $this->contentStorage->read(TomeSyncHelper::getContentNameFromParts($entity_type->id(), $uuid, $langcode));
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $this->serializer->denormalize($contents, $entity_type->getClass(), 'json');
    $entity = $this->serializer->denormalize($contents, $entity_type->getClass(), $this->encoderFormat);
    $entity->_tomeFields = array_keys($contents);
    return $entity;
  }
+14 −0
Original line number Diff line number Diff line
@@ -4,9 +4,13 @@ namespace Drupal\tome_sync;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderInterface;
use Drupal\Core\Site\Settings;
use Drupal\tome_sync\Encoder\YamlEncoder;
use Drupal\tome_sync\EventSubscriber\BookEventSubscriber;
use Drupal\tome_sync\EventSubscriber\LanguageConfigEventSubscriber;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Yaml\Dumper;

/**
 * Registers services in the container.
@@ -32,6 +36,16 @@ class TomeSyncServiceProvider implements ServiceProviderInterface {
        ->addArgument(new Reference('entity_type.manager'))
        ->addArgument(new Reference('file_system'));
    }
    if (Settings::get('tome_sync_encoder', 'json') === 'yaml') {
        $container->register('tome_sync.storage.content.yaml', YamlFileStorage::class)
          ->setDecoratedService('tome_sync.storage.content')
          ->setFactory([YamlFileStorageFactory::class, 'getContent']);
        $container->register('tome_sync.yaml_dumper', Dumper::class)
          ->addArgument(2);
        $container->register('tome_sync.encoder.yaml', YamlEncoder::class)
          ->addArgument(new Reference('tome_sync.yaml_dumper'))
          ->addTag('encoder', ['format' => 'tome_sync_yaml']);
    }
  }

}
Loading