我正在尝试为文本字段创建一个新的自定义字段格式化程序,但是什么都没有显示。
我尝试卸载并重新安装模块,并多次刷新了缓存,但没有任何效果。
这是我使用的代码
/**
* 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;
}
对这个疯狂的问题有帮助吗?!
最佳答案
我建议您在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;
}
关于drupal - 创建自定义字段格式化程序不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12012337/