2

I'm currently working on a project created by someone else. I have a textfield called "city". Currently the maximum limit is 255 characters. I want to update the maximum character to be 100. Looking at the code, it looks like the field was created using the features module. When I try to update the field, I got the FieldUpdateForbiddenException. I understand this is because I have value in the database. Is there a way update the field even if there is value in there? If I can't modify the database, can I change textfield max length in the form to be 100?

Thanks!

The website doesn't have CCK, so I'm not sure how to modify these fields. I'm using Drupal 7.

What I did: 1. I upgraded registration_page.info to upgrade the version from 7.x-1.0 to 7.x-1.1 2. I changed the field max length to 100 in registration.features.field.info

When I tried to deploy, I got this error

  [sshexec] WD php: FieldUpdateForbiddenException: field_sql_storage cannot          [error]
  [sshexec] change the schema for an existing field with data. in
  [sshexec] field_sql_storage_field_update_forbid() (line 229 of
  [sshexec] /var/lib/www.mine.com/deploys/site_20140515_140323/modules/field/modules/field_sql_storage/field_sql_storage.module).
  [sshexec] Cannot modify header information - headers already sent by (output     [warning]
  [sshexec] started at
  [sshexec] /var/lib/www.mine.com/deploys/site_20140515_140323/sites/default/drushrc.php:2)
  [sshexec] bootstrap.inc:1212
  [sshexec] FieldUpdateForbiddenException: field_sql_storage cannot change the schema for an existing field with data. in field_sql_storage_field_update_forbid()

This is what the code look like in registration.features.field.info

 // Exported field: 'node-registration-page-field_city'.
  $fields['node-registration_page-field_city'] = array(
    'field_config' => array(
      'active' => '1',
      'cardinality' => '1',
      'deleted' => '0',
      'entity_types' => array(),
      'field_name' => 'field_city',
      'foreign keys' => array(
        'format' => array(
          'columns' => array(
            'format' => 'format',
          ),
          'table' => 'filter_format',
        ),
      ),
      'indexes' => array(
        'format' => array(
          0 => 'format',
        ),
      ),
      'locked' => '0',
      'module' => 'text',
      'settings' => array(
        'max_length' => '255',
      ),
      'translatable' => '0',
      'type' => 'text',
    ),
    'field_instance' => array(
      'bundle' => 'registration_page',
      'default_value' => NULL,
      'deleted' => '0',
      'description' => 'Please enter your city',
      'display' => array(
        'default' => array(
          'label' => 'above',
          'module' => 'text',
          'settings' => array(),
          'type' => 'text_default',
          'weight' => 31,
        ),
        'teaser' => array(
          'label' => 'above',
          'settings' => array(),
          'type' => 'hidden',
          'weight' => 0,
        ),
      ),
      'entity_type' => 'node',
      'field_name' => 'field_city',
      'label' => 'City',
      'required' => 0,
      'settings' => array(
        'text_processing' => '0',
        'user_register_form' => FALSE,
      ),
      'widget' => array(
        'active' => 1,
        'module' => 'text',
        'settings' => array(
          'size' => '60',
        ),
        'type' => 'text_textfield',
        'weight' => '26',
      ),
    ),
  );
0

As you mentioned, you already have textfield with 255 character length and also contains data. If you want reduce length of this field then you need to make sure that there is no data within this field having more than 100 character length. Because if this field contains any data more than 100 character length then you can't alter it in this way. My suggestion to you is first take backup of this table and empty this table. Then you would be able to change its length and then you can import this table data. If there is any data more than 100 character either it will stored in truncated way or give you any warnings for this. One thing you need to remember you need to do this offline, this question is specific to database and take care of every piece of data. There is possibility you might need to make some changes manually, so should take data base backup first.

1
  • I can't take the site offline. Is it possible to just limit the length on the UI side?
    – Nhu
    May 16 '14 at 16:12
0

If you can't take the site offline, you can write custom code for validation of character length of your text field in your custom module.

Just get the value of your text field before saving to database and count the length and decide to save or generate error.

For ex field is used in node then implement hook_node_presave() or in hook_form_alter define $form['#validation'] function and generate error if length is greater than 100.

if you want to put this only on new addition of fields then you can check $op for new tag else just check when the user will edit the existing field it will check the length.

Hope it helps.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.