I'm trying to adjust the max_length attribute of a field in Drupal

1.4k views Asked by At

The reason I need to do this is because we are turning our CMS over to a client and we need them to follow the rules but we can't just change the value outright as there is content in the system that breaks the max length rules for some fields and we don't want them to get chopped. So going ahead we just want to target all new content by targeting the new content form (node-form). I currently have a hook that successfully changes the max value for the title below:

function myForm_form_alter(&$form, &$form_state, $form_id) {
  if($form['#id'] == 'node-form') {
     $form['title']['#maxlength'] = 30;  
  }   
}

I need to add one for a field named "field_feature_desc". I printed the form info to the screen and here is the info I received for that field:

[field_feature_desc] => Array
                (
                    [field_name] => field_feature_desc
                    [type_name] => article
                    [display_settings] => Array
                        (
                            [weight] => 11
                            [parent] => 
                            [label] => Array
                                (
                                    [format] => above
                                )

                            [teaser] => Array
                                (
                                    [format] => default
                                    [exclude] => 1
                                )

                            [full] => Array
                                (
                                    [format] => default
                                    [exclude] => 1
                                )

                            [4] => Array
                                (
                                    [format] => default
                                    [exclude] => 1
                                )

                            [2] => Array
                                (
                                    [format] => default
                                    [exclude] => 0
                                )

                            [3] => Array
                                (
                                    [format] => default
                                    [exclude] => 0
                                )

                        )

                    [widget_active] => 1
                    [type] => text
                    [required] => 0
                    [multiple] => 0
                    [db_storage] => 0
                    [module] => text
                    [active] => 1
                    [locked] => 0
                    [columns] => Array
                        (
                            [value] => Array
                                (
                                    [type] => text
                                    [size] => big
                                    [not null] => 
                                    [sortable] => 1
                                    [views] => 1
                                )

                        )

                    [text_processing] => 0
                    [max_length] => 
                    [allowed_values] => 
                    [allowed_values_php] => 
                    [widget] => Array
                        (
                            [rows] => 5
                            [size] => 60
                            [default_value] => Array
                                (
                                    [0] => Array
                                        (
                                            [value] => 
                                            [_error_element] => default_value_widget][field_feature_desc][0][value
                                        )

                                )

                            [default_value_php] => 
                            [label] => Feature Description
                            [weight] => -3
                            [description] => Description text in the feature area on the homepage, if applicable
                            [type] => text_textfield
                            [module] => text
                        )

                )

If you have an idea how i would code for this additional field it would be much appreciated. I've been trying the following:

$form['field_feature_desc']['max_length'] = 70;
$form['field_feature_desc']['columns']['value']['length'] = 70;

Sorry if there is already an answer for this question, I looked all over for one and couldn't locate one.

1

There are 1 answers

0
reub77 On

I discovered an array outside of the one i was working with. So the code that did it was:

     $form['#field_info'] = array(
        'field_feature_desc' => array(
           'max_length' => 70,
        ),
     );

Or a shorter version is:

$form['#field_info']['field_feature_desc']['max_length'] = 70;