Change Drupal 8 text field maximum length

Submitted by editor on Mon, 08/22/2016 - 16:20
Question

How to Modify Drupal 8/ Drupal 7 text field maximum length ?

If there no data, You can use field_update_field (tested on D7) Like:

$field = array(
'field_name' => 'field_name',
'type' => 'text_long',
'cardinality' => 1,
'settings' => array('max_length' => 0),
);
field_update_field($field);

But with data, you must change the database.

Change the size of a field (With data)

The 3 things that need to do:

  1. Change the VARCHAR length of the value column in the field_data_{fieldname} table
  2. Change the VARCHAR length of the value column in the field_revision_{fieldname} table
  3. Update the configuration of the field to reflect the new max length setting

 

This methode work on Drupal 7 and Drupal 8.

Example:

function mymodule_change_text_field_max_length ($field_name, $new_length) {
  $field_name = 'fieldname';
  // Get the current settings
  $result = db_query('SELECT data FROM {field_config} WHERE field_name = :name', array(':name' => $field_name))->fetchField();
  // Change the settings
  $data = unserialize($result);
  $may_length = 20000;
  if ($new_length > $may_length) {
    $new_length = $may_length;
  }
  $data['settings']['max_length'] = $new_length;
  // Write settings back to the database.
  db_update('field_config')
    ->fields(array( 'data' => serialize($data), 'type' => "text_long"))
    ->condition('field_name', $field_name)

    ->execute();
  // Update the value column in both the _data and _revision tables for the field
  $new_field = array('type' => 'varchar', 'length' => $new_length);
  $col_name = $field_name . '_value';
  db_change_field("field_data_$field_name", $col_name, $col_name, $new_field);
  db_change_field("field_revision_$field_name", $col_name, $col_name, $new_field);
  // Flush the caches
  drupal_flush_all_caches();
}
function mymodule_update_8002() {
  mymodule_change_text_field_max_length('field_name', 1000);
}

 

Another Methode :

function mymodule_change_text_field_max_length ($field_name, $new_length) {
  $field_table = 'field_data_' . $field_name;
  $field_revision_table = 'field_revision_' . $field_name;
  $field_column = $field_name . '_value';
  $may_length = 20000;
  if($new_length>$may_length){
    $new_length = $may_length;
  }

  // Alter value field length in fields table
  db_query("ALTER TABLE `{$field_table}` CHANGE `{$field_column}` `{$field_column}` VARCHAR( {$new_length} )");
  // Alter value field length in fields revision table
  db_query("ALTER TABLE `{$field_revision_table}` CHANGE `{$field_column}` `{$field_column}` VARCHAR( {$new_length} )");

//Update field type if need
//db_query("UPDATE {field_config} SET type = 'text_long' WHERE field_name = '
$field_name '");

  // Update field config with new max length
  $result = db_query("SELECT CAST(`data` AS CHAR($may_length) CHARACTER SET utf8) FROM `field_config` WHERE field_name = '{$field_name}'");
  $config = $result->fetchField();
  $config_array = unserialize($config);
  $config_array['settings']['max_length'] = $new_length;
  $config = serialize($config_array);
  db_update('field_config')
    ->fields(array('data' => $config))
    ->condition('field_name', $field_name)
    ->execute();
}
function mymodule_update_8002() {
  mymodule_change_text_field_max_length('field_name', 1000);
}

TODO : Update Field widget to text_textarea
//'widget' => array('type' => 'text_textarea')

 

Another Example : http://www.pixelite.co.nz/article/convert-existing-textfield-textarea-drupal-7/

Comments

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.