Php 关于为drupal创建自定义字段的问题

Php 关于为drupal创建自定义字段的问题,php,drupal,drupal-fields,Php,Drupal,Drupal Fields,我开始学习drupal定制,并尝试为drupal创建一个非常简单的定制字段 我试着遵循几个教程,但当我安装字段时(显然没有问题),它不会出现在字段列表中。 但是如果我尝试查看源代码,我的字段将具有“hidden”属性 实际上我开发了两个文件,信息文件和模块文件 下面是模块的代码: <?php /** * @pricefield.module * add a price field. * */ /** * Implements hook_field_formatter_info(

我开始学习drupal定制,并尝试为drupal创建一个非常简单的定制字段

我试着遵循几个教程,但当我安装字段时(显然没有问题),它不会出现在字段列表中。 但是如果我尝试查看源代码,我的字段将具有“hidden”属性

实际上我开发了两个文件,信息文件和模块文件

下面是模块的代码:

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

如果我理解正确,您需要实现并告诉Drupal您的字段可以使用textfield小部件:

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