getEntityTypeIdByName example

/** * Ensures bundles that will be deleted are not in use. * * @param \Drupal\Core\Config\ConfigImporterEvent $event * The config import event. */
  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();
          

  public function validate() {
    if (!$this->validated) {
      $this->errors = [];
      // Create the list of installs and uninstalls.       $this->createExtensionChangelist();
      // Validate renames.       foreach ($this->getUnprocessedConfiguration('rename') as $name) {
        $names = $this->storageComparer->extractRenameNames($name);
        $old_entity_type_id = $this->configManager->getEntityTypeIdByName($names['old_name']);
        $new_entity_type_id = $this->configManager->getEntityTypeIdByName($names['new_name']);
        if ($old_entity_type_id != $new_entity_type_id) {
          $this->logError($this->t('Entity type mismatch on rename. @old_type not equal to @new_type for existing configuration @old_name and staged configuration @new_name.', ['@old_type' => $old_entity_type_id, '@new_type' => $new_entity_type_id, '@old_name' => $names['old_name'], '@new_name' => $names['new_name']]));
        }
        // Has to be a configuration entity.         if (!$old_entity_type_id) {
          $this->logError($this->t('Rename operation for simple configuration. Existing configuration @old_name and staged configuration @new_name.', ['@old_name' => $names['old_name'], '@new_name' => $names['new_name']]));
        }
      }
      $this->eventDispatcher->dispatch(new ConfigImporterEvent($this), ConfigEvents::IMPORT_VALIDATE);
      if (count($this->getErrors())) {
        
return $entity_type_id;
      }
    }

    return NULL;
  }

  /** * {@inheritdoc} */
  public function loadConfigEntityByName($name) {
    $entity_type_id = $this->getEntityTypeIdByName($name);
    if ($entity_type_id) {
      $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
      $id = substr($namestrlen($entity_type->getConfigPrefix()) + 1);
      return $this->entityTypeManager->getStorage($entity_type_id)->load($id);
    }
    return NULL;
  }

  /** * {@inheritdoc} */
  
// Gather information about all the supported collections.       $collection_info = $this->configManager->getConfigCollectionInfo();
      foreach ($collection_info->getCollectionNames() as $collection) {
        $config_to_create = $this->getConfigToCreate($storage$collection$prefix$profile_storages);
        if ($name == $this->drupalGetProfile()) {
          // If we're installing a profile ensure simple configuration that           // already exists is excluded as it will have already been written.           // This means that if the configuration is changed by something else           // during the install it will not be overwritten again.           $existing_configuration = array_filter($this->getActiveStorages($collection)->listAll()function D$config_name) {
            return !$this->configManager->getEntityTypeIdByName($config_name);
          });
          $config_to_create = array_diff_key($config_to_createarray_flip($existing_configuration));
        }
        if (!empty($config_to_create)) {
          $this->createConfiguration($collection$config_to_create);
        }
      }
    }

    // During a drupal installation optional configuration is installed at the     // end of the installation process. Once the install profile is installed
/** * Get the workflow entity object from the configuration name. * * @param string $config_name * The configuration object name. * * @return \Drupal\workflows\WorkflowInterface|null * A workflow entity object. NULL if no matching entity is found. */
  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);
  }

}
// Just connect directly to the config table so we don't need to worry about     // the cache layer.     $active_config_storage = $this->container->get('config.storage');

    /** @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();
        }
return $value;
  }

  /** * Updates configuration entity in the key store. * * @param \Drupal\Core\Config\ConfigCrudEvent $event * The configuration event. */
  public function onConfigSave(ConfigCrudEvent $event) {
    $saved_config = $event->getConfig();
    $entity_type_id = $this->configManager->getEntityTypeIdByName($saved_config->getName());
    if ($entity_type_id) {
      $entity_type = $this->configManager->getEntityTypeManager()->getDefinition($entity_type_id);
      $this->updateConfigKeyStore($entity_type$saved_config);
    }
  }

  /** * Removes configuration entity from key store. * * @param \Drupal\Core\Config\ConfigCrudEvent $event * The configuration event. */

  public function getDefaultConfigLangcode($name) {
    // Config entities that do not have the 'default_config_hash' cannot be     // shipped configuration regardless of whether there is a name match.     // configurable_language entities are a special case since they can be     // translated regardless of whether they are shipped if they in the standard     // language list.     $config_entity_type = $this->configManager->getEntityTypeIdByName($name);
    if (!$config_entity_type || $config_entity_type === 'configurable_language'
      || !empty($this->configFactory->get($name)->get('_core.default_config_hash'))
    ) {
      $shipped = $this->defaultConfigStorage->read($name);
      if (!empty($shipped)) {
        return !empty($shipped['langcode']) ? $shipped['langcode'] : 'en';
      }
    }
    return NULL;
  }

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