Userpoints API changes in 7.x-1.x

Last updated on
30 April 2025

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

hook_userpoints_info()

This hook allows to define the operations am module uses and allows to provide translatable, dynamic descriptions for these.

Example implementations:

Static description:

/**
 * Implements hook_userpoints_info().
 */
function userpoints_userpoints_info() {
  return array(
    'expiry' => array(
      'description' => t('!Points have expired.', userpoints_translation()),
    )
  );
}

Dynamic description with a callback function:

function userpoints_nc_userpoints_info() {
  return array(
    'userpoints_nc_node_insert' => array(
      'description callback' => 'userpoints_nc_description_callback',
      'description' => t('Content was created.'),
    ),
    'userpoints_nc_node_publish' => array(
      'description callback' => 'userpoints_nc_description_callback',
    ),
  );
}

The callback function recieves the $operation object and the referenced $entity (if any) to provide a dynamic description.

Example (shortened):

function userpoints_nc_description_callback($operation, $entity) {
  $arguments = array();
  // Try to load content type name.
  if ($operation->entity_type == 'comment' && $entity) {
    $node = node_load($entity->nid);
    $arguments['%title'] = $node->title;
  }
  elseif ($operation->entity_type == 'node' && $entity) {
    $arguments['%title'] = $entity->title;
  }

  switch ($operation->operation) {
    case 'userpoints_nc_node_insert':
      return t('Added %title.', $arguments);
      break;
  }
}

More features based on this hook are planned, for example a flood control module that allows to define which operation can be executed how often.

Use of description in userpoints_userpoints_api() is deprecated and operation is a technical name

Instead, hook_userpoints_info() should be implemented to provide a description that can be translated. Additionally, operation is now a technical string that is not displayed to normal users anymore. Instead displaying operation and description, only a single column called "Reason" is shown which displays the description, if available or the operation if not.

Example in Drupal 6:

$params = array(
  'points' => 5,
  'operation' => t('Some operation'),
  'description' => t('Description for the operation.'),
);
userpoints_userpoints_api($params);

Example in Drupal 7:

/**
 * Implements hook_userpoints_info().
 */
function userpoints_userpoints_info() {
  return array(
    'mymodule_some_operation' => array(
      'description' => t('Description for the operation.'),
    )
  );
}

$params = array(
  'uid' => $user->uid,
  'points' => 5,
  'operation' => 'mymodule_some_operation',
);
userpoints_userpointsapi($params);

Help improve this page

Page status: Not set

You can: