0

I'd like to add placeholders to node_add field inputs. I'm trying to use dpm($form) but am not sure where to put it. I couldn't get node-add.tpl to work with this

 function MYTHEME_theme() {

  $items = array();

  $items['node_add'] = array(
    'render element' => 'form',
    'path' => drupal_get_path('theme', 'MYTHEME') . '/templates',
    'template' => 'node-add',
    'preprocess functions' => array(
      'MYTHEME_bootstrap_preprocess_node_add'
    ),
  );

  return $items;

}

The only template I got to work was page--node--add.tpl but $form shows a blank krumo.

1

To add placeholders, or any custom attributes, you need to dig deeper: implement appropriate hook_form_FORM_ID_alter. For node add/edit form, it will be mymodule_form_node_form_alter. First parameter is &$form - as you can see, it is taken by reference, so changes made to it are carried outside the function. Use dpm($form) to find appropriate element. Once you do, use #attributes to add attributes you want.

2
  • Had no idea you could dpm in a custom module. Drupal theming is confusing. Got it working. Thanks for the help. – delz Aug 4 '15 at 22:57
  • @delz dpm is a devel function, you can use it almost anywhere. Just remove it after development is finished. – Mołot Aug 5 '15 at 5:49
0

For adding placeholders you could use the #attributes property of the form elements. I'd suggest using a form_alter-hook for that. You can put the code into the template.php of your theme (or into it's own module). It would look something like:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MYTHEME_form_node_form_alter(&$form, &$form_state) {
  $form['title']['#attributes']['placeholder'] = t('Title placeholder');
}
3
  • 1
    As far as I remember (and it seems confirmed in discussion here) you are not right - form alter can be used in template.php – Mołot Aug 4 '15 at 19:28
  • Yes, you are right. It can be verfied by looking at the source of drupal_alter(). alter-hooks are always invoked for the active theme and it's base_themes. [Correcting the answer …] – zwirbeltier Aug 4 '15 at 19:33
  • Ah it has one caveat: It only works iff the theme has been initialized in the current request. Depending on which hook you are trying to implement it might fail in some cases. – zwirbeltier Aug 4 '15 at 19:37

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.