You are here

function mobile_codes_mobile_codes_tokens_alter_alter in Mobile Codes 7.2

Implements hook_mobile_codes_tokens_alter_alter().

File

includes/mobile_codes.inc, line 97
Mobile Codes module integration.

Code

function mobile_codes_mobile_codes_tokens_alter_alter(&$replacements, $context) {
  $presets = mobile_codes_presets_load_all();

  // Mobile Codes tokens.
  if ($context['type'] == 'mobile-codes' && !empty($context['data']['mobile-codes-data'])) {

    // [image] dynamic tokens.
    if ($image_tokens = token_find_with_prefix($context['tokens'], 'image')) {
      foreach ($image_tokens as $preset => $token) {
        if (isset($presets[$preset])) {
          $replacements[$token] = theme('mobilecode', array(
            'data' => $context['data']['mobile-codes-data'],
            'attributes' => array(
              '#preset' => $preset,
            ),
          ));
        }
      }
    }

    // [path] dynamic tokens.
    if ($path_tokens = token_find_with_prefix($context['tokens'], 'path')) {
      foreach ($path_tokens as $preset => $token) {
        if (isset($presets[$preset])) {
          $attributes = array(
            '#preset' => $preset,
          );
          $request = mobile_codes_process_url($context['data']['mobile-codes-data'], $attributes);
          $replacements[$token] = mobile_codes_generate($request);
        }
      }
    }

    // [url] dynamic tokens.
    if ($url_tokens = token_find_with_prefix($context['tokens'], 'url')) {
      foreach ($url_tokens as $preset => $token) {
        if (isset($presets[$preset])) {
          $attributes = array(
            '#preset' => $preset,
          );
          $request = mobile_codes_process_url($context['data']['mobile-codes-data'], $attributes);
          $replacements[$token] = file_create_url(mobile_codes_generate($request));
        }
      }
    }
  }

  // URL tokens.
  if ($context['type'] == 'url' && !empty($context['data']['path'])) {
    $url = url($context['data']['path'], array(
      'absolute' => TRUE,
    ));
    foreach ($context['tokens'] as $name => $original) {
      switch ($name) {
        case 'mobile-codes':
          $replacements[$original] = theme('mobilecode', array(
            'data' => $url,
            'attributes' => array(
              '#preset' => key($presets),
            ),
          ));
          break;
      }
    }
    if ($mobile_codes_tokens = token_find_with_prefix($context['tokens'], 'mobile-codes')) {
      $replacements += token_generate('mobile-codes', $mobile_codes_tokens, array(
        'mobile-codes-data' => $url,
      ));
    }
  }
}