Creating Custom Field Formatter is not working

1.9k views Asked by At

I'm trying to create a new custom field formatter for text fields but nothing is showing at all.

I tried un-installing and re-installing the module and flushed the cache many times, and nothing worked.

Here's the code I used

/**
 * Implementation of hook_field_formatter_info()
 */
function facebooklink_field_formatter_info()
{
    return array(
        'cfieldformatter_text_1'    => array(
            'label'         => t('Text 1'),
            'field types'   => array('text', 'text_long', 'text_with_summary'),
            'settings'      => array(
                'pic_size'  => 'small',
                'tooltip'   => 'Link to user Facebook page',
            ),
        ),
    );
}



/**
 * Implementation of hook_field_formatter_settings_form()
 */
function facebooklink_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['pic_size'] = array(
        '#type'           => 'select',                           // Use a select box widget
        '#title'          => t('Button Size'),                   // Widget label
        '#description'    => t('Select what size of FB button'), // Helper text
        '#default_value'  => $settings['pic_size'],              // Get the value if it's already been set
        '#options'        => array(
          'small'  => 'Small',
          'medium' => 'Medium',
          'large'  => 'Large',
        ),
    );

    $element['tooltip'] = array(
        '#type'           => 'textfield',                        // Use a textbox
        '#title'          => t('Tool Tip'),                      // Widget label
        '#description'    => t('This text will appear when a user mouses over.'),  // helper text
        '#default_value'  => $settings['tooltip'],               // Get the value if it's already been set
    );

    return $element;
}



/**
 * Implementation of hook_field_formatter_settings_summary()
 */
function facebooklink_field_formatter_settings_summary($field, $instance, $view_mode)
{
    $display = $instance['display'][$view_mode];
    $settings = $display['settings'];
    $summary = t('Use a @size Facebook button with the tooltip of "@tooltip"', array(
        '@size'     => $settings['pic_size'],
        '@tooltip'  => $settings['tooltip'],
    ));

    return $summary;
}



/**
 * Implementation of hook_field_formatter_view()
 */
function facebooklink_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display)
{
    $element = array(); // Initialize the var
    $settings = $display['settings']; // get the settings
    $size = $settings['pic_size']; // The Size setting selected in the settings form
    $tooltip = $settings['tooltip']; // The tool tip assigned in settings
    // Create the image - Note that I'm storing the images in our module but they could be anywhere
    $image = '<img src="/' . drupal_get_path('module', 'facebooklink') . 'fb-' . $size . '.png">';
    foreach ($items as $delta => $item) {
      $fb = $item['safe_value']; // Getting the actual value
    }
    $options = array(
      'html'        => TRUE, // This tells Drupal that we're sending HTML, not plain text, otherwise it would encode it
      'attributes'  => array(
        'title' => $tooltip, // This sets our tooltip
        ),
      );
    if(isset($fb)) {
      $link = l($image, $fb, $options); // Create the Link
      $element[0]['#markup'] = $link; // Assign it to the #markup of the element
    }
    return $element;
}

Any help with this crazy issue?!!!.

1

There are 1 answers

0
Laurent On

I would suggest you either name your formatter 'facebooklink' instead 'cfieldformatter_text_1' in hook_field_formatter_info (this works when you have one single formatter in a module) or explicity trigger the formatter in hook_field_formatter_view as below:

/**
 * Implementation of hook_field_formatter_view()
 */
function facebooklink_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display)
    {
        $element = array(); // Initialize the var
        $settings = $display['settings']; // get the settings

        switch ($display['type']) {
          case 'cfieldformatter_text_1':
          $size = $settings['pic_size']; // The Size setting selected in the settings form
          $tooltip = $settings['tooltip']; // The tool tip assigned in settings
          // Create the image - Note that I'm storing the images in our module but they could be anywhere
          $image = '<img src="/' . drupal_get_path('module', 'facebooklink') . 'fb-' . $size . '.png">';
          foreach ($items as $delta => $item) {
            $fb = $item['safe_value']; // Getting the actual value
          }
          $options = array(
            'html'        => TRUE, // This tells Drupal that we're sending HTML, not plain text, otherwise it would encode it
            'attributes'  => array(
              'title' => $tooltip, // This sets our tooltip
              ),
            );
          if(isset($fb)) {
            $link = l($image, $fb, $options); // Create the Link
            $element[0]['#markup'] = $link; // Assign it to the #markup of the element
          }
          break;
      }    
        return $element;
    }