2012-03-13 10 views
0

私はdrupalのカスタマイズを学び始めました。drupal用の非常に単純なカスタムフィールドを作成しようとしています。drupalのカスタムフィールドの作成に関する質問

私はいくつかのチュートリアルに従おうとしましたが、フィールドをインストールすると(明らかに問題なし)、フィールドリストには表示されません。 しかし、私はソースコードを見てみると、私のフィールドは "隠された"属性です。

実際に私は2つのファイル、情報ファイル、および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

関連する問題