I'm trying following code in hook_form_alter()

$form[$field_name]['#attributes'] = array('onchange' => "alert('test');");

where $field_name is existing object provided by text module (cck).

But it doesn't work.
In theme_content_multiple_values() there is Drupal recursive call to:

      $output .= drupal_render($element[$key]);

Here is $element (so when parsing $element[$key] there is no #attributes):

$element
: array = 
  #programmed: bool = FALSE
  #post: array = 
  #attributes: array = 
    onchange: string = "alert('test');"
  #parents: array = 
    0: string = "field_address_city"
  #array_parents: array = 
    0: string = "group_parent_details"
    1: &string = "field_address_city"
  #defaults_loaded: bool = TRUE
  #processed: bool = FALSE
  #count: long = 5
  #access: bool = TRUE
  #description: string = ""
  #required: string = "1"
  #title: string = "Town/City"
  0: array = 
    #type: string = "text_textfield"
    #default_value: array = 
      value: undefined = NULL
    #title: string = "Town/City"
    #description: string = ""
    #required: bool = TRUE
    #weight: long = 0
    #delta: &long = 0
    #columns: array = 
      0: string = "value"
    #field_name: string = "field_address_city"
    #type_name: string = "parent"
    #post: &array = 
    #programmed: &bool = FALSE
    #tree: bool = TRUE
    #parents: array = 
      0: string = "field_address_city"
      1: long = 0
    #array_parents: array = 
      0: &string = "group_parent_details"
      1: &string = "field_address_city"
      2: &long = 0
    #processed: bool = TRUE
    #attributes: array = 
    #input: bool = TRUE
    #process: array = 
      0: string = "text_textfield_process"
    #autocomplete_path: bool = FALSE
    #name: string = "field_address_city[0]"
    #id: string = "edit-field-address-city-0"
    #value: array = 
      value: &undefined = NULL
    value: array = 
      #type: string = "textfield"
      #default_value: undefined = NULL
      #autocomplete_path: &bool = FALSE
      #size: string = "60"
      #attributes: array = 
        class: string = "text"
      #title: &string = "Town/City"
      #description: &string = ""
      #required: &bool = TRUE
      #field_name: &string = "field_address_city"
      #type_name: &string = "parent"
      #delta: &long = 0
      #columns: &array = 
      #maxlength: undefined = NULL
      #post: &array = 
      #programmed: &bool = FALSE
      #tree: &bool = TRUE
      #parents: array = 
        0: string = "field_address_city"
        1: &long = 0
        2: string = "value"
      #array_parents: array = 
        0: &string = "group_parent_details"
        1: &string = "field_address_city"
        2: &long = 0
        3: &string = "value"
      #weight: long = 0
      #processed: bool = TRUE
      #input: bool = TRUE
      #process: array = 
        0: string = "form_expand_ahah"
        1: string = "fckeditor_process_input"
      #name: string = "field_address_city[0][value]"
      #id: string = "edit-field-address-city-0-value"
      #value: string = ""
      #defaults_loaded: bool = TRUE
      #sorted: bool = TRUE
      #printed: bool = TRUE
    _error_element: array = 
      #type: string = "value"
      #value: string = "field_address_city][0][value"
      #post: &array = 
      #programmed: &bool = FALSE
      #tree: &bool = TRUE
      #parents: array = 
        0: string = "field_address_city"
        1: &long = 0
        2: string = "_error_element"
      #array_parents: array = 
        0: &string = "group_parent_details"
        1: &string = "field_address_city"
        2: &long = 0
        3: &string = "_error_element"
      #weight: double = 0.001
      #processed: bool = FALSE
      #description: undefined = NULL
      #attributes: &array = 
      #required: bool = FALSE
      #input: bool = TRUE
      #name: string = "field_address_city[0][_error_element]"
      #id: string = "edit-field-address-city-0--error-element"
      #defaults_loaded: bool = TRUE
      #sorted: bool = TRUE
      #title: undefined = NULL
      #printed: bool = TRUE
    #defaults_loaded: bool = TRUE
    #sorted: bool = TRUE
    #children: string = "<div class=\"form-item\" id=\"edit-field-address-city-0-value-wrapper\">\n <label for=\"edit-field-address-city-0-value\">Town/City: <span class=\"form-required\" title=\"This field is required.\">*</span></label>\n <input type=\"text\" name=\"field_address_city[0][value]\" id=\"edit-field-address-city-0-value\" size=\"60\" value=\"\" class=\"form-text required text\" />\n</div>\n"
    #printed: bool = TRUE
  #field_name: &string = "field_address_city"
...
  #theme: string = "content_multiple_values"
...
  #type: string = "markup"

I've tried as well with $form[$field_name][0]['#attributes']

I've got somewhere else element:

              '#type'    => 'select',
              '#attributes' => array ('onchange' => 'CopyToFck();'),

and it does work.

What I'm doing wrong?

Comments

bjaspan’s picture

Project: Drupal core » Content Construction Kit (CCK)
Version: 6.x-dev » 6.x-2.x-dev
Component: field system » General

I think this is a D6 CCK question, not a D7 Field API question.

kenorb’s picture

Component: General » text.module
Category: support » bug

Yes, but it's good to try:)

Probably I know what's going on.
When I set that, it's being overridden by function text_textfield_process()

  $field = $form['#field_info'][$element['#field_name']];
  $field_key = $element['#columns'][0];
  $delta = $element['#delta'];
  $element[$field_key] = array(
    '#type' => 'textfield',
    '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
    '#autocomplete_path' => $element['#autocomplete_path'],
    '#size' => !empty($field['widget']['size']) ? $field['widget']['size'] : 60,
    '#attributes' => array('class' => 'text'),
    // The following values were set by the content module and need
    // to be passed down to the nested element.
    '#title' => $element['#title'],
    '#description' => $element['#description'],
    '#required' => $element['#required'],
    '#field_name' => $element['#field_name'],
    '#type_name' => $element['#type_name'],
    '#delta' => $element['#delta'],
    '#columns' => $element['#columns'],

Because there is hardcoded '#attributes' property?

    '#attributes' => array('class' => 'text'),

Is there any right way to add '#attributes' into text field to be properly rendered?

kenorb’s picture

jonskulski’s picture

If you var_dump $element['#attributes'] you will see that any #attribute you set in a form_alter is not present during the _processing.

So I believe the problem is deeper than just being over written.