duan0821 2012-03-13 20:01
浏览 15
已采纳

有关为drupal创建自定义字段的问题

I started learning drupal customization, and i'm trying to create a very simply custom field for drupal.

I tried to follow several tutorial, but when i install the field (apparently without problem) it doesn't appear into the field list. But if i try to look at the source code, my field is with "hidden" attribute.

Actually i developed 2 files, the info file, and the module_file.

Here the code for module:

<?php
/**
 * @pricefield.module
 * add a price field.
 *
 */
 /**
 * Implements hook_field_formatter_info().
 */
function pricefield_field_formatter_info() {
  return array(
    'pricefield_custom_type' => array( //Machine name of the formatter
      'label' => t('Price'),
      'field types' => array('text'), //This will only be available to text fields
      'settings'  => array( //Array of the settings we'll create
        'currency' => '$', //give a default value for when the form is first loaded        
      ),            
    ),
  );
}
/**
 * Implements hook_field_formatter_settings_form().
 */
function pricefield_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();
  //Add your select box
  $element['currency'] = array(
    '#type'           => 'textfield',                           // Use a select box widget
    '#title'          => 'Select Currency',                   // Widget label
    '#description'    => t('Select currency used by the field'), // Helper text
    '#default_value'  => $settings['currency'],              // Get the value if it's already been set    
  );
  return $element;
}
/**
 * Implements hook_field_formatter_settings_summary().
 */
function pricefield_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = t('The default currency is: @currency ', array(
    '@currency'     => $settings['currency'],    
  )); // we use t() for translation and placeholders to guard against attacks
  return $summary;
}
/**
 * Implements hook_field_formatter_view().
 */
function pricefield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array(); // Initialize the var
  $settings = $display['settings']; // get the settings
  $currency = $settings['currency']; // Get the currency  
  foreach ($items as $delta => $item) {
    $price = $item['safe_value']; // Getting the actual value
  }  
  if($price==0){
      $element[0] = array('#markup' => 'Free');
  } else {
      $element[0] = array('#markup' => $currency.' '.$price);
  }
  return $element;
}
?>

I'm not sure if the problem is a missing installation file. I tried to look at several of them but they are so different. I don't understand how to add my custom field to the database (i think it is needed). I must do a query? Or i must use some function.

I need to make a mymodule_install method? Or in that case is needed only the mymodule_field_schema? (looking at different basic module, some of them implement only that function, but other implement an insatll method, and not the field_schema).

So for example if i want to add my custom field that will be a string, and it need only a textbox what i still need to do, in order to have my field available on drupal?

Basically i don't need a new widget for my custom field, i want to use the usual Text Widget already available in drupal.

  • 写回答

1条回答 默认 最新

  • douhuifen9942 2012-03-13 23:55
    关注

    If I understand you right you need to implement hook_field_widget_info_alter() and tell Drupal that the textfield widget can be used by your field:

    function pricefield_field_widget_info_alter(&$info) {
      // 'pricefield' will be whatever the machine name of your field is
      $info['text_textfield']['field types'][] = 'pricefield';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀