You are here

DEVELOPER.txt in Activity 6.2

 /\  __|_.  ._|_
/~~\(_ | |\/| |\/
               /
Development specifications and integration
==========================================
 *for more details please see the handbook at http://drupal.org/node/328429

_Hooks:
------------------------------------------
hook_activity_info()
  Register a module to work with Activity's hook_action_info() implementation. All properties
  marked with * are required

  Properties:
    api*         - specifies the version of Activity you are registering with
    name*        - the name of the module
    object_type  - the object type that is used in token replacement.
                   @see activity_record(). This is needed because the $object
                   parameter is not available, so we need a key name in order to
                   reference the object from within the $context parameter.
    eid_field    - the field of the object_type that identifies this activity.
                   For instance, comment module uses this as 'cid' so that all
                   activities dealing with that comment can be retrieved or deleted.
    objects      - corresponds to the objects that are passed as properties of
                   the $context array within a module's hook_trigger_name(). @see
                   the example in flag_friend.module - compare it's
                   hook_trigger_name() and hook_activity_info(). The key/s to
                   this array are used as the labels fo rthe form elements.
    hooks        - a list of available hook that this action should operate upon.
                   @see activity_action_info(). This allows other modules to
                   extend the scope of what can be recorded by the activity
                   action.
    type_options - an array keyed (value => english name) for types. For instance,
                   node_activity_info() returns something like
                   array('page' => 'Page'). These types are to be used in conjuction
                   with hook_activity_type_check().
    realms       - a structured array, keyed by realm_id that points to a human 
                   readable name (note don't use t()) 
                   ex: realm_id => 'My Human Readable name'

  Return value:
    An object with the above properties.

  Example:
    /**
     * Implementation of hook_activity_info().
     */
    function flag_friend_activity_info() {
      $info = new stdClass();
      $info->api = 2;
      $info->name = 'flag_friend';
      $info->object_type = 'flag_friend';
      $info->objects = array('requestor' => 'user', 'requestee' => 'flag_friend'); // array keys are the labels
      $info->hooks = array('flag_friend' => array('approve', 'request', 'deny', 'remove'));
      $info->realms = array('flag_friend' => 'Flag Friend');
      $info->type_options = array();
      return $info;
    }
------------------------------------------
hook_activity_grants($activity)
  Provides a means to record what should have access to any particular message.

  Parameters:
    $activity - an object that holds a full activity record from the database.

  Return value:
    A list of keyed by reaml of ids to be stored into the access_table with the above properties.
  
  Example:
    For instance, flag_friend returns a one element array of the creator of the 
    activity. For OG, it would return all the groups that the node belongs in 
    for instance.

    /**
     * Implementation of hook_activity_grants().
     */
    function flag_friend_activity_grants($activity) {
      return array(
        'flag_friend' => array($activity->uid), // the module_id that will be used
      );
    }
------------------------------------------
hook_activity_access_grants($account)
  Provide a means for other modules to determine who can have access to any
  given activity message.

  Parameters:
    $account - the account of the message that we're determining access for.

  Return value:
    The an array keyed by realm of Ids for the module that the user will have access too.

  Example:
    /**
     * Implementation of hook_activity_access_grants().
     */
    function flag_friend_activity_access_grants($acccount) {
      $friends = flag_friend_get_friends($account->uid);
      $realm_ids = array();
      if (!empty($friends)) {
        foreach ($friends as $friend) {
          $realm_ids['flag_friend'][] = $friend;
        }
      }
      return $realm_ids;
    }
------------------------------------------
hook_activity_record_alter(&$record, $context)
  Provide a means to alter an activity record before it is inserted into the db.

  Parameters:
    $record  - the record for insertion, containing uid, op, type,
               author_message, everyone_message, nid, and created.
    $context - the context from the trigger, containing hook, op, object,
               author-pattern, and everyone-pattern.

  Return value:
    $record is passed by reference in order to make changes to it before insert

  Example:
    /**
     * Implementation of hook_activity_records_alter().
     */
    function example_activity_records_alter(&$record, $context) {
      // If we have a story node rather than another type, we can change the
      // token pattern.
      if ($object->type == 'story') {
        $author_pattern = $context['author-pattern'] .' - [node-type]';
        $record->author_message = token_replace($author_pattern, $record->type, $context[$record->type]);
      }
    }
------------------------------------------
hook_activity_messages_alter(&$messages, $type)
  Provides a means to alter the activity messages before they are inserted.

  Parameters:
    $messages - the translated messages keyed by uid which have already had
                their tokens replaced.
    $type     - the type of activity message as defined by the implementing
                module.

  Example:
    /**
     * Implementation of hook_activity_messages_alter().
     */
    function example_activity_messages_alter(&$messages, $type) {
      if ($type == 'nodeapi') {
        // You should probably never do this, but it illustrates the $message
        // structure. Do no record Anonymous messages.
        unset($messages[0]);
        // Most use cases will actually be string replacement methods on the
        // $messages[$uid] so that you can target a specific message that will
        // show up for a particular user.
      }
    }
------------------------------------------
hook_activity_message_recorded($record, $context)
  Provides a means to do something with a record after it has been saved.

  Parameters:
    These are the same as hook_activity_record_alter() except that since the
    $record has been saved, it now has a $record->amid.

  Example:
    /**
     * Implementation of hook_activity_message_recorded().
     */
    function activity_user_status_activity_message_recorded($record) {
      // After a message has been recorded with activity, we then save it's id so
      // that we can reference it as a foreign key.
      if ($record->type == 'activity_user_status') {
        db_query("UPDATE {activity_user_status} SET amid = %d WHERE uid = %d", $record->amid, $record->uid);
      }
    }
------------------------------------------
hook_activity_access_records_alter(&$grants)
  Provides a mean to alter the grants that are recorded to the activity_access
  table.

  Parameters:
    $grants  - these are the grants that come back from other modules whom have
               implemented hook_activity_grants().
    $record  - This is the activity record in the database

  Example:
    /**
     * Implementation of hook_activity_access_records_alter().
     * This example removes any access records except og. Prevents friend
     * modules from providing access.
     */
    function example_activity_access_records_alter(&$grants, $record) {
      foreach ($grants as $realm => $value) {
         if ($realm != 'og') {
             unset($grants[$module]);
         }
      }
    }
-------------------------------------------
hook_activity_type_check($token_objects, $types)
  This hook is called when a module implements hook_activity_info() and provides configurable
  types. These types need to be checked against the objects loaded up for this activity.
  
  Parameters
    $token_objects  - This array of objects are objects that will be used during token_replace. 
                      These objects generally match those defined in $info->objects

    $types          - These are the types selected from the options provided in $info->types. 
                      For instance, when the activity template is created, the users chooses 
                      for node insert activity, type of page and story. Then the $types array
                      will contain two elements, array('page', 'story').

Example:
/**
 * Implementation of hook_activity_type_check().
 */
function node_activity_type_check($token_objects, $types) {
  return (in_array($token_objects['node']->type, $types));
}
---------------------------------------------
hook_activity_objects_alter(&token_$objects, $activity_type)
  This hook is called for each activity record. It gives modules the
	oppurtunity to change the objects based on activity_type which
	corresponds to the type column in the activity table.

  Parameters
    $token_objects - This arrya of objects will be used during
    token_replace.

   $activity_type  - The type of activity being recorded. i.e. 'node',
   'comment', 'user'

Example:
/**
 * Implementation of hook_activity_objects_alter().
 */
function comment_activity_objects_alter(&$token_objects, $activity_type) {
  if ($type == 'comment') {
    $objects['node'] = node_load($objects['comment']->nid);
    $objects['user'] = $GLOBALS['user'];

    // If the comment and the node are the same, provide an object for it.
    if (isset($objects['comment']) && $objects['node']->uid == $objects['comment']->uid) {
      $objects['node_comment_author'] = $objects['comment'];
    }
  }
}

File

DEVELOPER.txt
View source
  1. /\ __|_. ._|_
  2. /~~\(_ | |\/| |\/
  3. /
  4. Development specifications and integration
  5. ==========================================
  6. *for more details please see the handbook at http://drupal.org/node/328429
  7. _Hooks:
  8. ------------------------------------------
  9. hook_activity_info()
  10. Register a module to work with Activity's hook_action_info() implementation. All properties
  11. marked with * are required
  12. Properties:
  13. api* - specifies the version of Activity you are registering with
  14. name* - the name of the module
  15. object_type - the object type that is used in token replacement.
  16. @see activity_record(). This is needed because the $object
  17. parameter is not available, so we need a key name in order to
  18. reference the object from within the $context parameter.
  19. eid_field - the field of the object_type that identifies this activity.
  20. For instance, comment module uses this as 'cid' so that all
  21. activities dealing with that comment can be retrieved or deleted.
  22. objects - corresponds to the objects that are passed as properties of
  23. the $context array within a module's hook_trigger_name(). @see
  24. the example in flag_friend.module - compare it's
  25. hook_trigger_name() and hook_activity_info(). The key/s to
  26. this array are used as the labels fo rthe form elements.
  27. hooks - a list of available hook that this action should operate upon.
  28. @see activity_action_info(). This allows other modules to
  29. extend the scope of what can be recorded by the activity
  30. action.
  31. type_options - an array keyed (value => english name) for types. For instance,
  32. node_activity_info() returns something like
  33. array('page' => 'Page'). These types are to be used in conjuction
  34. with hook_activity_type_check().
  35. realms - a structured array, keyed by realm_id that points to a human
  36. readable name (note don't use t())
  37. ex: realm_id => 'My Human Readable name'
  38. Return value:
  39. An object with the above properties.
  40. Example:
  41. /**
  42. * Implementation of hook_activity_info().
  43. */
  44. function flag_friend_activity_info() {
  45. $info = new stdClass();
  46. $info->api = 2;
  47. $info->name = 'flag_friend';
  48. $info->object_type = 'flag_friend';
  49. $info->objects = array('requestor' => 'user', 'requestee' => 'flag_friend'); // array keys are the labels
  50. $info->hooks = array('flag_friend' => array('approve', 'request', 'deny', 'remove'));
  51. $info->realms = array('flag_friend' => 'Flag Friend');
  52. $info->type_options = array();
  53. return $info;
  54. }
  55. ------------------------------------------
  56. hook_activity_grants($activity)
  57. Provides a means to record what should have access to any particular message.
  58. Parameters:
  59. $activity - an object that holds a full activity record from the database.
  60. Return value:
  61. A list of keyed by reaml of ids to be stored into the access_table with the above properties.
  62. Example:
  63. For instance, flag_friend returns a one element array of the creator of the
  64. activity. For OG, it would return all the groups that the node belongs in
  65. for instance.
  66. /**
  67. * Implementation of hook_activity_grants().
  68. */
  69. function flag_friend_activity_grants($activity) {
  70. return array(
  71. 'flag_friend' => array($activity->uid), // the module_id that will be used
  72. );
  73. }
  74. ------------------------------------------
  75. hook_activity_access_grants($account)
  76. Provide a means for other modules to determine who can have access to any
  77. given activity message.
  78. Parameters:
  79. $account - the account of the message that we're determining access for.
  80. Return value:
  81. The an array keyed by realm of Ids for the module that the user will have access too.
  82. Example:
  83. /**
  84. * Implementation of hook_activity_access_grants().
  85. */
  86. function flag_friend_activity_access_grants($acccount) {
  87. $friends = flag_friend_get_friends($account->uid);
  88. $realm_ids = array();
  89. if (!empty($friends)) {
  90. foreach ($friends as $friend) {
  91. $realm_ids['flag_friend'][] = $friend;
  92. }
  93. }
  94. return $realm_ids;
  95. }
  96. ------------------------------------------
  97. hook_activity_record_alter(&$record, $context)
  98. Provide a means to alter an activity record before it is inserted into the db.
  99. Parameters:
  100. $record - the record for insertion, containing uid, op, type,
  101. author_message, everyone_message, nid, and created.
  102. $context - the context from the trigger, containing hook, op, object,
  103. author-pattern, and everyone-pattern.
  104. Return value:
  105. $record is passed by reference in order to make changes to it before insert
  106. Example:
  107. /**
  108. * Implementation of hook_activity_records_alter().
  109. */
  110. function example_activity_records_alter(&$record, $context) {
  111. // If we have a story node rather than another type, we can change the
  112. // token pattern.
  113. if ($object->type == 'story') {
  114. $author_pattern = $context['author-pattern'] .' - [node-type]';
  115. $record->author_message = token_replace($author_pattern, $record->type, $context[$record->type]);
  116. }
  117. }
  118. ------------------------------------------
  119. hook_activity_messages_alter(&$messages, $type)
  120. Provides a means to alter the activity messages before they are inserted.
  121. Parameters:
  122. $messages - the translated messages keyed by uid which have already had
  123. their tokens replaced.
  124. $type - the type of activity message as defined by the implementing
  125. module.
  126. Example:
  127. /**
  128. * Implementation of hook_activity_messages_alter().
  129. */
  130. function example_activity_messages_alter(&$messages, $type) {
  131. if ($type == 'nodeapi') {
  132. // You should probably never do this, but it illustrates the $message
  133. // structure. Do no record Anonymous messages.
  134. unset($messages[0]);
  135. // Most use cases will actually be string replacement methods on the
  136. // $messages[$uid] so that you can target a specific message that will
  137. // show up for a particular user.
  138. }
  139. }
  140. ------------------------------------------
  141. hook_activity_message_recorded($record, $context)
  142. Provides a means to do something with a record after it has been saved.
  143. Parameters:
  144. These are the same as hook_activity_record_alter() except that since the
  145. $record has been saved, it now has a $record->amid.
  146. Example:
  147. /**
  148. * Implementation of hook_activity_message_recorded().
  149. */
  150. function activity_user_status_activity_message_recorded($record) {
  151. // After a message has been recorded with activity, we then save it's id so
  152. // that we can reference it as a foreign key.
  153. if ($record->type == 'activity_user_status') {
  154. db_query("UPDATE {activity_user_status} SET amid = %d WHERE uid = %d", $record->amid, $record->uid);
  155. }
  156. }
  157. ------------------------------------------
  158. hook_activity_access_records_alter(&$grants)
  159. Provides a mean to alter the grants that are recorded to the activity_access
  160. table.
  161. Parameters:
  162. $grants - these are the grants that come back from other modules whom have
  163. implemented hook_activity_grants().
  164. $record - This is the activity record in the database
  165. Example:
  166. /**
  167. * Implementation of hook_activity_access_records_alter().
  168. * This example removes any access records except og. Prevents friend
  169. * modules from providing access.
  170. */
  171. function example_activity_access_records_alter(&$grants, $record) {
  172. foreach ($grants as $realm => $value) {
  173. if ($realm != 'og') {
  174. unset($grants[$module]);
  175. }
  176. }
  177. }
  178. -------------------------------------------
  179. hook_activity_type_check($token_objects, $types)
  180. This hook is called when a module implements hook_activity_info() and provides configurable
  181. types. These types need to be checked against the objects loaded up for this activity.
  182. Parameters
  183. $token_objects - This array of objects are objects that will be used during token_replace.
  184. These objects generally match those defined in $info->objects
  185. $types - These are the types selected from the options provided in $info->types.
  186. For instance, when the activity template is created, the users chooses
  187. for node insert activity, type of page and story. Then the $types array
  188. will contain two elements, array('page', 'story').
  189. Example:
  190. /**
  191. * Implementation of hook_activity_type_check().
  192. */
  193. function node_activity_type_check($token_objects, $types) {
  194. return (in_array($token_objects['node']->type, $types));
  195. }
  196. ---------------------------------------------
  197. hook_activity_objects_alter(&token_$objects, $activity_type)
  198. This hook is called for each activity record. It gives modules the
  199. oppurtunity to change the objects based on activity_type which
  200. corresponds to the type column in the activity table.
  201. Parameters
  202. $token_objects - This arrya of objects will be used during
  203. token_replace.
  204. $activity_type - The type of activity being recorded. i.e. 'node',
  205. 'comment', 'user'
  206. Example:
  207. /**
  208. * Implementation of hook_activity_objects_alter().
  209. */
  210. function comment_activity_objects_alter(&$token_objects, $activity_type) {
  211. if ($type == 'comment') {
  212. $objects['node'] = node_load($objects['comment']->nid);
  213. $objects['user'] = $GLOBALS['user'];
  214. // If the comment and the node are the same, provide an object for it.
  215. if (isset($objects['comment']) && $objects['node']->uid == $objects['comment']->uid) {
  216. $objects['node_comment_author'] = $objects['comment'];
  217. }
  218. }
  219. }