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:
- Change the VARCHAR length of the value column in the field_data_{fieldname} table
 - Change the VARCHAR length of the value column in the field_revision_{fieldname} table
 - 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/
Comments4
Hi! Where is this code…
Hi! Where is this code supposed to be used? Thanks
Create a custom module
Hi,
For that you must create a custom module like : /en/tutoriels/5/create-a-simple-module-for-drupal-8-step-2-create-simple-page
then use the file yourmodule.module
Undefined table: 7 ERROR: …
Undefined table: 7 ERROR: relation "field_config" does not exist
Use db abstraction layer
Hello,
The code
// 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} )");
Does not use the databse abstraction layer : it will succeed for mysql but fail with other (postgre...).
You should use db_change_field() instead.
https://api.drupal.org/api/drupal/core%21includes%21database.inc/function/db_change_field/8.2.x