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
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | after-build-hint.patch | 1.05 KB | amitaibu |
Comments
Comment #1
quicksketchFirst, 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.
Comment #2
jvandooren commentedThank you for your feedback.
I tried this before but it didn't work. Turns out my missing piece was the "value" part of:
It's working great now...
Comment #3
quicksketchComment #5
amitaibuPatch adds a generic after build. to be used with CCK.
Comment #6
manuel garcia commentedany chance we can get this patch in?