Hello

I was trying to use the Hint module together with CCK text fields but I ran into some problems.
I'll explain what I did here and create a patch later if needed.

I wanted to implement hook_form_alter() using my own module. I added the specific form to the $supported_forms array and checked it in the admin form.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if (hint_enabled_forms($form_id)) {
    switch ($form_id) {
      case 'tp_node_form':
        // CCK Text Field
        hint_set_hint($form['field_naam'][0], t('Help text name'));
        // OR
        hint_set_hint($form['field_naam'], t('Help text name'));
        
        // Default title 
        hint_set_hint($form['title'], t('Help text title'));
        break;
      default:
    }
  }
}

OK, this works for the Title field, but not for my CCK name field. So I did some debugging and found out that the #Hint attribute for the CCK field is not set when Drupal calls "hint_pre_render".
I read somewhere on Drupal.org that I had to use the "$form['#after_build']" attribute. So I tried:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if (hint_enabled_forms($form_id)) {
    switch ($form_id) {
      case 'tp_node_form':
        $form['#after_build'][] = '_mymodule_after_build';
        break;
      default:
    }
  }
}

function _mymodule_after_build($form, &$form_state) {
  hint_set_hint($form['field_naam'], t('Help text name'));
  // OR
  hint_set_hint($form['field_naam'][0], t('Help text name'));
  hint_set_hint($form['title'], t('Help text title'));
  return $form;
}

This still didn't solve my problem because the #Hint attribute is still unavailable when the "hint_pre_render" function is called.
So I did some more research and tried the following change in the Hint module:

function hint_set_hint(&$element, $hint = NULL) {
  foreach (element_children($element) as $key) {
    if (isset($element[$key]) && $element[$key]) {
      // Recurse through all children elements.
      hint_set_hint($element[$key], $hint);
    }
  }

  if (!isset($hint)) {
    $element['#hint'] = $element['#title'];
  }
  else{
    $element['#hint'] = $hint;
  }
  $element['#hint_remove_title'] = TRUE;
}

With this change (together with the #after_build change), my custom text or title is rendered correctly in the text field, disappearing when it gets focus.

So if anyone has the same problem... Try my solution.
If anyone knows a better way to do this... Please let me know.

Ozmodiar

CommentFileSizeAuthor
#5 after-build-hint.patch1.05 KBamitaibu

Comments

quicksketch’s picture

First, I would recommend you just use the #hint property rather than using hint_set_hint(), which is intended to be a shortcut if you want to remove the label of the field and set it to #hint all at the same time. You shouldn't use hint_enabled_forms($form_id) at all, since that's just for the built-in predefined forms that hint modifies.

I think you're mostly correct with needing to use #after_build (or #process) when dealing CCK fields, this is because these fields don't usually exist initially but they are created later. The problem with your code is that CCK textfields create a "value" key that stores the actual textfield.

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['field_first_name'][0])) {
    $form['field_first_name'][0]['#after_build'][] = 'hint_something';
  }
}

function mymodule_something($element) {
  $element['value']['#hint'] = 'First';
  return $element;
}
jvandooren’s picture

Thank you for your feedback.

I tried this before but it didn't work. Turns out my missing piece was the "value" part of:

$element['value']['#hint'] = 'First';

It's working great now...

quicksketch’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

amitaibu’s picture

Category: support » feature
Status: Closed (fixed) » Needs review
StatusFileSize
new1.05 KB

Patch adds a generic after build. to be used with CCK.

manuel garcia’s picture

any chance we can get this patch in?