私はdrupalのカスタマイズを学び始め、drupal用の非常に単純なカスタムフィールドを作成しようとしています。
いくつかのチュートリアルに従おうとしましたが、フィールドをインストールすると(明らかに問題なく)、フィールドリストに表示されません。しかし、ソースコードを見ようとすると、私のフィールドは「hidden」属性になります。
実際、私は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ですでに利用可能な通常のテキストウィジェットを使用したいと思います。