Creating Custom Field Formatter is not working
Asked Answered
G

1

6

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?!!!.

Ghazi answered 17/8, 2012 at 19:53 Comment(11)
I know the tutorial you've followed. Have you tried using the exact code from that and then adjusting it once that works?Scriber
I did, I used the exact code. And it did not work either.Ghazi
Strange. I've used it and it worked for me. I'll have a look when I get some spare time for you.Scriber
could you plz upload your module as is and send me the link ?!Ghazi
Here's a link to a custom formatter for YouTube video -> github.com/SpaceBeers/Drupal-YouTube-FormatterScriber
Many thanks. en-sha2 Allah, I'll have a look at it. Thanks again.Ghazi
What is your one trying to do? Just a Facebook link?Scriber
Not exactly, I wanna create my first custom field formatter, so I tried to follow this tutorial.Ghazi
Ah ok. Try and get my one working and see if you can adjust that.Scriber
That's what I'm planning to do. I'll keep you updated, Thanks :)Ghazi
I tried your module and it didn't work either. Kindly note that I installed in on a different drupal installation. Here's a screenshot for the formGhazi
R
1

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;
    }
Receptive answered 11/9, 2012 at 6:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.