1

new to Drupal / PHP.

What I'd like to do is get the value of a couple fields from a node. Then create a custom token from them so I can use them in the meta tag module. Or just get the value of a node token that's already been created.

I'm not really sure where to start, does this need to happen in the node template or a module?

At the moment I am using the following code to create a module that creates a custom token.

<?php


/**
 * Implements hook_token_info().
 */
function globalpost_custom_token_info() {
  $info = array();
  // Define a new token type.
  $info['types']['gp_tokens'] = array(
    'name' => t('GlobalPost Custom Tokens'),
    'description' => t('A token type for GlobalPost tokens.'),
  );
  // Define any new tokens.
  $info['tokens']['gp_tokens']['gp_keywords'] = array(
    'name' => t('GlobalPost Keywords'),
    'description' => t('A list of formated keywords for metatags'),
  );
  return $info;
}

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

  if ($type == 'gp_tokens') {
    // Loop through the available tokens.
    foreach ($tokens as $name => $original) {
      // Find our custom tokens by name.
      switch ($name) {
        case 'gp_keywords':
          // Work out the value of our token.
          $value = "CAN I PUT ANOTHER NODE TOKEN HERE OR DATA FROM NODE's FIELD?" ;
          // Give our token it's value!
          $replacements[$original] = $value;
          break;
      }
    }
  }

  return $replacements;
}



?>
2
  • 1
    Welcome to Drupal Answers! This might help you: see Examples module, specifically the token_example. Jan 18, 2015 at 10:24
  • Thanks, I saw that the example module for token does a check on the body using field_get_items(); I tried to grab my subhead field using the same function, then pass it to the token: $output= field_get_items('node', $node, 'field_subhead'); $replacements[$original] = $output; But I just get an empty value. Jan 18, 2015 at 17:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.