创建自定义字段格式化程序不起作用

时间:2012-08-17 19:53:40

标签: drupal drupal-7 drupal-modules drupal-fields drupal-field-api

我正在尝试为文本字段创建一个新的自定义字段格式化程序,但根本没有显示。

我尝试卸载并重新安装模块并多次刷新缓存,但没有任何效果。

这是我使用的代码

/**
 * 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;
}

对这个疯狂的问题有什么帮助吗?!!!。

1 个答案:

答案 0 :(得分:1)

我建议您在hook_field_formatter_info中将格式化程序'facebooklink'命名为'cfieldformatter_text_1'(这在模块中有一个格式化程序时有效)或明确触发hook_field_formatter_view中的格式化程序,如下所示:

/**
 * 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;
    }