나는 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 만 필요합니까? (다른 기본 모듈을 살펴보면 그 중 일부는 해당 기능 만 구현하지만 다른 모듈은 field_schema가 아닌 insatll 메소드를 구현합니다).
예를 들어 문자열이 될 사용자 정의 필드를 추가하고 Drupal에서 필드를 사용할 수 있도록 텍스트 상자 만 있으면됩니다.
기본적으로 내 사용자 정의 필드에 새 위젯이 필요하지 않습니다. Drupal에서 이미 사용 가능한 일반적인 텍스트 위젯을 사용하고 싶습니다.
감사는 :) 일 – Ivan