It would be nice to set the currency automatically from currency fields on the node which are configured to inherit from.

Would this feature be accepted (in principal)? I may be able to work on it this week.

Comments

Bevan’s picture

  • Configuration; There would be two new form elements in the field-instance configuration for Money fields;
    1. Options for;
      • Do not inherit currency
      • Inherit currency
      • Force currency inheritance (disable the currency select menu)
    2. Select menu to choose the Currency field from which to inherit the currency.
  • On node-forms, if there is a Money field that is configured to inherit, add jQuery which:
    • if force option is on, disables child currency <select>s
    • binds to change event on parent field, to
      • update child currency <selects>
    • triggers the change event to initialize the child form elements
  • On node-form validation:
    • check parent field currency == child field currency
Bevan’s picture

Status: Needs work » Needs review
StatusFileSize
new2.09 KB

Here is an initial stab at this.

It implements the jQuery part of it. The javascript expects Drupal.settings.moneyInheritCurrency to be an object where the property names are the parent-field's names and their values are arrays of child-field's names. This example module-implementation would go in a form_alter function or similar for a node-form;

      // Build an array of Money fields that inherit from a currency field.
      $settings = array();
      foreach ($form as $id => $element) {
        // Ignore anything that is not a CCK field element.
        // @todo What about fields that are in fieldsets?
        $str = 'field_';
        if (strncmp($id, $str, strlen($str)) === 0) {
          // Iterate over each setting and widget in the form element;
          foreach ($element as $key => $item) {
            // Ignore Form API settings & widgets that are not Money fields.
            if (is_numeric($key) && 'money_widget' == $item['#type']) {
              // Store the relationship between Currency & Money fields.
              // @todo Check for settings stored in the field configuration.
              $settings['field_hct_country_currency'][] = $id;
            }
          }
        }
      }

      // Add the javascript settings and script to the page.
      drupal_add_js(array('moneyInheritCurrency' => $settings), 'setting');
      drupal_add_js(drupal_get_path('module', 'money') . '/money.js');
Bevan’s picture

StatusFileSize
new2.13 KB

Adds a trigger('change') so that other jQuery can respond to changes to currency
elements.

Bevan’s picture

StatusFileSize
new3.23 KB

Refactors to be comply with jslint and only trigger events if a value was set.

Bevan’s picture

StatusFileSize
new3.03 KB

On review, it's a bad idea to initialize everything on page load.