Drupal is a registered trademark of Dries Buytaert
Protected Pages 3.0.0 Major update available for module protected_pages (3.0.0). Commerce Core 3.3.8 Minor update available for module commerce (3.3.8). Layout Builder Reorder 2.0.1 Minor update available for module layout_builder_reorder (2.0.1). Ban 1.1.0 Minor update available for module ban (1.1.0). Field Formatter Range 2.0.0 Major update available for module field_formatter_range (2.0.0). Field Formatter Range 8.x-1.8 Minor update available for module field_formatter_range (8.x-1.8). UI Patterns (SDC in Drupal UI) 2.0.18 Minor update available for module ui_patterns (2.0.18). Varbase FAQs 9.2.1 Minor update available for module varbase_faqs (9.2.1). Search API Solr 4.3.11 Module search_api_solr updated after 14 months of inactivity (4.3.11). Provus Mega Menu Module provus_mega_menu crossed 1,000 active installs.

This module provides a safe way to update entity type definitions in your database using update hooks, serving as an alternative to Drupal core's Entity Type Definition Update Manager. It allows for easy synchronization of entity definitions from code to the database, and also supports complex updates that involve preserving existing data through a multi-step process of backing up, truncating, updating, restoring, and cleaning up database tables.

Provides the ability to update entity type definitions in your database in a safe way via regular update hooks. This module is an alternative to the Entity Type Definition Update Manager from Drupal core.

Let's keep entity definition updates fairly easy

If you don't have any existing fields in your entity type, which you like to update, but you have just new fields which you want to install:

<?php
declare(strict_types=1);

/**
 * Implements hook_install().
 */
function my_easy_module_install($is_syncing) {
  /**
   * @var EntityDefinitionUpdateManager $entityDefinitionUpdate
   */
  $entityDefinitionUpdate = \Drupal::service('entity_definition_update.entity_definition_update_manager');
  $entityDefinitionUpdate->applyUpdates();
}

Et voilĂ ! Your entity definitions from code are in sync with the ones in your database.

Complex example usage within a example_module.install file

This approach can be followed, if you have already data for existing fields, which you want to update. Then you must migrate data for preserving it. The entity definition update process is split into five steps:

  1. Backing up the date into a temporary database table
  2. Truncating the old database table
  3. Updating the entity definition
  4. Restoring the data into the database table, with the updated entity definition
  5. Deleting the temporary created database table with the old structure, for backing up the data
<?php declare(strict_types=1);

use Drupal\Core\Database\Database;
use Drupal\entity_definition_update\EntityDefinitionUpdateManager;


/**
 * Update entity type definitions in your database, to reflect the definitions in the entity type code.
 */
function example_module_update_9001(): void {
  $database = \Drupal::database();
  $tables = [
    'announcement' => 'announcement__temp',
  ];

  // Create a full copy of source tables.
  foreach ($tables as $originalTableName => $tempTableName) {
    $database->query("CREATE TABLE $tempTableName LIKE $originalTableName");
    $database->query("INSERT INTO $tempTableName SELECT * FROM $originalTableName");
    $database->truncate($originalTableName)->execute();
  }

  /**
   * @var EntityDefinitionUpdateManager $entityDefinitionUpdate
   */
  $entityDefinitionUpdate = \Drupal::service('entity_definition_update.entity_definition_update_manager');
  $entityDefinitionUpdate->applyUpdates();

  $schema = Database::getConnection()->schema();
  $schema->changeField('announcement', 'contracted_company_other_information__value', 'contracted_company_other_information__value', [
    'not null' => FALSE,
    'type' => 'text',
  ]);

  \Drupal::logger('entity_definition_update')->notice('Updated field storage config.');

  foreach ($tables as $originalTableName => $tempTableName) {
    // Method 1: copy back everything into a table with "changed" database columns
    $database->query("INSERT INTO $originalTableName (
      id,
      langcode,
      uid,
      contracted_company_other_information__value
    )
        SELECT
          id,
          langcode,
          uid,
          contracted_company_other_information
        FROM $tempTableName");

    if ($originalTableName === 'table_with_same_structure')
        // Method 2: copy back everything into a table with "same" database columns
       $database->query("INSERT INTO $originalTableName SELECT * FROM $tempTableName");
    }
    $database->query("DROP TABLE $tempTableName");
    \Drupal::logger('entity_definition_update')->notice("Dropped table $tempTableName");
  }
}

Using the Entity Definition Update Manager from Drupal core

For applying field updates for an existing entity type definition, the Entity Definition Update Manager from Drupal core can be also an option. See an example below:

function your_module_update_8001() {
  // First we need to get update manager.
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();

  // Load the storage definition of the field.
  $field_storage = $definition_update_manager->getFieldStorageDefinition('field_name',
    'node');
  // Set a new list of options for the list field.
  $field_storage->setSettings([
    'allowed_values' => [
      'link' => 'Link',
      'posts' => 'Posts',
      'events' => 'Events',
      'page' => 'Page',
    ],
  ]);

  // Update the field storage definition.
  $definition_update_manager->updateFieldStorageDefinition($field_storage);
}

Activity

Total releases
1
First release
Mar 2025
Latest release
1 year ago
Releases (12 mo)
0 ▼ from 1
Maintenance
Dormant

Releases

Version Type Release date
1.2.0 Stable Mar 17, 2025