2012-03-13 48 views
0

我开始学习drupal自定义,并且我正在尝试为drupal创建一个非常简单的自定义字段。关于为drupal创建自定义字段的问题

我试着按照几个教程,但是当我安装字段(显然没有问题)它不会出现在字段列表中。 但是,如果我试图看看源代码,我的领域是与“隐藏”属性。

其实我开发了2个文件,info文件和module_file。

下面的代码模块:

<?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; 
} 
?> 

我不知道,如果问题是缺少安装文件。我试图看看其中几个,但他们是如此不同。 我不明白如何将我的自定义字段添加到数据库(我认为这是必要的)。我必须做一个查询?或者我必须使用一些功能。

我需要做一个mymodule_install方法?或者在这种情况下只需要mymodule_field_schema? (看着不同的基本模块,其中一些只实现该功能,但其他实施insatll方法,而不是field_schema)。

因此,举例来说,如果我想补充一点,将是一个字符串我的自定义字段,并且只需要一个文本框什么,我仍然需要做的,为了对现有的Drupal我的领域?

基本上我不需要为我的自定义字段一个新的小工具,我想用普通的文本控件在Drupal已经可用。

回答

2

如果我理解你的权利,你需要实现hook_field_widget_info_alter(),并告诉Drupal的,该文本框控件可以通过你的领域被使用:

function pricefield_field_widget_info_alter(&$info) { 
    // 'pricefield' will be whatever the machine name of your field is 
    $info['text_textfield']['field types'][] = 'pricefield'; 
} 
+0

谢谢它的工作:) – Ivan 2012-03-14 21:47:49