1

I am currently trying to make lots of things work together. I'll try to put it simple without annoying details.

I work on a lawyer's site with drupal commerce installed, as well as AddressBook, Webform and FillPDF.

Outside the commerce functionalities (selling legal templates), my client wants her users to share loads of necessary informations with her (about the case she needs to work on).
Among these information are… you guessed it: name and address.
These informations might be used (many times) in pdf generated from forms (like mandates etc.)

Do any of you know a way…
to pick an address already stored in AddressBook to re-use it in a WebForm, (let's say, via a token)?
---OR---
to fill The AddressBook values as well as user informations from data typed in a WebForm which I could grab later on (the other way round suits me fine).

The aim being to provide a unique place to register all the necessary infos, in order to keep the site super-simple for middle aged people.

0

I was able, lately, to use a list of existing profiles in a node field. As these profiles are entities, an 'entity reference' field can do the job. But I can't offer the possibility to choose from a list of entities in a webform though.
Now, I get a token related to this newly created field but not to the current user.

0

Her is what I came up with, if some of you are interrested. Mistake spotters welcome…

  <?php
  /**
   * Implements hook_token_info().
   * @ http://fredparke.com/blog/create-your-own-tokens-drupal-7
   */
  function address_token_token_info() {
    $info = array();
    // Define a new token type.
    $info['types']['my_custom_tokens'] = array(
    'name' => t('My custom tokens'),
    'description' => t('A token type for my custom tokens.'),
      );
  // Define any new tokens.
    $info['tokens']['my_custom_tokens']['user_name_line'] = array(
    'name' => t('Current user\'s full name'),
    'description' => t('A token I use to display the full name.'),
  );
  $info['tokens']['my_custom_tokens']['user_thoroughfare'] = array(
    'name' => t('Current user\'s thoroughfare'),
    'description' => t('A token I use to display the thoroughfare.'),
  );
  $info['tokens']['my_custom_tokens']['user_postal_code'] = array(
    'name' => t('Current user\'s postal code'),
    'description' => t('A token I use to display the postal code.'),
  );
  $info['tokens']['my_custom_tokens']['user_locality'] = array(
    'name' => t('Current user\'s postal code'),
    'description' => t('A token I use to display the locality.'),
  );
  $info['tokens']['my_custom_tokens']['user_country'] = array(
    'name' => t('Current user\'s postal code'),
    'description' => t('A token I use to display the country.'),
  );
  $info['tokens']['my_custom_tokens']['user_billing-address'] = array(
    'name' => t('Current user\'s billing address'),
    'description' => t('A token I use to display the whole address.'),
  );
  return $info;
}


/**
 * Implements hook_tokens().
 */
function address_token_tokens($type, $tokens, array $data = array(), array $options = array()) {

/* Variables to use in foreach within the fonction 
@ http://stackoverflow.com/questions/8506359/how-to-assign-foreach-loop-to-a-variable#8506477
Elements declared via AddressBook
@ http://drupal.stackexchange.com/questions/116453/how-to-get-default-drupal-commerce-billing-shipping-customer-profile-in-code#146244
@ https://www.drupal.org/node/1245956#comment-5593970
Nested arrays
@ http://www.w3schools.com/php/php_arrays_multi.asp
*/
global $user;
$default_pid=commerce_addressbook_get_default_profile_id($user->uid,'billing');
$profiles = commerce_customer_profile_load_multiple(array(), array('uid' => $user->uid));

$user_name_line = $profiles[$default_pid]->commerce_customer_address['und']['0']['name_line'];
$user_thoroughfare = $profiles[$default_pid]->commerce_customer_address['und']['0']['thoroughfare'];
$user_postal_code = $profiles[$default_pid]->commerce_customer_address['und']['0']['postal_code'];
$user_locality = $profiles[$default_pid]->commerce_customer_address['und']['0']['locality'];
$user_country = $profiles[$default_pid]->commerce_customer_address['und']['0']['country']; 

  $replacements = array();

  if ($type == 'my_custom_tokens') {
    // Loop through the available tokens.
    foreach ($tokens as $name => $original) {
      // Find our custom tokens by name.
      switch ($name) {
        case 'user_name_line':
                    // Work out the value of our token.
                    $value = $user_name_line;
          // Give our token it's value!
          $replacements[$original] = $value;
          break;
        case 'user_thoroughfare':
                    // Work out the value of our token.
                    $value = $user_thoroughfare;
          // Give our token it's value!
          $replacements[$original] = $value;
          break;
        case 'user_postal_code':
                    // Work out the value of our token.
                    $value = $user_postal_code;
          // Give our token it's value!
          $replacements[$original] = $value;
          break;
        case 'user_locality':
                    // Work out the value of our token.
                    $value = $user_locality;
          // Give our token it's value!
          $replacements[$original] = $value;
          break;
        case 'user_country':
                    // Work out the value of our token.
                    $value = $user_country;
          // Give our token it's value!
          $replacements[$original] = $value;
          break;
                case 'user_billing-address':
          // Work out the value of our token.
          $value = $user_thoroughfare.'<br />'.$user_postal_code.'<br />'.$user_locality.'<br />'.$user_country;
          // Give our token it's value!
          $replacements[$original] = $value;
          break;
      }
    }
  }

  return $replacements;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.