Hi,

is it possible to add an suffix and/or a prefix to the calculated field? Like adding a dollar or euro-sign before or after the calculated numbers?

Thanks,
Oscar

Comments

Moonshine’s picture

If you're saving computed values as being purely numeric, then you can just add the formatting via the "Display Format" section of the computed field. Something like this may work for you:

$display = '$' . number_format($node_field_item['value'], 2);
oscarelloo’s picture

Thanks! That works great. One thing though, if I add the code above, the field will always print, even if it is empty. I would rather see that this does not happen. Any suggestions?

Moonshine’s picture

Sure.. you'll probably just want to check for a value.. something like:

  if ($node_field_item['value'] != '') $display = '$' . number_format($node_field_item['value'], 2);
  else $display = '';
oscarelloo’s picture

Thanks for your quick response!
I'm trying my best to get your code to work, but the field stills print.

oscarelloo’s picture

Nevermind - solved it. I only removed the ";" after the first line.

if ($node_field_item['value'] != '') $display = '$' . number_format($node_field_item['value'], 2)
  else $display = '';

I have never before coded in php (just some Pascal...), so I hope this solution is ok.

oscarelloo’s picture

Damn.

I works, the field is hidden when empty and prints when numbers are added. But I do get a syntax error for that unclosed line:

Parse error: syntax error, unexpected T_ELI

So I guess you always have to close the arguments. Any other ideas?

Moonshine’s picture

Well you definitely don't want to remove the semicolon. Really what I had there should work. What are you seeing displayed when there is no value?

oscarelloo’s picture

$ 0.00

is displayed if the field is left empty.

/ Oscar

Moonshine’s picture

ahh.. well that looks like the value isn't really "empty", rather it's set to 0. Try this instead:

if ($node_field_item['value'] == 0 ) $display = '';
else $display = '$' . number_format($node_field_item['value'], 2);
oscarelloo’s picture

YOU RULE! :)

Now it all works like a charm!

Thanks again for all your fast and precise help. I really appreciate it.

/ Oscar

Moonshine’s picture

Status: Active » Closed (fixed)
Witch’s picture

@ 9

The "$" sign is in this case a prefix. i tried to add a suffix but without any success. may you post an example where the "$" is a suffix?

Thank you!

Witch’s picture

Next time i just should try to open my eyes.

 if ($node_field_item['value'] == 0 ) $display = '';
else $display = number_format($node_field_item['value'], 2) . '€';
vicentefoxxx’s picture

Well I am trying to add the prefix but is not working i tried what is sugested in post 13# with prefix and is working

This is the code that is not working:

if (!empty($node_field[0]['value'])) { // the node is not new
return $node_field[0]['value'];
}
else { // the node is new
$count = db_result(db_query("SELECT COUNT(*) from {node} WHERE type = 'expeborrar'"));
$node_field[0]['value'] = '$' . $count + 1000;
}

And this one is working but only with prefix:

if (!empty($node_field[0]['value'])) { // the node is not new
return $node_field[0]['value'];
}
else { // the node is new
$count = db_result(db_query("SELECT COUNT(*) from {node} WHERE type = 'expeborrar'"));
$node_field[0]['value'] = $count + 1000 . '-2011';
}

Thanks!

vicentefoxxx’s picture

Status: Closed (fixed) » Active
braindrift’s picture

Hi vicentefoxxx,

you should add the prefix or the suffix in the "Display Format" textarea. In the "Computed Code" textarea you should specify the raw value that should be seved in the DB.

braindrift’s picture

Status: Active » Needs review
vicentefoxxx’s picture

Ahhhhhhhhhhhhhhhhhh great dendi, is working great I just write the code for the people like me that dont understand a lot of PHP Syntax thank you very much :)

Just copy this in "Display Format" textarea and change the prefix or sufix for the wanted data.
$display = 'prefix' . $node_field_item['value'] . 'sufix';

cj-a-min’s picture

Nice work everyone!

The above works great for not displaying the content, if there is no content to be displayed. And if there is content than it adds the prefix and suffix.

But, how about stopping the computed code from running unnecessary queries, if there is no data to begin with? For example.

The Computed Code:

$from = 'USD';
$to   = 'EUR';
$amt  = $node->field_sixpack_usd[0]['amount'];
$ret  = currency_api_convert($from, $to, $amt);
if ($ret['status'] == FALSE) {
drupal_set_message(t('An error occured: '). $ret['message']);
}
else {
$node_field[0]['value'] = $ret['value'] ;
}

Now we have to stop the query from happening in the "computed code" if there is no USD amount to begin with. So the computed code would look like this:

The New Computed Code, which needs some Luvn:

$amt  = $node->field_sixpack_usd[0]['amount'];
if ($amt == 0 ) {
stop everything and return a value of NULL
}
else {
$from = 'USD';
$to   = 'EUR';
$ret  = currency_api_convert($from, $to, $amt);
if ($ret['status'] == FALSE) {
drupal_set_message(t('An error occured: '). $ret['message']);
}
else {
$node_field[0]['value'] = $ret['value'] ;
}
}

You can keep the display code, as a second attempt at stopping from displaying the field, and of course to add prefix and suffix:

if ($node_field_item['value'] == 0 ) $display = '';
else $display = '€' . number_format($node_field_item['value'], 2) . ' EUR';

The code 'stop everything and return a value of NULL' is what I am looking for? Anyone know how to do this? Or maybe I'm on the wrong track all together :\

dqd’s picture

Issue summary: View changes
Status: Needs review » Closed (outdated)

Due to the Drupal core life cycle policy and security support advisery, Drupal 6 is no longer supported. So issues for Drupal 6 cannot be longer maintained. The project maintainer has asked for closing all D6 issues to clean up the issue queue. Feel free to reopen the issue if required or set it to "needs to be ported" and latest D8 dev version, if the issue discusses a still missing feature which can be implemented in the D8 version.