Hi create my own node type "Address". In the node edit form I add:

  if (!empty($node_address)) {
    // Example 1: create an edit form for address ID 2 from user 1.
    $address = UcAddressesAddressBook::get($user->uid)->getAddressById($node_address->aid);
    $form['address'] = array(
      '#type' => 'uc_addresses_address',
      '#uc_addresses_address' => $address,
    );
    $form['address_serialize'] = array(
      '#type' => 'value',
      '#value' => serialize($address)
    )
  }
  else {
    // Example 2: create an edit form for a new address for user 1.
    $address = UcAddressesAddressBook::get($user->uid)->addAddress();
    $form['address'] = array(
      '#type' => 'uc_addresses_address',
      '#uc_addresses_address' => $address
    );
    $form['address_serialize'] = array(
      '#type' => 'value',
      '#value' => serialize($address)
    )

Now I get address form to node edit form. But when I submit the form in the hook_insert I get only array of fields of the address form:

$node->address is only array of fields of the address form, so I can not use $node->address->save(); because it is not object of UcAddressesAddress type!!!

So I add the variable 'address_serialize'. Now in the hook_insert I get instance of UcAddressesAddress:

$address = unserialize($node->address_serialize);
if ($address instanceof UcAddressesAddress) {
  $address->save();
}

But in the uc_address table new address is not save!!!

Can you help me please.

Thanks Marinex

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

marinex’s picture

Issue summary: View changes

small correct

MegaChriz’s picture

I have never tried to attach an UcAddressesAddress to a node type, so it can be that it doesn't work nicely. Normally, I pick up the address in the form submit function, like this for example (function uc_addresses_get_address_form_submit() in uc_addresses.pages.inc):

$address = $form['uc_addresses']['address']['#uc_addresses_address'];

What you could try is to look how an address is handled when an user registers. I think that comes the closest to your situation. See uc_addresses_user() in uc_addresses.module. Anyway, I think you shouldn't serialize the address in the form definition. The Drupal Form API should take care for serializing the address (when the form is cached) or just recreate the address.

I'm glad to hear you are experimenting with extending Ubercart Addresses. Let me know if you are able to get this fixed (and how).

marinex’s picture

So I have simple solution - I add own submit handler and there I call address instance:

/*
 * Implementation of hook_form_alter()
 */
function uc_addresses_node_form_alter(&$form, &$form_state, $form_id){
  // target a single form
  if($form_id == "uc_addresses_node_node_form"){
    //dsm($form);
    // Add my extra submition & validation functions
    $form['#submit'][] = 'uc_addresses_node_node_form_submit_handler';
  }
}
/*
 * Implementation custom handler
 */
function uc_addresses_node_node_form_submit_handler($form, &$form_state) {
  $address = $form['address']['#uc_addresses_address'];
  $address->save();
  $form_state['values']['address']['aid'] = $address->getId();
}
/*
 * Implementation of hook_insert()
 */
function uc_addresses_node_insert($node) {
  if (!empty($node->address['aid'])) {
    $node->aid = $node->address['aid'];
    drupal_write_record('uc_addresses', $node, 'aid');
  }
}



Everything works great, thanks for your effort (great module).
M.

MegaChriz’s picture

Status: Active » Fixed

Thanks for reporting back. I only had the feeling that one of the last lines isn't quite right:

drupal_write_record('uc_addresses', $node, 'aid');

Are the address values right in $node? The previous code lines imply that the values are in $node->address. And one other note: by using drupal_write_record() the uc_addresses hooks won't be invoked (like hook_uc_addresses_address_update()).

There was also an other method I forgot to mention. You can also load an address and populate it with new values using the setMultipleFields() method:

$address = UcAddressesAddressBook::get($user->uid)->getAddressById($aid);
$address->setMultipleFields($values);
marinex’s picture

FileSize
9.34 KB

Yes, address values are right in $node (hook_load), but I need only one value 'aid', because I insert address form to node edit form with:

  // This is in the uc_addresses_node_form, this create node edit form with address form fields!
  if (!empty($node->aid)) {
    // Example 1: create an edit form for address ID 2 from user 1.
    $address = UcAddressesAddressBook::get($user->uid)->getAddressById($node->aid);
    $form['address'] = array(
      '#type' => 'uc_addresses_address',
      '#uc_addresses_address' => $address,
    );
  }
  else {
    // Example 2: create an edit form for a new address for user 1.
    $address = UcAddressesAddressBook::get($user->uid)->addAddress();
    $form['address'] = array(
      '#type' => 'uc_addresses_address',
      '#uc_addresses_address' => $address
    );
  }

in the hook_load() I added address values to node(I need only one value 'aid'):
return db_fetch_object(db_query('SELECT * FROM {uc_addresses} WHERE nid = %d', $node->nid));


When I create a new address:
First, in the custom handler I save new address without NID and VID, because at this time node is not created.
Second, after that in the hook insert I only update this new address with NID and VID. I use uc_addresses table.


Insted of the:

drupal_write_record('uc_addresses', $node, 'aid');

it can be used db query like this:

$result = db_query("UPDATE {uc_addresses} SET vid = %d, nid = %d WHERE aid = %d", array($node->vid, $node->nid, $node->aid));

BUT it is has same result as drupal_write_record.
I think the hook_uc_addresses_address_update() and others should be work normal way.
I attach my dev module :-) this is early dev. version.

MegaChriz’s picture

I agree with you that using drupal_write_record() is better than using db_query(). I mentioned it because if you write to the uc_addresses table directly, other modules will not get a chance to respond to that event. I don't mean writing to the table directly is wrong, I was just telling what the consequences are. Anyway, using the address book API is the preferable way. I'm not sure if you registered the vid and nid fields via the hook hook_uc_addresses_fields() (it's not in your attached module file), but if you did, you could just do this:

$address->setField('nid', $node->nid);
$address->setField('vid', $node->vid);
$address->save();
I attach my dev module :-) this is early dev. version.

Interesting, thanks for sharing! It could become a solution for issue #711764: uc_addresses CCK Field as well! :-)

//zakomentoval jsem to protože, tento handler je již načtený přes modul uc_addresses!

Nice comment :). I can't not make much of it. I only know a few Czech words, for example: "podmasli".

marinex’s picture

I have registred vid and nid fields with:

/**
 * Implements hook_uc_addresses_fields().
 *
 * Registers all extra address fields for Ubercart Addresses.
 *
 * @return array
 */
function uc_addresses_node_uc_addresses_fields() {
  module_load_include('fields.inc', 'uc_addresses_node');
  return _uc_addresses_node_schema_fields();
}

This is in dev module. I attach my fields. I thought that is right way. Anything wrong? If yes could you help me?

MegaChriz’s picture

I have not executed the code, but it looks right. So you could also use the setField() method for the nid and vid fields. And if it doesn't work, you'll get an UcAddressesInvalidFieldException (this exception is thrown if you try to set a field where Ubercart Addresses doesn't know about).

Good luck with the further development. :-)

marinex’s picture

Thanks, At the moment I am doing integration views. Once this module will be finished, so I upload it here and you can add it as submodule ;-)

M.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

marinex’s picture

Title: New module uc_addresses_node here! » I cannot save new address - save() is not member function
FileSize
47.78 KB

@MegaChriz
So I am here :-) as I promised I attach my module.
My modul work, there is implemet Vertical tab, views2/3 (there are default view), CCK

I think the the Views should be used as address book (I call it Glossary), I suggest rename "Address list" in the original module (UC Address) to "Default addresses" and remove every addresses (one template) from there and put there only default addresses for Ubercart (Billing and Shipping). Sorry for my English.

TO DO:
1) in my module I re-use your tokens from uc_addresses but system return "Duplicate tokens" in the status report page! But I think it work.
look at row 509 uc_addresses_node_token_list

2) I tried also to implement Feed API but I am not familiar with. In my module there are some code... but need to do!
3) Integrate Invite module or RSVP modules

marinex’s picture

Title: I cannot save new address - save() is not member function » New module uc_addresses_node here!
Category: support » task
Status: Closed (fixed) » Active

I rename this issue :-)

MegaChriz’s picture

Title: I cannot save new address - save() is not member function » New module uc_addresses_node here!
Status: Active » Needs review

Thanks for your work! :-) I see you put a lot of time in it. I hope to find time to review your work, but it may take a while before I will get to it (December is usually a very busy month). At first glance I see a few minor coding standards issues (tabs instead of spaces in uc_addresses_node.fields.inc) and a few code comments are in Czech (for example Dodělat verzování i pro adresy pak to bude mít smysl! on line 407 in uc_addresses_node.module). I saw also a syntax error on line 288 in FeedsUcAddressesNodeProcessor.inc:
'description' => t('The nickname of this address, like "Home", "Office", "Anna's apartment."'),
The single quote in "Anna's" is not escaped.

I understand that the module is not in a finished state yet (you pointed out some Todo's in #10), so the few minor issues I saw at first glance won't stop me from reviewing it (when I have found the time to do that).

I think the the Views should be used as address book

I already made plans to implement this, see #1433034: Redesign the address book page using Views (first for the Drupal 7 version, then for the Drupal 6 version). Or are you pointing to the Views integration code in your uc_addresses_node module?

About your suggestion in #8 to add this a submodule: I'm not sure yet, maybe it would fit better in it's own project. Because the more code, the more time it will cost to maintain the module (more cases to test, potentially more bugs). Also, it's my intention that the 7.x-1.x version can at least do what the 6.x-2.x version can do, so that would mean the module should be ported to Drupal 7 first before it can be added as a submodule.
Well, I should first try out the module and see how I think about it after that. ;-)

marinex’s picture

The Views in uc_addresses_node is tied with this module and is specifically intended to work with this module; and is compatible with uc_views_addresses. I think this integration is light solution but effective. Unlike uc_views_addresses I also integrated uc_zones and uc_countries tables.

marinex’s picture

Issue summary: View changes

small correct