Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could do this with a custom module using the <a href="http://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_presave/7" rel="nofollow">node_presave</a> hook. The users only enter the prefix value into the 'invoice_number' field. Before the node is saved to the database your hook does the following:</p> <p>if the node is of type 'invoice' and has not yet been saved 'nid == 0'</p> <ul> <li>gets the current year</li> <li>gets the current number of invoices for this year (either from a stored variable or a database query)</li> <li>alters the field value and appends the year/number</li> </ul> <p>So something along the lines of this:</p> <pre><code>&lt;?php function mymodule_node_presave($node){ if (($node-&gt;type == 'invoice') &amp;&amp; ($node-&gt;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'=&gt;'invoice',':year'=&gt;$this_year))-&gt;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-&gt;field_invoice_number['und'][0]['value'].=$this_year.$invoice_count; //save the increment variable_set('invoice_count_'.$this_year,($count+1)); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload