0

I have managed to print date and dollar amount CCK fields on a custom print.tpl.php template file (associated with the Printer, email and PDF versions module), but they are showing up in a different format than how I have them displayed in the CCK field itself.

The date is showing up like this: 2012-06-01 00:00:00
Instead of like this: June 1, 2012

And the dollar amount is showing up like this: 10000
Instead of like this: $10,000

Here are the code snippets:

    <?php if(isset($node->field_invoice_due_date['und'][0]['value'])) { 
          print $node->field_invoice_due_date['und'][0]['value'];
   }
 ?>


    <?php if(isset($node->field_invoice_amount_to_bill['und'][0]['value'])) { 
          print $node->field_invoice_amount_to_bill['und'][0]['value'];
   }
 ?>

Can anyone direct me on how I can change those two snippets to output their values in the formats I need?

Thanks!

2 Answers 2

1

Actually date() takes a timestamp so the code should be:

<?php if(isset($node->field_invoice_due_date['und'][0]['value'])) { 
          print date('F j, Y', strtotime($node->field_invoice_due_date['und'][0]['value']));
   }
 ?>

and to insert the thousands separator, you can use number_format():

<?php if(isset($node->field_invoice_amount_to_bill['und'][0]['value'])) { 
          print "$". number_format($node->field_invoice_amount_to_bill['und'][0]['value']);
   }
 ?>

if you wish to avoid a preprocess function.

3
  • Perfect! Thank you! Two follow-up items... I also have a Computed Field in this particular content type. How can I print the contents of that Computed Field and retain its formatting? I have a Textarea block as well; how can I print that while retaining its HTML formatting?
    – hockey2112
    Jun 6, 2013 at 20:24
  • Welcome. This should really be posed as a new question. Assuming you have a 'Print' display type, set the Computed field to 'Unsanitised' and the Textarea should work with 'Full HTML'. If this does not work, post a new question.
    – Triskelion
    Jun 6, 2013 at 20:43
  • Changing the textarea field's text format to "Raw HTML" (a format I created that is devoid of any HTML help) and adding line breaks where necessary allowed that field to display in HTML format. Not a perfect solution, but workable for now I suppose. My computed fields are already set to "Unsanitized". I have posted a new question, here: drupal.stackexchange.com/questions/75432/…
    – hockey2112
    Jun 6, 2013 at 21:07
2

You'll need to preprocess these elements into the template and format them to the way you want.

Let's take this practical example. Let's say we have a content type of 'blog' and we wanted to preprocess the same variables as above to be used in the template file...

/**
 * Preprocess variables for node.tpl.php
 *
 * @see node.tpl.php
 */
function theme_preprocess_node(&$vars) {
  if ($vars['type'] == 'blog') {
    if (isset($vars['field_invoice_due_date'][0]['value'])) {
      // Makes June 1, 2012 format from the time provided
      $vars['date_format'] = date("F j, Y", strtotime($vars['field_invoice_due_date'][0]['value']));
    } else {
      $vars['date_format'] = '';
    }
    if (isset($vars['field_invoice_amount_to_bill'][0]['value'])) {
      // you can use whatever function to format the money value, this is just an example.
      $vars['amount_to_bill'] = "$". number_format('%i', $vars['field_invoice_amount_to_bill'][0]['value']);
    } else {
      $vars['amount_to_bill'] = '';
    }
  }
}

if your moving these fields into a custom template, than you can preprocess with this hook as well:

 /**
 *
 * Implements hook_preprocess
 *
 * @param $variables
 * @param $hook
 */
function hook_preprocess(&$variables, $hook) {
  if ($hook == 'custom_template') {
    // do your preprocess magic here, you can find the variables by either dsm() or print_r()
  }
}

then, in the template do this:

<?php print $date_format; ?>
<?php print $amount_to_bill; ?>
3
  • The function money_format() is only defined if the system has strfmon capabilities. For example, Windows does not, so money_format() is undefined in Windows.
    – Triskelion
    Jun 6, 2013 at 20:26
  • ahh, gotcha - I'm such a linux guru. Thanks for the strtotime() Jun 6, 2013 at 20:27
  • Glad to help. It is a good answer. I will let you do this edit. Remove the formatting string: string number_format ( float $number [, int $decimals = 0 ] ). :-)
    – Triskelion
    Jun 6, 2013 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.