trait RawConfigWriterTrait

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Traits/Core/Config/RawConfigWriterTrait.php \Drupal\Tests\Traits\Core\Config\RawConfigWriterTrait

Allows writing raw config data for testing purposes.

The writes do not use schema to normalize the data.

Hierarchy

1 file declares its use of RawConfigWriterTrait
ItemsPerPageTest.php in core/modules/views/tests/src/Functional/Wizard/ItemsPerPageTest.php

File

core/tests/Drupal/Tests/Traits/Core/Config/RawConfigWriterTrait.php, line 15

Namespace

Drupal\Tests\Traits\Core\Config
View source
trait RawConfigWriterTrait {
  
  /**
   * Writes raw config data.
   *
   * @param string $name
   *   The config name.
   * @param array $data
   *   The config data to write.
   */
  protected function writeRawConfig(string $name, array $data) : void {
    $storage = \Drupal::service('config.storage');
    $storage->write($name, $data);
    \Drupal::service('cache_tags.invalidator')->invalidateTags([
      'config:' . $name,
    ]);
    \Drupal::configFactory()->clearStaticCache();
  }
  
  /**
   * Writes a config entity without using the entity system or schema.
   *
   * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
   *   The config entity to write.
   */
  protected function writeRawConfigEntity(ConfigEntityInterface $entity) : void {
    $this->writeRawConfig($entity->getConfigDependencyName(), $entity->toArray());
    $tags = Cache::mergeTags($entity->getCacheTags(), $entity->getEntityType()
      ->getListCacheTags(), $entity->getEntityType()
      ->hasKey('bundle') ? $entity->getEntityType()
      ->getBundleListCacheTags($entity->bundle()) : []);
    if (!empty($tags)) {
      \Drupal::service('cache_tags.invalidator')->invalidateTags($tags);
    }
    \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())
      ->resetCache([
      $entity->id(),
    ]);
  }

}

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