Skip to main content
Home

Main navigation

  • Home
  • Blogs
  • Team
  • Jobs
  • Contact

Right Menu

  • Hire

Update existing BaseField definition size

If you want to update the base field size then you can do by using some hooks. Use hook_entity_base_field_info_alter to increase the size in the form

<?php
use Drupal\Core\Entity\EntityTypeInterface;
/**
 * Implements hook_entity_base_field_info_alter().
 */
function burndown_extend_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  /** @var \Drupal\Core\Field\BaseFieldDefinition $fields[] */
  if ($entity_type->id() === 'burndown_task') {
    $fields['name']->setSetting('max_length', 255);
  }
}

 

Once you implement the above then still you need to update the field storage etc in the database. You can use the hook_update_n to update the storage

<?php

/**
 * Implements hook_update_N().
 *
 * Update the task name field size.
 */
function burndown_extend_update_8001(&$sandbox) {
  burndown_extend_db_change_varchar_field('burndown_task', 'name', 255);
}


/**
 * Change length of a varchar entity field with data, safe with entity-updates.
 *
 * This updates the storage schema, the database schema, and the last
 * installed schema.
 *
 * The entity schema must also be changed in code in the entities
 * baseFieldDefinitions() or in an alter.
 *
 * @param string $entity_type_id
 *   The entity type.
 * @param string $field_name
 *   The field name to change.
 * @param int $field_length
 *   The new length of the field, must be larger than the previous value.
 */
function burndown_extend_db_change_varchar_field($entity_type_id, $field_name, $field_length) {
  /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $schema_repository */
  $schema_repository = \Drupal::service('entity.last_installed_schema.repository');
  /** @var \Drupal\Core\Entity\EntityFieldManager $entity_field_manager */
  $entity_field_manager = \Drupal::service('entity_field.manager');
  $base_field_definitions = $entity_field_manager->getBaseFieldDefinitions($entity_type_id);
  $schema_repository->setLastInstalledFieldStorageDefinition($base_field_definitions[$field_name]);
  $field_storage_definitions = $schema_repository->getLastInstalledFieldStorageDefinitions($entity_type_id);

  // Update the field definition in the last installed schema repository.
  $schema_repository->setLastInstalledFieldStorageDefinitions($entity_type_id, $field_storage_definitions);

  // Update the storage schema.
  $key_value = \Drupal::keyValue('entity.storage_schema.sql');
  $key_name = $entity_type_id . '.field_schema_data.' . $field_name;
  $storage_schema = $key_value->get($key_name);
  // Update all tables where the field is present.
  foreach ($storage_schema as &$table_schema) {
    $table_schema['fields'][$field_name]['length'] = $field_length;
  }
  $key_value->set($key_name, $storage_schema);

  // Update the database tables where the field is part of.
  $db = Drupal::database();
  foreach ($storage_schema as $table_name => $table_schema) {
    $db->schema()->changeField($table_name, $field_name, $field_name, $table_schema['fields'][$field_name]);
  }
}

 

About us

We are a web development company. We have vast excperience in building web sites in Drupal CMS. We develop simple website to complex system to meet the client requirements.

 

Follow us

  facebook twitter instagram linkedin