uc_stock.tokens.inc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * @file
  4. * Token hooks for the uc_stock module.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function uc_stock_token_info() {
  10. $type = array(
  11. 'name' => t('Stock level'),
  12. 'description' => t('Tokens for the stock levels of products.'),
  13. 'needs-data' => 'uc_stock',
  14. );
  15. $tokens['level'] = array(
  16. 'name' => t('Level'),
  17. 'description' => t('The current stock level.'),
  18. );
  19. $tokens['model'] = array(
  20. 'name' => t('Model'),
  21. 'description' => t('The model or SKU of the stock level.'),
  22. );
  23. $tokens['threshold'] = array(
  24. 'name' => t('Threshold'),
  25. 'description' => t('The threshold or warning limit of the stock level.'),
  26. );
  27. return array(
  28. 'types' => array('uc_stock' => $type),
  29. 'tokens' => array('uc_stock' => $tokens),
  30. );
  31. }
  32. /**
  33. * Implements hook_tokens().
  34. */
  35. function uc_stock_tokens($type, $tokens, $data = array(), $options = array()) {
  36. $sanitize = !empty($options['sanitize']);
  37. $replacements = array();
  38. if ($type == 'uc_stock' && !empty($data['uc_stock'])) {
  39. $object = $data['uc_stock'];
  40. foreach ($tokens as $name => $original) {
  41. switch ($name) {
  42. case 'level':
  43. $replacements[$original] = $object->stock;
  44. break;
  45. case 'model':
  46. $replacements[$original] = $sanitize ? check_plain($object->sku) : $object->sku;
  47. break;
  48. case 'threshold':
  49. $replacements[$original] = $object->threshold;
  50. break;
  51. }
  52. }
  53. }
  54. return $replacements;
  55. }