1

I'm having difficulty getting my custom field formatter to activate. My intention is to allow users to request a 'Copy to Clipboard' button to be added to a text field.

I've tried to follow online tutorials and examples:

http://www.metaltoad.com/blog/drupal-7-tutorial-creating-custom-formatters

https://github.com/SpaceBeers/Drupal-YouTube-Formatter/blob/master/youtube_formatter.module

and I've come up with the following (in a file clipboard.module, referenced from clipboard.info): the formatter_view() function is currently left empty until I figure the other stuff out.

How can I trigger this code? I've tried unloading and reloading the module, clearing the cache, but whenever I add a new text field and then go into the Field Settings for that field, I don't see any form appearing for the custom settings. Should I be setting some other config elsewhere in Drupal in order to enable this?

Thanks in advance for any help.

/**
 * Implements hook_field_formatter_info().
 *
 */

function clipboard_field_formatter_info() {
  return array(
      'clipboard_formatter' => array(
      'label' => t('Copy to Clipboard'),
      'field types' => array('text','text_long','text_with_summary'),
      'settings' => array('show_clipboard_copy_button' => FALSE),
      )
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 */

function clipboard_field_formatter_settings_form($field, $instance,   $view_mode, $form, &$form_state) {
  //This gets the view_mode where our settings are stored
  $display = $instance['display'][$view_mode];
  //This gets the actual settings
  $settings = $display['settings'];
  // Initialize the element variable
  $element = array();
  $element['show_clipboard_copy_button'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable/Disable Button'),
    '#description' => t('Controls display of Copy to Clipboard Button'),
    '#default_value' => $settings['show_clipboard_copy_button'],
  );

  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */

function clipboard_field_formatter_settings_summary($field, $instance,     $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = t('Enable or Disable a Copy to Clipboard button for text fields', array(
    '@show_clipboard_copy_button'     =>     $settings['show_clipboard_copy_button'],
    )
  );
  return $summary;
}

/**
 * Implements hook_field_formatter_view()
 */

function clipboard_field_formatter_view($entity_type, $entity, $field,     $instance, $langcode, $items, $display) {

  $element = array();
  return $element;

}
1

You are not supposed to see the settings form in Manage Fields > edit (a specific field) > Field settings as you expect. Formatters are used for managing the display of fields (theming). So, go to 'Manage Display' tab (for your Content Type) and choose the view mode for which you want to manage the button visibility (default, full content, teaser, e.t.c). Find the field(s) you wish this functionality to be applied, and then select as 'Format' option, the label of the formatter you just created, which in this case is 'Copy to Clipboard'. Then if you click on the cogwheel icon on the right, you will see your settings form. Note that this setting will be applicable to all instances of this CT for the specified field for the specified view mode.

If you want other fields of the same CT as well, you need to repeat the process for the other fields as well. If you want this functionality for additional displays of this CT (e.g. teaser view), then you need to repeat the process. And of course, repeat the process for all CTs/view modes/fields as well. This is because Drupal offers much flexibility and variety in displaying content. If you want such a functionality to be applied globally (e.g. for all CTs, for all text fields) then perhaps you should consider another strategy; a new theming function or override of an existing one for example.

  • Thank you Argyro!! I can now see my custom formatters. But now I discover that the field in question already has a custom formatter (from the ace_editor module) so I guess I'll have to look at a different strategy anyway. Time to learn about theming maybe!! – Monkeybrain Aug 5 '15 at 16:25

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.