繁体   English   中英

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

[英]Questions about creating a custom field for drupal

我开始学习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中已有的常用Text Widget。

如果我理解你,你需要实现hook_field_widget_info_alter()并告诉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';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM