0

I have a partially working solution for using hook_form after_build on a CCK field. The field has various elements (it's created with a custom module but I don't want to modify that so I've created a small overrides module to do some trivial form alter stuff.) I've been at this a good part of the day and finally had some luck adding some simple text to the field so I am know I'm getting close.

In the main module, the CCK field has various elements. Once such element is:

$element['week_night'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Week Night'),
    '#default_value' => isset( $element['#value']['week_night'] ) ? $element['#value']['week_night'] : NULL,
    '#attributes'    => array( 'class' => 'rate-widget-rate-field' )
  );

I am targeting the field itself in my overrides module as such:

<?php 
function rates_override_form_alter(&$form, $form_state, $form_id) {
  //drupal_set_message($form_id);
  //dsm($form);
  if($form_id == 'ldisilink_property_node_form'){
    $form['#after_build'][] = '_rates_override_after_build';
  }
}

function _rates_override_after_build($form, &$form_state) {
  // This works so we know we are good to this point:
  $form['field_rates']['#prefix'] = 'do stuff';
  // Now we want to change the label on week_night but it's not working. 
   $week_night = $element['week_night'];
   $form['field_rates'][$week_night]['#title'] = 'My new label title';
  return $form;
}

What I am trying to do is alter the label for the element week_night. Using dsm($form) I've tried various combinations of:

   $week_night = $element['week_night'];
   $form['field_rates'][$week_night]['#title'] = 'My new label title';

... but it's not working yet, but I think I'm close.

2
  • where/how are you setting $element in your _rates_override_after_build() function?
    – Jimajamma
    Feb 26, 2013 at 2:49
  • @Jimajamma -- ah yes, it would appear I need to set the $elements variable. I just could not find any documentation on that part of CCK. Feb 26, 2013 at 5:38

1 Answer 1

0

You are on the right track. You want to set the #title attribute of the form element whose label you want to change, which can all be done at the hook_form_alter() time.

In order to change the label of the 'week_night' form element, try something like this:

function rates_override_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'ldisilink_property_node_form') {
    $form['field_rates']['week_night']['#title'] = t('My new label');
}

This is assuming the 'week_night' form element is nested within 'field_rates' fieldset. If that is not the case, use:

$form['week_night']['#title'] = t('My new label');

instead.

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.