0

I am using the Countries module to assign countries and associated data to nodes. I am currently writing a custom token and only can access the ISO2 data for the value, not the associated data (full name, language, ISO3).

How can programmatically get the other data with only the ISO2?

1

Continuing the answer from the initial question, I think you have to load the Country entity to replace the token with what values you are after.

Based on looking at the Countries module, it stores countries as a custom entity:

/**
 * Implements hook_entity_info().
 */
function countries_entity_info() {
  $return = array(
    'country' => array(
      'label' => t('Country'),
      'plural label' => t('Countries'),
      'entity class' => 'Entity',
      'controller class' => 'EntityAPIControllerExportable',
      'base table' => 'countries_country',
      'token type' => 'country',
      'fieldable' => TRUE,
      'module' => 'countries',
      'entity keys' => array(
        'id' => 'cid',
        'name' => 'iso2',
        'label' => 'name',
      ),
      'bundles' => array(
        'country' => array(
          'label' => t('Country'),
          'admin' => array(
            'path' => 'admin/config/regional/countries',
            'access arguments' => array('administer site configuration'),
          ),
        ),
      ),
      'view modes' => array(
        'full' => array(
          'label' => t('Country'),
          'custom settings' => FALSE,
        ),
      ),
    ),
  );
  return $return;
}

/**
 * Loads a country entity by ISO alpha-2 code.
 *
 * This is also a menu wildcard loader.
 *
 * @param string $iso2
 *   An ISO alpha-2 string. This is case-insensitive.
 *
 * @return object
 *   A country object loaded by the entity controller.
 */
function country_load($iso2) {
  // The URL's should be case insensitive.
  $iso2 = $iso2 ? drupal_strtoupper($iso2) : $iso2;
  return entity_load_single('country', $iso2);
}

It looks like the iso2 value is the entity id, based on this code and the helper load function here, so, you can access the full entity when a token is replaced by:

/**
 * Implements hook_tokens().
 */
function MYMODULE_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = [];

  if ($type == 'node' && !empty($data['node']) && array_key_exists('MYTOKEN', $tokens)) {
    $country = country_load($data['node']->field_prgm_country[LANGUAGE_NONE][0]['iso2']);
    // $country should be a fully loaded Country entity with all its fields and properties
    // check out $country here with ksm($country);exit;
    $replacements['[node:MYTOKEN]'] = // data you want to show;
  }

  return $replacements;
}

It looks like the Country module provides its own country: scope of tokens, but I do not know how (or if) they work in the context of fields on a node. This should get you closer at least.

  • Yes, the country provides some tokens but not working exactly as needed. – Sam Jan 5 at 15:56
  • I don't have any project or code setup in front of me, so I can't go too much deeper. But looking at the token_example module of the Drupal Examples module: cgit.drupalcode.org/examples/tree/token_example/… - once you get the country entity, I think you can work your field with token_replace('STRING TO AFFECT', ['country' => $country]). I don't know enough about your project to know what or where, though. – Kevin Jan 5 at 16:03
  • Thanks kevin. I'm getting close. I've got the continent code printing now but now I need to use a function that is defined elsewhere in a template but is is erroring out since I'm guessing it loads after this. – Sam Jan 5 at 16:09
  • What is the error? – Kevin Jan 5 at 16:13
  • Function not defined yet. – Sam Jan 5 at 16:13

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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