Block for currency exchange rates
| Project: | Currency Exchange |
| Version: | 5.x-1.3 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs work |
Great Module !!!
I tried to add this block to the CVS - but i don't know how ...
//################################################################################
function currency_block($op = 'list', $delta = 0, $edit = array())
{
if ($op == 'list') {
$blocks[0] = array('info' => t('Currency Converter'),
'weight' => 0, 'enabled' => 1, 'region' => 'left');
return $blocks;
}
else if ($op == 'configure' && $delta == 0) {
$form['currency_block_title'] = array(
'#type' => 'textfield',
'#title' => t('Block Title'),
'#default_value' => variable_get('currency_block_title', t('Currency Converter')),
);
return $form;
}
else if ($op == 'save' && $delta == 0) {
variable_set('currency_block_title', $edit['currency_block_title']);
}
else if ($op == 'view') {
switch($delta) {
case 0:
$block = array('subject' => variable_get('currency_block_title', t('Currency Converter')),
'content' => currency_contents());
break;
}
return $block;
}
//################################################################################

#1
http://drupal.org/node/134786
#2
Marking this as active, and the other one as duplicate, since this is the one that has code in it.
#3
Above block is inoperable without corresponding currency_contents() function.
#4
Fatal error: Call to undefined function currency_contents() in sites/all/modules/contributions/currency/currency.module on line 171Can anybody put some working block code?
Thanks.
#5
Does this enable displaying of currency within a block? Where do you put this code?
#6
Code is incomplete ...
#7
Any update on this issue...?
#8
Does this code work with 6.x? I wasn't able to get it working either.
#9
Wonder if there is a chance we can someone to re look at getting this patch to work with d6? Thanks.
#10
The patch never worked for D5 so unfortunately there's nothing to port.
That said it wouldn't be that difficult to create... someone just needs the inspiration to do so.
#11
Inspiration:
It's a pretty darn unique module right here. There isn't actually anything else like this for Drupal...
And it's almost feature complete... all it needs is a block display.
Once that's done, it would be pretty killer.
#12
Do you mean you want the currency converter form in a block?
#13
currency converter in a block would be fabulous, thanx!
#14
Yes indeed. If you or someone else could write a working patch it would be grand.
#15
As requested here's the currency exchange form in a block. This was written for and tested with Drupal 5, but conversion to Drupal 6 would be a breeze. A nice addition would be to eliminate the refresh and retrieve the conversion data with AJAX.
The form, validate and submit code is copied directly from currency.module. The rest of code outputs the form and related messaging in a block.
To use:
1) create a directory called currency_block inside the currency module directory
2) copy/paste each portion of code below into your favorite IDE or text editor and save as currency_block.info and currency_block.module respectively
3) enable the module, then the block
4) happy converting!
currency_block.info
name = Currency Blockdescription = This module provides currency exchange rates in a block.
dependencies = currency_api
package = Currency
currency_block.module
<?php
/**
* Implementation of hook_block.
*/
function currency_block_block($op, $delta = 0) {
switch ($op) {
case 'list':
$block[0]['info'] = t('Currency exchange block');
return $block;
case 'view':
switch($delta) {
case 0:
$block['subject'] = t('Currency exchange');
$block['content'] = drupal_get_form('currency_block_form');
break;
}
return $block;
}
}
/**
* Custom function for currency block form.
*/
function currency_block_form() {
$amount = $_SESSION['currency_amount'] ? $_SESSION['currency_amount'] : 1;
$from = $_SESSION['currency_from'] ? $_SESSION['currency_from'] : variable_get('currency_default_from', 'CAD');
$to = $_SESSION['currency_to'] ? $_SESSION['currency_to'] : variable_get('currency_default_to', 'USD');
$form['currency_amount'] = array(
'#type' => 'textfield',
'#title' => t('Amount'),
'#default_value' => $amount,
'#size' => 9,
'#maxlength' => 9,
'#description' => t('Amount to convert'),
);
$form['currency_from'] = array(
'#type' => 'select',
'#title' => t('From'),
'#default_value' => $from,
'#options' => currency_api_get_list(),
);
$form['currency_to'] = array(
'#type' => 'select',
'#title' => t('To'),
'#default_value' => $to,
'#options' => currency_api_get_list(),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Convert'),
);
$form['#validate']['currency_block_form_validate'] = array();
$form['#submit']['currency_block_form_submit'] = array();
return $form;
}
/**
* Custom theme function for currency_block_form().
*/
function theme_currency_block_form($form) {
$output = theme('status_messages', 'block');
$output .= drupal_render($form['currency_amount']);
$output .= drupal_render($form['currency_from']);
$output .= drupal_render($form['currency_to']);
$output .= drupal_render($form['submit']);
$output .= drupal_render($form);
return $output;
}
/**
* Custom handler validate function for currency_block_form().
*/
function currency_block_form_validate($form_id, $form_values) {
if (!$form_values['currency_amount']) {
form_set_error('', t('Amount is required.'));
}
if (!is_numeric($form_values['currency_amount'])) {
form_set_error('', t('Invalid Amount. Please enter a valid numeric amount.'));
}
}
/**
* Custom handler submit function for currency_block_form().
*/
function currency_block_form_submit($form_id, $form_values) {
$from = $form_values['currency_from'];
$to = $form_values['currency_to'];
$amount = $form_values['currency_amount'];
$url = 'http://finance.yahoo.com/q?s=' . $from . $to . '=X';
$ret = currency_api_convert($from, $to, $amount);
if ($ret['status'] == FALSE) {
drupal_set_message(t('currency exchange error: ') . $ret['message'], 'block');
}
else {
$result .= '<p>';
$result .= t('@amount @from = @value @to', array(
'@amount' => $amount,
'@from' => currency_api_get_desc($from),
'@value' => $ret['value'],
'@to' => currency_api_get_desc($to)));
$result .= '</p><p>';
$result .= l(t('Detailed history and chart'), $url);
$result .= '</p>';
}
// Save the last used values in the session
$_SESSION['currency_amount'] = $amount;
$_SESSION['currency_from'] = $from;
$_SESSION['currency_to'] = $to;
drupal_set_message($result, 'block');
}
#16
Bacteria Man
Is it possible to roll this as a patch, so it can be reviewed and committed to the module?
Thanks
#17
Are you suggesting to create a patch for the above separate module (to be included in the currency module distribution) or roll the code into currency.module?
I put this together almost as a lark and it should be tested before deciding to do either. The currency dropdowns are excessively wide due to one lengthy currency name, which may make the block column width too wide for ordinary themes.
#18
It is a useful feature to have, and making it part of standard module may be helpful.
Regarding the long currency name, perhaps you can truncate them to a reasonable length.
Any module that implements a block is by definition optional: the site admin decides whether to enable a block or not, so there is no harm in including it as is.
Post a screen shot here and let us see what everyone thinks: put it in the module, or keep it separate.
#19
Well, I like the idea of consolidating and rolling the block code into currency.module. It'd be nice to reuse currency_form_validate(), but the block submit function will have to remain separate because a custom message type is passed to drupal_set_message() at the end of currency_block_form_submit() so that the form errors and exchange rate reporting appear in the block instead of the content area. Sidebar: contrary to the drupal_set_message documentation you can define as many custom message types as you like. A corresponding class name just needs to be added to the theme stylesheet so that the format is properly outputted.
If you decide to keep things as a separate module you simply need to create the above files and check them into CVS.
#20
subscribing.
#21
modules page says this module does not work with drupal version 6.10.
any idea?
#22
How is your comment related to this issue (having a "block")?
If you want to say something unrelated, then open a new issue. Don't confuse others by posting in an existing unrelated issue.
And even what you are saying is false: the module has a 6.x-1.x release, and nothing says it does not run on 6.x.
#23
Hi,
I'm Trying to install this module in my Drupal Real Estate Site and it says:
This version is incompatible with the 6.13 version of Drupal core.
Any Ideas,
Can anyone point my in the right direction please?
#24
alerez:
are you sure that you downloaded 6.x version?
#25
Hi,
I have
• Currency 6.x-1.1
• Currency API 6.x-1.1
• And the currency block posted here by Bacteria Man.
Thanks for your help...
#26
alarez:
If you have latest version for 6.x, you should have in your currency.info line:
core = 6.x
This making it compatible with 6.x
#27
Hi,
I have the same problem of alarez
I'm using Drupal 6.11 and when i try to install the module i get this message:
This version is incompatible with the 6.11 version of Drupal core.and an "x" image instead of the checkbox. (please see attach)
Thanks in advance.
#28
As the post clearly states, the currency exchange form in a block module was created for Drupal 5. Besides adding "core = 6.x" to the currency_block.info the Form API related functions need to be converted to Drupal 6. This is a relatively trivial matter, but I personally don't have the bandwidth right now.
#29
Nope. Don't see it even when I did what was mentioned. To recap:
- I'm using version 5.3 of the currency exchange mod and Drupal 5
- I created the folder currency_block and put the files currency_block.info and currency_block.module inside
- I copy pasted your code into the 2 files .info and .module
- I copied the currency_block over to currency folder
- I disabled, uninstalled, currency module
- I uploaded new currency module, enabled it, ran update.php
After that what was supposed to happen? I don't see an entry for a new module in the modules page. I don't see a block named "currency exchange" either.
#30
Uh scratch that.
Seems like I got a problem with proper spelling after 20 years of education.
Please ignore last.
#31
I built a D6 module/block for converting currency based upon code posted above. I also created a plugin for the Ajax Forms module so that you can submit a number for conversion via Ajax. The result is then "jQueried" into a div.
If anyone wants the code let me know--I could post it here but have to run.
PP
#32
ppmax, I could do with that D6 block, that would be awesome!
#33
hey ppmax - bring it on ;)
#34
No problems--
I've uploaded two zip files. Here's a couple notes:
ppcustom_currency_block: this is the block module that depends on Currency Exchange. (The module has a dependency in it). This module could be modified to sit inside the Currency Exchange module--but I didnt go that route because I didnt want my stuff to get hosed if Currency Exchange was updated. If the maintainer wants to add this to the project it's cool with me ;)
ppcustom_currency_form: this is the plugin for the Ajax module and has to be installed in the /sites/all/modules/ajax/plugins directory. I cant remember if this module needed to be explicitly activated or if Ajax will just pick it up. Needless to say, the ajaxilicious currency exchange block needs this module to be ajaxtivated.
For my own sanity I usually name all my custom modules ppcustom_* and group them all in the zPP Modules "package." Once you've added these modules in their proper places go to admin/build/modules and scroll down to the bottom of the page to activate them.
UPDATED: I just checked and my Ajax plugin will show up in the Ajax group and needs to be explicitly activated. You'll also need the jQuery goodies too.
Last notes:
I haven't added any js validation so when you enter a zero or any non-numeric value in the Amount form field you'll get "undefined undefined" and an ugly drupal message above the form. If I have time to fix those annoyances I'll post an update.
If you "Inspect Element" with Firefox/Firebug you'll see a
<div id="results"></div>. You can add a selector to your stylesheet to style this content any way you like.If you find any issues/bugs post here and I'll see what I can do when I get time.
Hope this helps someone--
pp