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 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了