initWithData example

public function __construct($name, StorageInterface $storage, EventDispatcherInterface $event_dispatcher, TypedConfigManagerInterface $typed_config) {
    $this->name = $name;
    $this->storage = $storage;
    $this->eventDispatcher = $event_dispatcher;
    $this->typedConfigManager = $typed_config;
  }

  /** * {@inheritdoc} */
  public function initWithData(array $data) {
    parent::initWithData($data);
    $this->resetOverriddenData();
    return $this;
  }

  /** * {@inheritdoc} */
  public function get($key = '') {
    if (!isset($this->overriddenData)) {
      $this->setOverriddenData();
    }
    
$storage_data = $this->storage->readMultiple($names);

      if ($immutable && !empty($storage_data)) {
        // Only get module overrides if we have configuration to override.         $module_overrides = $this->loadOverrides($names);
      }

      foreach ($storage_data as $name => $data) {
        $cache_key = $this->getConfigCacheKey($name$immutable);

        $this->cache[$cache_key] = $this->createConfigObject($name$immutable);
        $this->cache[$cache_key]->initWithData($data);
        if ($immutable) {
          if (isset($module_overrides[$name])) {
            $this->cache[$cache_key]->setModuleOverride($module_overrides[$name]);
          }
          if (isset($GLOBALS['config'][$name])) {
            $this->cache[$cache_key]->setSettingsOverride($GLOBALS['config'][$name]);
          }
        }

        $this->propagateConfigOverrideCacheability($cache_key$name);

        
$storage = $this->getStorage($langcode);
    $data = $storage->read($name);

    $override = new LanguageConfigOverride(
      $name,
      $storage,
      $this->typedConfigManager,
      $this->eventDispatcher
    );

    if (!empty($data)) {
      $override->initWithData($data);
    }
    return $override;
  }

  /** * {@inheritdoc} */
  public function getStorage($langcode) {
    if (!isset($this->storages[$langcode])) {
      $this->storages[$langcode] = $this->baseStorage->createCollection($this->createConfigCollectionName($langcode));
    }
    

  protected function importInvokeOwner($collection$op$name) {
    // Renames are handled separately.     if ($op == 'rename') {
      return $this->importInvokeRename($collection$name);
    }
    // Validate the configuration object name before importing it.     // Config::validateName($name);     if ($entity_type = $this->configManager->getEntityTypeIdByName($name)) {
      $old_config = new Config($name$this->storageComparer->getTargetStorage($collection)$this->eventDispatcher, $this->typedConfigManager);
      if ($old_data = $this->storageComparer->getTargetStorage($collection)->read($name)) {
        $old_config->initWithData($old_data);
      }

      $data = $this->storageComparer->getSourceStorage($collection)->read($name);
      $new_config = new Config($name$this->storageComparer->getTargetStorage($collection)$this->eventDispatcher, $this->typedConfigManager);
      if ($data !== FALSE) {
        $new_config->setData($data);
      }

      $method = 'import' . ucfirst($op);
      $entity_storage = $this->configManager->getEntityTypeManager()->getStorage($entity_type);
      // Call to the configuration entity's storage to handle the configuration
// Attempt to treat the single value as a nested item.     $this->expectError();
    $this->config->set('testData.illegalOffset', 1);
  }

  /** * @covers ::initWithData * @dataProvider nestedDataProvider */
  public function testInitWithData($data) {
    $config = $this->config->initWithData($data);

    // Should return the Config object.     $this->assertInstanceOf('\Drupal\Core\Config\Config', $config);

    // Check config is not new.     $this->assertEquals(FALSE, $this->config->isNew());

    // Check that data value was set correctly.     $this->assertConfigDataEquals($data);

    // Check that original data was set.
$this->assertTrue($this->configTranslation->isNew());
    $this->configTranslation->save();
  }

  /** * @covers ::save */
  public function testSaveExisting() {
    $this->cacheTagsInvalidator->expects($this->once())
      ->method('invalidateTags')
      ->with(['config:config.test']);
    $this->configTranslation->initWithData([]);
    $this->configTranslation->save();
  }

  /** * @covers ::delete */
  public function testDelete() {
    $this->cacheTagsInvalidator->expects($this->once())
      ->method('invalidateTags')
      ->with(['config:config.test']);
    $this->configTranslation->initWithData([]);
    
Home | Imprint | This part of the site doesn't use cookies.