0
votes

I'm wondering if anyone can assist me in updating the code detailed here (http://oif.eafarris.com/blog/pre-fill-cck-node-fields-based-on-a-node-re...) for Drupal 7. The function described in that post is identical to what I'm looking to do on my Drupal 7 site but I'm not well versed enough programmatically to do it myself.

I have a content type Event. On the node creation form for Event, I have an autocomplete field for "Client". Below that are additional fields for name, address, etc. The end result I'm hoping to achieve here is:

  1. User enters client name in the autocomplete Client field.

  2. Entered client name matches an existing client and is selected.

  3. Using the node ID of the selected client, the address fields are then populated automatically.

I have a JSON view with a nid argument which spits out the required fields at the url http://domain.com/json-clients/[nid]. But I am unable to get that info returned to the correct fields on the form.

Below is the code as I've got it modified trying to get it to work with D7. Anyone see the glaring errors and care to assist?

(function ($) {
    Drupal.behaviors.sponsorhelper = function () {
        $("input[name='field_client[und][0][nid]']").blur(function() {

            nidRegEx = /\[nid:(\d+)\]/;

            SponsorHelper.fill($(this).attr('value').match(nidRegEx)[1]);

        })
    };

    SponsorHelper.fill = function(nid) {
        var url = Drupal.settings.basePath + 'json-clients/' + nid;

        jQuery.getJSON(url, function (data, result) {
            if (result != 'success') {
                return;
            }

            $("input[name='field_address_1[und][0][value]']")
                .attr('value',data.nodes[0].node.field_address_1_value);

            $("input[name='field_address_2[und][0][value]']")
                .attr('value',data.nodes[0].node.field_address_2_value);
        })
    };
})(jQuery);

Any help is greatly appreciated.

Thanks.

1

1 Answers

0
votes

Instead of writing your own javascript try handling this with a couple of drupal's community modules. Check out: http://drupal.org/project/conditional_fields http://drupal.org/project/computed_field/

You can us conditional fields to hide the address until the client info is put in. Then use computed fields to search for the client and auto fill the address fields.