I had to dig a bit to get this to work for link fields, so I thought I'd share my code. This is based on drupal.org/node/1002560

For this example, I've got a module called "mymodule" and a field called "field_link_url" and I'm adding a "http://www.example.com" to the url field.

<?php
/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  // Add placeholder for link fields
  if (isset($form['field_link_url'])) {
    $form['#after_build'][] = 'mymodule_link_field_after_build';
  }
}

/**
 * Adds the placeholder element to link fields as an after build action due to how they are constructed.
 * @see drupal.org/node/1002560
 */
function mymodule_link_field_after_build($form, &$form_state) {
  $lang = isset($form['field_link_url']['#language']) ? $form['field_link_url']['#language'] : LANGUAGE_NONE;
  if (isset($form['field_link_url'][$lang])) {
    // Go through each delta
    foreach($form['field_link_url'][$lang] as $delta => $item) {
      // Make sure this is a delta and not just another element. 
      // Gross, but there's no clean list of delta items.
      if (is_numeric($delta)) {
        // Sanity check to make sure this is properly formed.
        if (isset($form['field_link_url'][$lang][$delta]['url'])) {
          // Add the placeholder as an attribute, because otherwise it doesn't work.
          $form['field_link_url'][$lang][$delta]['url']['#attributes']['placeholder'] = t('http://www.example.com');
        }
      }
    }
  }
  // Attach the module js file. This will actually invoke the library.
  $form['field_link_url']['#attached']['js'] = array(
    drupal_get_path('module', 'placeholder') . '/placeholder.js',
  );
  return $form;
}
?>

Improvements welcome. This doesn't seem like a big enough use case to add to the module. If someone is looking to do something similar, hopefully it will save them some time. Marking as needs review just so the maintainers see it. Feel free to close or whatever as needed. Thanks!

Comments

seanbfuller’s picture

Minor update to example build function that includes the library and checks for the placeholder module (just in case someone stumbles across this).

<?php
function mymodule_link_field_after_build($form, &$form_state) {
  if (module_exists('placeholder')){

    $lang = isset($form['field_link_url']['#language']) ? $form['field_link_url']['#language'] : LANGUAGE_NONE;
    if (isset($form['field_link_url'][$lang])) {
      // Go through each delta
      foreach($form['field_link_url'][$lang] as $delta => $item) {
        // Make sure this is a delta and not just another element. 
        // Gross, but there's no clean list of delta items.
        if (is_numeric($delta)) {
          // Sanity check to make sure this is properly formed.
          if (isset($form['field_link_url'][$lang][$delta]['url'])) {
            // Add the placeholder as an attribute, because otherwise it doesn't work.
            $form['field_link_url'][$lang][$delta]['url']['#attributes']['placeholder'] = t('http://www.example.com');
          }
        }
      }
    }
    
    // Add the library, if it's available.
    if (($library = libraries_load('placeholder', 'minified')) && !empty($library['loaded'])) {
      // Attach the library files.
      libraries_load_files($library);
      // Attach the module js file. This will actually invoke the library.
      $element['#attached']['js'] = array(
        drupal_get_path('module', 'placeholder') . '/placeholder.js',
      );
    }
    
  }
  return $form;
}
?>
bleen’s picture

Status: Needs review » Fixed

I added this to the README and committed ... thanks!

Status: Fixed » Closed (fixed)

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