【问题标题】:Questions about creating a custom field for drupal关于为 drupal 创建自定义字段的问题
【发布时间】:2012-03-13 20:01:35
【问题描述】:

我开始学习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 中已有的常用文本小部件。

【问题讨论】:

    标签: php drupal drupal-fields


    【解决方案1】:

    如果我理解正确,您需要实现 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';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-08
      • 1970-01-01
      相关资源
      最近更新 更多