2012-03-13 56 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