1

So I had to update a size of a field and I used an update hook. However, in my update hook I deleted the field and the index and added the field again with the updated size. Now when I go to the configuration page on the drupal admin, I get the following error:

Error: Call to a member function getLabel() on null in Drupal\Core\Entity\EntityDefinitionUpdateManager->getChangeSummary() (line 79 of core/lib/Drupal/Core/Entity/EntityDefinitionUpdateManager.php

Here is my update script:

<?php

function featured_banner_field_update_8002() {
  $table  = 'field_banner_section'; // table name without 'node__'
  $column = 'content_body';
  $spec = [
    'type' => 'text',
  ];
  $schema = \Drupal\Core\Database\Database::getConnection()->schema();
  $schema->dropField('node__' . $table, $table . '_' . $column);
  $schema->dropIndex('node__' . $table, $table . '_' . $column);

  $schema->addField('node__' . $table, $table . '_' . $column, $spec);
  $schema->addIndex('node__' . $table, $table . '_' . $column, [$table . '_' . $column], [
    'fields' => [$table . '_' . $column => $spec]
  ]);

  // The revision table.
  $schema->dropField('node_revision__' . $table, $table . '_' . $column);
  $schema->dropIndex('node_revision__' . $table, $table . '_' . $column);

  $schema->addField('node_revision__' . $table, $table . '_' . $column, $spec);
  $schema->addIndex('node_revision__' . $table, $table . '_' . $column, [$table . '_' . $column], [
    'fields' => [$table . '_' . $column => $spec]
  ]);

  // Current node field configurations.
  $field_manager = \Drupal::getContainer()->get('entity_field.manager');
  // Because the manager was already loaded before the above config was forced,
  // it will return the old configuration that was cached.
  $field_manager->clearCachedFieldDefinitions();
  $field_storage_configs = $field_manager->getFieldStorageDefinitions('node');

  // Get the last installed manager, this is the gatekeeper that determines if
  // an update is needed or can be done.
  $last_installed_repo = \Drupal::getContainer()->get('entity.last_installed_schema.repository');

  // Get the last installed configurations for node fields
  // These are iterative objects and need to stored as such, not just simple arrays,
  // so reusing the previously set configs is not an option.
  $last_installed_configs = $last_installed_repo->getLastInstalledFieldStorageDefinitions('node');

  // Force the last installed config to be the current for the field.
  $last_installed_configs['field_banner_section_content_body'] = $field_storage_configs['field_banner_section_content_body'];
  $last_installed_repo->setLastInstalledFieldStorageDefinitions('node', $last_installed_configs);
}
2
  • 1
    Presumably you killed some required data while manually dropping the fields, or the data you're manually adding to config is incorrect. Clear the cache, if that doesn't fix it, restore to a backup and examine your update hook code carefully before running it again – Clive Apr 12 '18 at 14:54
  • It is too late to revert to the old database (too many important changes have been made to the db). Is there a way I can delete the field's definition from the database? – Patel. D Apr 12 '18 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.