0

I want to change #weight, #prefix, #title for a cck field.

First I tried the following code.

function test_form_taxonomy_form_term_alter(&$form, &$form_state, $form_id) {
    //Working   
    $form['field_test1']['#weight'] = -5;   
    $form['field_test1']['#prefix'] = 'This text will rule them all!';

    //Not working
    $form['field_test1']['#title'] =  'test title';
    $form['field_test1']['und']['#title'] = 'test title';
    $form['field_test1']['und'][0]['#title'] = 'test title';        
}

It is working good with #weight, #prefix elements.

//Working   
$form['field_test1']['#weight'] = -5;   
$form['field_test1']['#prefix'] = 'This text will rule them all!';

But not working for #title element in anyways.

$form['field_test1']['#title'] =  'test title'; //Not working
$form['field_test1']['und']['#title'] = 'test title'; //Not working
$form['field_test1']['und'][0]['#title'] = 'test title'; //Not working

I tried with #after_build as well but no luck.

function test_form_taxonomy_form_term_alter(&$form, &$form_state, $form_id) {
    $form['#after_build'][] = 'test_after_build';   
}
function test_after_build($form, &$form_state) {
    //Working   
    $form['field_test1']['#weight'] = -5;   
    $form['field_test1']['#prefix'] = 'This text will rule them all!';  

    $form['field_test1']['#title'] =  'test title'; //Not working
    $form['field_test1']['und']['#title'] = 'test title'; //Not working
    $form['field_test1']['und'][0]['#title'] = 'test title'; //Not working  

    return $form;
}

Please give me the solution for alter cck field title through form_alter.

Thanks in advance, Logan

1 Answer 1

0

First, you may check the exact field array with dsm()

function MODULE_form_taxonomy_form_term_alter(&$form, &$form_state, $form_id) {
  dsm($form);
}

form taxonomy dsm

In the result returned by dsm(), #title is set in 2 places. So just try :

$form['field_bibliographie']['#title']= t('Foo');//doesn't work
$form['field_bibliographie']['und'][0]['#title']= t('Foo');//correct one

I see no reason for which it would not take the correct value if you set the exact field array

2
  • In the taxonomy taken as example, I don't know why #title is found in 2 places, if somebody wishes to comment/explain that point
    – Kojo
    Commented Feb 8, 2015 at 15:57
  • I already used dsm and tried but no luck.
    – Logan
    Commented Feb 9, 2015 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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