Skip to content

Instantly share code, notes, and snippets.

@junaidpv
Created December 28, 2021 13:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junaidpv/3a5211deec20ae770e6d93a6a9d2c8bd to your computer and use it in GitHub Desktop.
Save junaidpv/3a5211deec20ae770e6d93a6a9d2c8bd to your computer and use it in GitHub Desktop.
Drush command to fix "non-existent config entity name returned by FieldStorageConfigInterface::getBundles()" errors reported at https://www.drupal.org/project/drupal/issues/2916266
services:
update.commands:
class: \Drupal\my_module\Commands\UpdateCommands
arguments:
- '@keyvalue'
tags:
- { name: drush.command }
<?php
namespace Drupal\my_module\Commands;
use Drush\Commands\DrushCommands;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
/**
* A Drush commandfile.
*
* In addition to this file, you need a drush.services.yml
* in root of your module, and a composer.json file that provides the name
* of the services file to use.
*
* See these files for an example of injecting Drupal services:
* - http://git.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
* - http://git.drupalcode.org/devel/tree/drush.services.yml
*/
class UpdateCommands extends DrushCommands {
/**
* The key value store to use.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
*/
protected $keyValueStore;
/**
* @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
* The key value store to use.
*/
public function __construct(KeyValueFactoryInterface $key_value_factory) {
$this->keyValueStore = $key_value_factory;
}
/**
* Corrects a field storage configuration. See https://www.drupal.org/project/drupal/issues/2916266 for more info
*
* @command update:correct-field-config-storage
*
* @param string $entity_type
* Entity type
* @param string $bundle
* Bundle name
* @param string $field_name
* Field name
*/
public function correctFieldStorageConfig($entity_type, $bundle, $field_name) {
$field_map_kv_store = $this->keyValueStore->get('entity.definitions.bundle_field_map');
$map = $field_map_kv_store->get($entity_type);
unset($map[$field_name]['bundles'][$bundle]);
$field_map_kv_store->set($entity_type, $map);
}
}
@rtvenge
Copy link

rtvenge commented Nov 15, 2022

For those who aren't super familiar with drush commands in a module, the UpdateCommands.php file needs to be in the src/Commands relative to your custom module.

CleanShot 2022-11-15 at 09 02 50@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment