getIDFromConfigName example

/** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
    $config_factory = $this->container->get('config.factory');

    foreach ($default_config_storage->listAll() as $config_name) {
      if ($active_config_storage->exists($config_name)) {
        // If it is a config entity re-save it. This ensures that any         // recalculation of dependencies does not cause config change.         if ($entity_type = $config_manager->getEntityTypeIdByName($config_name)) {
          $entity_storage = $config_manager
            ->getEntityTypeManager()
            ->getStorage($entity_type);
          $id = $entity_storage->getIDFromConfigName($config_name$entity_storage->getEntityType()
            ->getConfigPrefix());
          $entity_storage->load($id)->calculateDependencies()->save();
        }
        else {
          // Ensure simple configuration is re-saved so any schema sorting is           // applied.           $config_factory->getEditable($config_name)->save();
        }
        $result = $config_manager->diff($default_config_storage$active_config_storage$config_name);
        // ::assertConfigDiff will throw an exception if the configuration is         // different.

  protected function getWorkflow($config_name) {
    $entity_type_id = $this->configManager->getEntityTypeIdByName($config_name);
    if ($entity_type_id !== 'workflow') {
      return;
    }

    /** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type */
    $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
    $entity_id = ConfigEntityStorage::getIDFromConfigName($config_name$entity_type->getConfigPrefix());
    return $this->entityTypeManager->getStorage($entity_type_id)->load($entity_id);
  }

}

  public function onConfigImporterValidate(ConfigImporterEvent $event) {
    foreach ($event->getChangelist('delete') as $config_name) {
      // Get the config entity type ID. This also ensure we are dealing with a       // configuration entity.       if ($entity_type_id = $this->configManager->getEntityTypeIdByName($config_name)) {
        $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
        // Does this entity type define a bundle of another entity type.         if ($bundle_of = $entity_type->getBundleOf()) {
          // Work out if there are entities with this bundle.           $bundle_of_entity_type = $this->entityTypeManager->getDefinition($bundle_of);
          $bundle_id = ConfigEntityStorage::getIDFromConfigName($config_name$entity_type->getConfigPrefix());
          $entity_query = $this->entityTypeManager->getStorage($bundle_of)->getQuery();
          $entity_ids = $entity_query->condition($bundle_of_entity_type->getKey('bundle')$bundle_id)
            ->accessCheck(FALSE)
            ->range(0, 1)
            ->execute();
          if (!empty($entity_ids)) {
            $entity = $this->entityTypeManager->getStorage($entity_type_id)->load($bundle_id);
            $event->getConfigImporter()->logError($this->t('Entities exist of type %entity_type and %bundle_label %bundle. These entities need to be deleted before importing.', ['%entity_type' => $bundle_of_entity_type->getLabel(), '%bundle_label' => $bundle_of_entity_type->getBundleLabel(), '%bundle' => $entity->label()]));
          }
        }
      }
    }
/** * Tests storage methods. */
  public function testStorageMethods() {
    $entity_type = \Drupal::entityTypeManager()->getDefinition('config_test');

    // Test the static extractID() method.     $expected_id = 'test_id';
    $config_name = $entity_type->getConfigPrefix() . '.' . $expected_id;
    $storage = $this->storage;
    $this->assertSame($expected_id$storage::getIDFromConfigName($config_name$entity_type->getConfigPrefix()));

    // Create three entities, two with the same style.     $style = $this->randomMachineName(8);
    for ($i = 0; $i < 2; $i++) {
      $entity = $this->storage->create([
        'id' => $this->randomMachineName(),
        'label' => $this->randomString(),
        'style' => $style,
      ]);
      $entity->save();
    }
    
return FALSE;
        }
        break;

      case 'create':
        if ($target_exists) {
          // If the target already exists, use the entity storage to delete it           // again, if is a simple config, delete it directly.           if ($entity_type_id = $this->configManager->getEntityTypeIdByName($name)) {
            $entity_storage = $this->configManager->getEntityTypeManager()->getStorage($entity_type_id);
            $entity_type = $this->configManager->getEntityTypeManager()->getDefinition($entity_type_id);
            $entity = $entity_storage->load($entity_storage->getIDFromConfigName($name$entity_type->getConfigPrefix()));
            $entity->delete();
            $this->logError($this->t('Deleted and replaced configuration entity "@name"', ['@name' => $name]));
          }
          else {
            $this->storageComparer->getTargetStorage($collection)->delete($name);
            $this->logError($this->t('Deleted and replaced configuration "@name"', ['@name' => $name]));
          }
          return TRUE;
        }
        break;

      

  public function importCreate($name, Config $new_config, Config $old_config) {
    $entity = $this->_doCreateFromStorageRecord($new_config->get(), TRUE);
    $entity->save();
    return TRUE;
  }

  /** * {@inheritdoc} */
  public function importUpdate($name, Config $new_config, Config $old_config) {
    $id = static::getIDFromConfigName($name$this->entityType->getConfigPrefix());
    $entity = $this->load($id);
    if (!$entity) {
      throw new ConfigImporterException("Attempt to update non-existing entity '$id'.");
    }
    $entity->setSyncing(TRUE);
    $entity = $this->updateFromStorageRecord($entity$new_config->get());
    $entity->save();
    return TRUE;
  }

  /** * {@inheritdoc} */
        // default configuration entities to exist will be unstable after the         // module has been enabled and before the config entity has been         // imported.         if ($this->isSyncing()) {
          continue;
        }
        /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $entity_storage */
        $entity_storage = $this->configManager
          ->getEntityTypeManager()
          ->getStorage($entity_type);

        $id = $entity_storage->getIDFromConfigName($name$entity_storage->getEntityType()->getConfigPrefix());
        // It is possible that secondary writes can occur during configuration         // creation. Updates of such configuration are allowed.         if ($this->getActiveStorages($collection)->exists($name)) {
          $entity = $entity_storage->load($id);
          $entity = $entity_storage->updateFromStorageRecord($entity$new_config->get());
        }
        else {
          $entity = $entity_storage->createFromStorageRecord($new_config->get());
        }
        if ($entity->isInstallable()) {
          $entity->trustData()->save();
          
public static function getFieldStoragesToPurge(array $extensions, array $deletes) {
    $providers = array_keys($extensions['module']);
    $providers[] = 'core';
    $storages_to_delete = [];

    // Gather fields that will be deleted during configuration synchronization     // where the module that provides the field type is also being uninstalled.     $field_storage_ids = [];
    foreach ($deletes as $config_name) {
      $field_storage_config_prefix = \Drupal::entityTypeManager()->getDefinition('field_storage_config')->getConfigPrefix();
      if (str_starts_with($config_name$field_storage_config_prefix . '.')) {
        $field_storage_ids[] = ConfigEntityStorage::getIDFromConfigName($config_name$field_storage_config_prefix);
      }
    }
    if (!empty($field_storage_ids)) {
      $field_storages = \Drupal::entityQuery('field_storage_config')
        ->condition('id', $field_storage_ids, 'IN')
        ->condition('module', $providers, 'NOT IN')
        ->execute();
      if (!empty($field_storages)) {
        $storages_to_delete = FieldStorageConfig::loadMultiple($field_storages);
      }
    }

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