Drupal 7 发票的Drupal自定义文件类型

Drupal 7 发票的Drupal自定义文件类型,drupal-7,content-type,invoice,Drupal 7,Content Type,Invoice,我有一个内容类型“发票” 我想添加一个字段invoice\u number,例如:ABC2012001 ABC:前缀 2012年:每年都有变化 001:发票编号(每年重置) 该字段是自动递增的 我该怎么做?是否可以不编程,或者我必须使用钩子函数?您可以使用钩子使用自定义模块来实现这一点。用户仅在“发票编号”字段中输入前缀值。在将节点保存到数据库之前,挂钩执行以下操作: 如果节点的类型为“invoice”且尚未保存,则“nid==0” 获取当前年份 获取今年的当前发票数(从存储变量或数据库查

我有一个内容类型“发票”

我想添加一个字段
invoice\u number
,例如:
ABC2012001

  • ABC:前缀

  • 2012年:每年都有变化

  • 001:发票编号(每年重置)

该字段是自动递增的


我该怎么做?是否可以不编程,或者我必须使用钩子函数?

您可以使用钩子使用自定义模块来实现这一点。用户仅在“发票编号”字段中输入前缀值。在将节点保存到数据库之前,挂钩执行以下操作:

如果节点的类型为“invoice”且尚未保存,则“nid==0”

  • 获取当前年份
  • 获取今年的当前发票数(从存储变量或数据库查询)
  • 更改字段值并追加年份/编号
因此,大致如下:

<?php

function mymodule_node_presave($node){
    if (($node->type == 'invoice') && ($node->nid == 0)) { //node has not been saved
        //get the current year
        $this_year = date('Y');

        if ($count = variable_get('invoice_count_'.$this_year,0)){
            //have the invoice number
        }else{
            //get the number of invoices created this year from DB
            $count_query = "SELECT COUNT(DISTINCT nid)) FROM {node} WHERE type = :type AND FROM_UNIXTIME(created,'%Y') = :year";
            $count = db_query($count_query,array(':type'=>'invoice',':year'=>$this_year))->fetchField();
        }

        $invoice_count = $count;
        //append number with 0's?
        $invoice_count = str_repeat('0',(3-strlen($invoice_count))).$invoice_count;
        //alter the field value and append the year.number
        $node->field_invoice_number['und'][0]['value'].=$this_year.$invoice_count;
        //save the increment
        variable_set('invoice_count_'.$this_year,($count+1));
    }

}