Last active
September 7, 2020 12:06
-
-
Save heitoralthmann/5c5949c5e5b9bf2a41b4f55aac8bafb7 to your computer and use it in GitHub Desktop.
How to delete ghost / broken / missing fields from Drupal - Base table or view not found
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Deletes ghost/broken/missing fields from the file system. | |
* | |
* I've recently run into a problem where some deleted fields were not | |
* completely wiped out from the system. | |
* This was causing the following error every time I tried to delete a | |
* piece of content: | |
* | |
* Drupal\Core\Entity\EntityStorageException: SQLSTATE[42S02]: Base table | |
* or view not found: 1146 Table 'field_my_field' doesn't exist: DELETE FROM | |
* {field_my_field}... | |
* | |
* Or even this one: | |
* | |
* Drupal\Core\Field\FieldException: Attempt to create a field 'field_my_field' | |
* that does not exist on entity type node. | |
* | |
* To my luck I have later found that these deleted fields were still present | |
* in the following key_value collections: | |
* | |
* - entity.definitions.bundle_field_map > node | |
* - entity.definitions.installed > node.field_storage_definitions | |
* | |
* So removing these fields from the key_value collections did the trick | |
* for me and put my site back to work. | |
* | |
* UPDATE on Sept 7 2020: | |
* | |
* I've found this snippet to be a helpful resource on getting rid of hard errors | |
* when excluding fields through the UI. Although it would help me bypass the hard | |
* errors, next time I would try to delete a field I would again see that WSOD. | |
* | |
* What really fixed the problem for me was this: | |
* | |
* - First, recreate the fields Drupal is complaining about on the same entity type | |
* where they were first created on. | |
* - Then, follow the instructions on this Stack Exchange answer: | |
* https://drupal.stackexchange.com/a/223114/18445 | |
* - After that, run cron. | |
* | |
* That should do the trick. | |
* | |
* @param string $field_name | |
* The machine name of the "ghost" field that is causing you trouble. | |
*/ | |
function deleteGhostField($field_name) { | |
$bundle_field_map = \Drupal::keyValue('entity.definitions.bundle_field_map'); | |
$installed = \Drupal::keyValue('entity.definitions.installed'); | |
$bundle_fields = $bundle_field_map->get('node'); | |
$installed_fields = $installed->get('node.field_storage_definitions'); | |
unset($bundle_fields[$field_name]); | |
unset($installed_fields[$field_name]); | |
$bundle_field_map->set('node', $bundle_fields); | |
$installed->set('node.field_storage_definitions', $installed_fields); | |
} | |
deleteGhostField('field_ordinary_dividends'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment