danielebarchiesi@0: 0, danielebarchiesi@0: * 'gid' => 888, danielebarchiesi@0: * 'realm' => 'example_realm', danielebarchiesi@0: * 'grant_view' => 1, danielebarchiesi@0: * 'grant_update' => 0, danielebarchiesi@0: * 'grant_delete' => 0, danielebarchiesi@0: * ); danielebarchiesi@0: * drupal_write_record('node_access', $record); danielebarchiesi@0: * @endcode danielebarchiesi@0: * And then in its hook_node_grants() implementation, it would need to return: danielebarchiesi@0: * @code danielebarchiesi@0: * if ($op == 'view') { danielebarchiesi@0: * $grants['example_realm'] = array(888); danielebarchiesi@0: * } danielebarchiesi@0: * @endcode danielebarchiesi@0: * If you decide to do this, be aware that the node_access_rebuild() function danielebarchiesi@0: * will erase any node ID 0 entry when it is called, so you will need to make danielebarchiesi@0: * sure to restore your {node_access} record after node_access_rebuild() is danielebarchiesi@0: * called. danielebarchiesi@0: * danielebarchiesi@0: * @see node_access_view_all_nodes() danielebarchiesi@0: * @see node_access_rebuild() danielebarchiesi@0: * danielebarchiesi@0: * @param $account danielebarchiesi@0: * The user object whose grants are requested. danielebarchiesi@0: * @param $op danielebarchiesi@0: * The node operation to be performed, such as 'view', 'update', or 'delete'. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An array whose keys are "realms" of grants, and whose values are arrays of danielebarchiesi@0: * the grant IDs within this realm that this user is being granted. danielebarchiesi@0: * danielebarchiesi@0: * For a detailed example, see node_access_example.module. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_access danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_grants($account, $op) { danielebarchiesi@0: if (user_access('access private content', $account)) { danielebarchiesi@0: $grants['example'] = array(1); danielebarchiesi@0: } danielebarchiesi@0: $grants['example_owner'] = array($account->uid); danielebarchiesi@0: return $grants; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Set permissions for a node to be written to the database. danielebarchiesi@0: * danielebarchiesi@0: * When a node is saved, a module implementing hook_node_access_records() will danielebarchiesi@0: * be asked if it is interested in the access permissions for a node. If it is danielebarchiesi@0: * interested, it must respond with an array of permissions arrays for that danielebarchiesi@0: * node. danielebarchiesi@0: * danielebarchiesi@0: * Node access grants apply regardless of the published or unpublished status danielebarchiesi@0: * of the node. Implementations must make sure not to grant access to danielebarchiesi@0: * unpublished nodes if they don't want to change the standard access control danielebarchiesi@0: * behavior. Your module may need to create a separate access realm to handle danielebarchiesi@0: * access to unpublished nodes. danielebarchiesi@0: * danielebarchiesi@0: * Note that the grant values in the return value from your hook must be danielebarchiesi@0: * integers and not boolean TRUE and FALSE. danielebarchiesi@0: * danielebarchiesi@0: * Each permissions item in the array is an array with the following elements: danielebarchiesi@0: * - 'realm': The name of a realm that the module has defined in danielebarchiesi@0: * hook_node_grants(). danielebarchiesi@0: * - 'gid': A 'grant ID' from hook_node_grants(). danielebarchiesi@0: * - 'grant_view': If set to 1 a user that has been identified as a member danielebarchiesi@0: * of this gid within this realm can view this node. This should usually be danielebarchiesi@0: * set to $node->status. Failure to do so may expose unpublished content danielebarchiesi@0: * to some users. danielebarchiesi@0: * - 'grant_update': If set to 1 a user that has been identified as a member danielebarchiesi@0: * of this gid within this realm can edit this node. danielebarchiesi@0: * - 'grant_delete': If set to 1 a user that has been identified as a member danielebarchiesi@0: * of this gid within this realm can delete this node. danielebarchiesi@0: * - 'priority': If multiple modules seek to set permissions on a node, the danielebarchiesi@0: * realms that have the highest priority will win out, and realms with a lower danielebarchiesi@0: * priority will not be written. If there is any doubt, it is best to danielebarchiesi@0: * leave this 0. danielebarchiesi@0: * danielebarchiesi@0: * danielebarchiesi@0: * When an implementation is interested in a node but want to deny access to danielebarchiesi@0: * everyone, it may return a "deny all" grant: danielebarchiesi@0: * danielebarchiesi@0: * @code danielebarchiesi@0: * $grants[] = array( danielebarchiesi@0: * 'realm' => 'all', danielebarchiesi@0: * 'gid' => 0, danielebarchiesi@0: * 'grant_view' => 0, danielebarchiesi@0: * 'grant_update' => 0, danielebarchiesi@0: * 'grant_delete' => 0, danielebarchiesi@0: * 'priority' => 1, danielebarchiesi@0: * ); danielebarchiesi@0: * @endcode danielebarchiesi@0: * danielebarchiesi@0: * Setting the priority should cancel out other grants. In the case of a danielebarchiesi@0: * conflict between modules, it is safer to use hook_node_access_records_alter() danielebarchiesi@0: * to return only the deny grant. danielebarchiesi@0: * danielebarchiesi@0: * Note: a deny all grant is not written to the database; denies are implicit. danielebarchiesi@0: * danielebarchiesi@0: * @see node_access_write_grants() danielebarchiesi@0: * danielebarchiesi@0: * @param $node danielebarchiesi@0: * The node that has just been saved. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An array of grants as defined above. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_node_access_records_alter() danielebarchiesi@0: * @ingroup node_access danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_access_records($node) { danielebarchiesi@0: // We only care about the node if it has been marked private. If not, it is danielebarchiesi@0: // treated just like any other node and we completely ignore it. danielebarchiesi@0: if ($node->private) { danielebarchiesi@0: $grants = array(); danielebarchiesi@0: // Only published nodes should be viewable to all users. If we allow access danielebarchiesi@0: // blindly here, then all users could view an unpublished node. danielebarchiesi@0: if ($node->status) { danielebarchiesi@0: $grants[] = array( danielebarchiesi@0: 'realm' => 'example', danielebarchiesi@0: 'gid' => 1, danielebarchiesi@0: 'grant_view' => 1, danielebarchiesi@0: 'grant_update' => 0, danielebarchiesi@0: 'grant_delete' => 0, danielebarchiesi@0: 'priority' => 0, danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: // For the example_author array, the GID is equivalent to a UID, which danielebarchiesi@0: // means there are many groups of just 1 user. danielebarchiesi@0: // Note that an author can always view his or her nodes, even if they danielebarchiesi@0: // have status unpublished. danielebarchiesi@0: $grants[] = array( danielebarchiesi@0: 'realm' => 'example_author', danielebarchiesi@0: 'gid' => $node->uid, danielebarchiesi@0: 'grant_view' => 1, danielebarchiesi@0: 'grant_update' => 1, danielebarchiesi@0: 'grant_delete' => 1, danielebarchiesi@0: 'priority' => 0, danielebarchiesi@0: ); danielebarchiesi@0: danielebarchiesi@0: return $grants; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Alter permissions for a node before it is written to the database. danielebarchiesi@0: * danielebarchiesi@0: * Node access modules establish rules for user access to content. Node access danielebarchiesi@0: * records are stored in the {node_access} table and define which permissions danielebarchiesi@0: * are required to access a node. This hook is invoked after node access modules danielebarchiesi@0: * returned their requirements via hook_node_access_records(); doing so allows danielebarchiesi@0: * modules to modify the $grants array by reference before it is stored, so danielebarchiesi@0: * custom or advanced business logic can be applied. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_node_access_records() danielebarchiesi@0: * danielebarchiesi@0: * Upon viewing, editing or deleting a node, hook_node_grants() builds a danielebarchiesi@0: * permissions array that is compared against the stored access records. The danielebarchiesi@0: * user must have one or more matching permissions in order to complete the danielebarchiesi@0: * requested operation. danielebarchiesi@0: * danielebarchiesi@0: * A module may deny all access to a node by setting $grants to an empty array. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_node_grants() danielebarchiesi@0: * @see hook_node_grants_alter() danielebarchiesi@0: * danielebarchiesi@0: * @param $grants danielebarchiesi@0: * The $grants array returned by hook_node_access_records(). danielebarchiesi@0: * @param $node danielebarchiesi@0: * The node for which the grants were acquired. danielebarchiesi@0: * danielebarchiesi@0: * The preferred use of this hook is in a module that bridges multiple node danielebarchiesi@0: * access modules with a configurable behavior, as shown in the example with the danielebarchiesi@0: * 'is_preview' field. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_access danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_access_records_alter(&$grants, $node) { danielebarchiesi@0: // Our module allows editors to mark specific articles with the 'is_preview' danielebarchiesi@0: // field. If the node being saved has a TRUE value for that field, then only danielebarchiesi@0: // our grants are retained, and other grants are removed. Doing so ensures danielebarchiesi@0: // that our rules are enforced no matter what priority other grants are given. danielebarchiesi@0: if ($node->is_preview) { danielebarchiesi@0: // Our module grants are set in $grants['example']. danielebarchiesi@0: $temp = $grants['example']; danielebarchiesi@0: // Now remove all module grants but our own. danielebarchiesi@0: $grants = array('example' => $temp); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Alter user access rules when trying to view, edit or delete a node. danielebarchiesi@0: * danielebarchiesi@0: * Node access modules establish rules for user access to content. danielebarchiesi@0: * hook_node_grants() defines permissions for a user to view, edit or delete danielebarchiesi@0: * nodes by building a $grants array that indicates the permissions assigned to danielebarchiesi@0: * the user by each node access module. This hook is called to allow modules to danielebarchiesi@0: * modify the $grants array by reference, so the interaction of multiple node danielebarchiesi@0: * access modules can be altered or advanced business logic can be applied. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_node_grants() danielebarchiesi@0: * danielebarchiesi@0: * The resulting grants are then checked against the records stored in the danielebarchiesi@0: * {node_access} table to determine if the operation may be completed. danielebarchiesi@0: * danielebarchiesi@0: * A module may deny all access to a user by setting $grants to an empty array. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_node_access_records() danielebarchiesi@0: * @see hook_node_access_records_alter() danielebarchiesi@0: * danielebarchiesi@0: * @param $grants danielebarchiesi@0: * The $grants array returned by hook_node_grants(). danielebarchiesi@0: * @param $account danielebarchiesi@0: * The user account requesting access to content. danielebarchiesi@0: * @param $op danielebarchiesi@0: * The operation being performed, 'view', 'update' or 'delete'. danielebarchiesi@0: * danielebarchiesi@0: * Developers may use this hook to either add additional grants to a user or to danielebarchiesi@0: * remove existing grants. These rules are typically based on either the danielebarchiesi@0: * permissions assigned to a user role, or specific attributes of a user danielebarchiesi@0: * account. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_access danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_grants_alter(&$grants, $account, $op) { danielebarchiesi@0: // Our sample module never allows certain roles to edit or delete danielebarchiesi@0: // content. Since some other node access modules might allow this danielebarchiesi@0: // permission, we expressly remove it by returning an empty $grants danielebarchiesi@0: // array for roles specified in our variable setting. danielebarchiesi@0: danielebarchiesi@0: // Get our list of banned roles. danielebarchiesi@0: $restricted = variable_get('example_restricted_roles', array()); danielebarchiesi@0: danielebarchiesi@0: if ($op != 'view' && !empty($restricted)) { danielebarchiesi@0: // Now check the roles for this account against the restrictions. danielebarchiesi@0: foreach ($restricted as $role_id) { danielebarchiesi@0: if (isset($account->roles[$role_id])) { danielebarchiesi@0: $grants = array(); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Add mass node operations. danielebarchiesi@0: * danielebarchiesi@0: * This hook enables modules to inject custom operations into the mass danielebarchiesi@0: * operations dropdown found at admin/content, by associating a callback danielebarchiesi@0: * function with the operation, which is called when the form is submitted. The danielebarchiesi@0: * callback function receives one initial argument, which is an array of the danielebarchiesi@0: * checked nodes. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An array of operations. Each operation is an associative array that may danielebarchiesi@0: * contain the following key-value pairs: danielebarchiesi@0: * - label: (required) The label for the operation, displayed in the dropdown danielebarchiesi@0: * menu. danielebarchiesi@0: * - callback: (required) The function to call for the operation. danielebarchiesi@0: * - callback arguments: (optional) An array of additional arguments to pass danielebarchiesi@0: * to the callback function. danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_operations() { danielebarchiesi@0: $operations = array( danielebarchiesi@0: 'publish' => array( danielebarchiesi@0: 'label' => t('Publish selected content'), danielebarchiesi@0: 'callback' => 'node_mass_update', danielebarchiesi@0: 'callback arguments' => array('updates' => array('status' => NODE_PUBLISHED)), danielebarchiesi@0: ), danielebarchiesi@0: 'unpublish' => array( danielebarchiesi@0: 'label' => t('Unpublish selected content'), danielebarchiesi@0: 'callback' => 'node_mass_update', danielebarchiesi@0: 'callback arguments' => array('updates' => array('status' => NODE_NOT_PUBLISHED)), danielebarchiesi@0: ), danielebarchiesi@0: 'promote' => array( danielebarchiesi@0: 'label' => t('Promote selected content to front page'), danielebarchiesi@0: 'callback' => 'node_mass_update', danielebarchiesi@0: 'callback arguments' => array('updates' => array('status' => NODE_PUBLISHED, 'promote' => NODE_PROMOTED)), danielebarchiesi@0: ), danielebarchiesi@0: 'demote' => array( danielebarchiesi@0: 'label' => t('Demote selected content from front page'), danielebarchiesi@0: 'callback' => 'node_mass_update', danielebarchiesi@0: 'callback arguments' => array('updates' => array('promote' => NODE_NOT_PROMOTED)), danielebarchiesi@0: ), danielebarchiesi@0: 'sticky' => array( danielebarchiesi@0: 'label' => t('Make selected content sticky'), danielebarchiesi@0: 'callback' => 'node_mass_update', danielebarchiesi@0: 'callback arguments' => array('updates' => array('status' => NODE_PUBLISHED, 'sticky' => NODE_STICKY)), danielebarchiesi@0: ), danielebarchiesi@0: 'unsticky' => array( danielebarchiesi@0: 'label' => t('Make selected content not sticky'), danielebarchiesi@0: 'callback' => 'node_mass_update', danielebarchiesi@0: 'callback arguments' => array('updates' => array('sticky' => NODE_NOT_STICKY)), danielebarchiesi@0: ), danielebarchiesi@0: 'delete' => array( danielebarchiesi@0: 'label' => t('Delete selected content'), danielebarchiesi@0: 'callback' => NULL, danielebarchiesi@0: ), danielebarchiesi@0: ); danielebarchiesi@0: return $operations; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Respond to node deletion. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from node_delete_multiple() after the type-specific danielebarchiesi@0: * hook_delete() has been invoked, but before hook_entity_delete and danielebarchiesi@0: * field_attach_delete() are called, and before the node is removed from the danielebarchiesi@0: * node table in the database. danielebarchiesi@0: * danielebarchiesi@0: * @param $node danielebarchiesi@0: * The node that is being deleted. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_delete($node) { danielebarchiesi@0: db_delete('mytable') danielebarchiesi@0: ->condition('nid', $node->nid) danielebarchiesi@0: ->execute(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Respond to deletion of a node revision. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from node_revision_delete() after the revision has been danielebarchiesi@0: * removed from the node_revision table, and before danielebarchiesi@0: * field_attach_delete_revision() is called. danielebarchiesi@0: * danielebarchiesi@0: * @param $node danielebarchiesi@0: * The node revision (node object) that is being deleted. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_revision_delete($node) { danielebarchiesi@0: db_delete('mytable') danielebarchiesi@0: ->condition('vid', $node->vid) danielebarchiesi@0: ->execute(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Respond to creation of a new node. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from node_save() after the database query that will danielebarchiesi@0: * insert the node into the node table is scheduled for execution, after the danielebarchiesi@0: * type-specific hook_insert() is invoked, and after field_attach_insert() is danielebarchiesi@0: * called. danielebarchiesi@0: * danielebarchiesi@0: * Note that when this hook is invoked, the changes have not yet been written to danielebarchiesi@0: * the database, because a database transaction is still in progress. The danielebarchiesi@0: * transaction is not finalized until the save operation is entirely completed danielebarchiesi@0: * and node_save() goes out of scope. You should not rely on data in the danielebarchiesi@0: * database at this time as it is not updated yet. You should also note that any danielebarchiesi@0: * write/update database queries executed from this hook are also not committed danielebarchiesi@0: * immediately. Check node_save() and db_transaction() for more info. danielebarchiesi@0: * danielebarchiesi@0: * @param $node danielebarchiesi@0: * The node that is being created. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_insert($node) { danielebarchiesi@0: db_insert('mytable') danielebarchiesi@0: ->fields(array( danielebarchiesi@0: 'nid' => $node->nid, danielebarchiesi@0: 'extra' => $node->extra, danielebarchiesi@0: )) danielebarchiesi@0: ->execute(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on arbitrary nodes being loaded from the database. danielebarchiesi@0: * danielebarchiesi@0: * This hook should be used to add information that is not in the node or node danielebarchiesi@0: * revisions table, not to replace information that is in these tables (which danielebarchiesi@0: * could interfere with the entity cache). For performance reasons, information danielebarchiesi@0: * for all available nodes should be loaded in a single query where possible. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked during node loading, which is handled by entity_load(), danielebarchiesi@0: * via classes NodeController and DrupalDefaultEntityController. After the node danielebarchiesi@0: * information is read from the database or the entity cache, hook_load() is danielebarchiesi@0: * invoked on the node's content type module, then field_attach_load_revision() danielebarchiesi@0: * or field_attach_load() is called, then hook_entity_load() is invoked on all danielebarchiesi@0: * implementing modules, and finally hook_node_load() is invoked on all danielebarchiesi@0: * implementing modules. danielebarchiesi@0: * danielebarchiesi@0: * @param $nodes danielebarchiesi@0: * An array of the nodes being loaded, keyed by nid. danielebarchiesi@0: * @param $types danielebarchiesi@0: * An array containing the node types present in $nodes. Allows for an early danielebarchiesi@0: * return for modules that only support certain node types. However, if your danielebarchiesi@0: * module defines a content type, you can use hook_load() to respond to danielebarchiesi@0: * loading of just that content type. danielebarchiesi@0: * danielebarchiesi@0: * For a detailed usage example, see nodeapi_example.module. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_load($nodes, $types) { danielebarchiesi@0: // Decide whether any of $types are relevant to our purposes. danielebarchiesi@0: if (count(array_intersect($types_we_want_to_process, $types))) { danielebarchiesi@0: // Gather our extra data for each of these nodes. danielebarchiesi@0: $result = db_query('SELECT nid, foo FROM {mytable} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes))); danielebarchiesi@0: // Add our extra data to the node objects. danielebarchiesi@0: foreach ($result as $record) { danielebarchiesi@0: $nodes[$record->nid]->foo = $record->foo; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Control access to a node. danielebarchiesi@0: * danielebarchiesi@0: * Modules may implement this hook if they want to have a say in whether or not danielebarchiesi@0: * a given user has access to perform a given operation on a node. danielebarchiesi@0: * danielebarchiesi@0: * The administrative account (user ID #1) always passes any access check, so danielebarchiesi@0: * this hook is not called in that case. Users with the "bypass node access" danielebarchiesi@0: * permission may always view and edit content through the administrative danielebarchiesi@0: * interface. danielebarchiesi@0: * danielebarchiesi@0: * Note that not all modules will want to influence access on all node types. If danielebarchiesi@0: * your module does not want to actively grant or block access, return danielebarchiesi@0: * NODE_ACCESS_IGNORE or simply return nothing. Blindly returning FALSE will danielebarchiesi@0: * break other node access modules. danielebarchiesi@0: * danielebarchiesi@0: * Also note that this function isn't called for node listings (e.g., RSS feeds, danielebarchiesi@0: * the default home page at path 'node', a recent content block, etc.) See danielebarchiesi@0: * @link node_access Node access rights @endlink for a full explanation. danielebarchiesi@0: * danielebarchiesi@0: * @param $node danielebarchiesi@0: * Either a node object or the machine name of the content type on which to danielebarchiesi@0: * perform the access check. danielebarchiesi@0: * @param $op danielebarchiesi@0: * The operation to be performed. Possible values: danielebarchiesi@0: * - "create" danielebarchiesi@0: * - "delete" danielebarchiesi@0: * - "update" danielebarchiesi@0: * - "view" danielebarchiesi@0: * @param $account danielebarchiesi@0: * The user object to perform the access check operation on. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * - NODE_ACCESS_ALLOW: if the operation is to be allowed. danielebarchiesi@0: * - NODE_ACCESS_DENY: if the operation is to be denied. danielebarchiesi@0: * - NODE_ACCESS_IGNORE: to not affect this operation at all. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_access danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_access($node, $op, $account) { danielebarchiesi@0: $type = is_string($node) ? $node : $node->type; danielebarchiesi@0: danielebarchiesi@0: if (in_array($type, node_permissions_get_configured_types())) { danielebarchiesi@0: if ($op == 'create' && user_access('create ' . $type . ' content', $account)) { danielebarchiesi@0: return NODE_ACCESS_ALLOW; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if ($op == 'update') { danielebarchiesi@0: if (user_access('edit any ' . $type . ' content', $account) || (user_access('edit own ' . $type . ' content', $account) && ($account->uid == $node->uid))) { danielebarchiesi@0: return NODE_ACCESS_ALLOW; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if ($op == 'delete') { danielebarchiesi@0: if (user_access('delete any ' . $type . ' content', $account) || (user_access('delete own ' . $type . ' content', $account) && ($account->uid == $node->uid))) { danielebarchiesi@0: return NODE_ACCESS_ALLOW; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Returning nothing from this function would have the same effect. danielebarchiesi@0: return NODE_ACCESS_IGNORE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on a node object about to be shown on the add/edit form. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from node_object_prepare() after the type-specific danielebarchiesi@0: * hook_prepare() is invoked. danielebarchiesi@0: * danielebarchiesi@0: * @param $node danielebarchiesi@0: * The node that is about to be shown on the add/edit form. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_prepare($node) { danielebarchiesi@0: if (!isset($node->comment)) { danielebarchiesi@0: $node->comment = variable_get("comment_$node->type", COMMENT_NODE_OPEN); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on a node being displayed as a search result. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from node_search_execute(), after node_load() and danielebarchiesi@0: * node_view() have been called. danielebarchiesi@0: * danielebarchiesi@0: * @param $node danielebarchiesi@0: * The node being displayed in a search result. danielebarchiesi@0: * danielebarchiesi@0: * @return array danielebarchiesi@0: * Extra information to be displayed with search result. This information danielebarchiesi@0: * should be presented as an associative array. It will be concatenated with danielebarchiesi@0: * the post information (last updated, author) in the default search result danielebarchiesi@0: * theming. danielebarchiesi@0: * danielebarchiesi@0: * @see template_preprocess_search_result() danielebarchiesi@0: * @see search-result.tpl.php danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_search_result($node) { danielebarchiesi@0: $comments = db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = :nid', array('nid' => $node->nid))->fetchField(); danielebarchiesi@0: return array('comment' => format_plural($comments, '1 comment', '@count comments')); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on a node being inserted or updated. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from node_save() before the node is saved to the danielebarchiesi@0: * database. danielebarchiesi@0: * danielebarchiesi@0: * @param $node danielebarchiesi@0: * The node that is being inserted or updated. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_presave($node) { danielebarchiesi@0: if ($node->nid && $node->moderate) { danielebarchiesi@0: // Reset votes when node is updated: danielebarchiesi@0: $node->score = 0; danielebarchiesi@0: $node->users = ''; danielebarchiesi@0: $node->votes = 0; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Respond to updates to a node. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from node_save() after the database query that will danielebarchiesi@0: * update node in the node table is scheduled for execution, after the danielebarchiesi@0: * type-specific hook_update() is invoked, and after field_attach_update() is danielebarchiesi@0: * called. danielebarchiesi@0: * danielebarchiesi@0: * Note that when this hook is invoked, the changes have not yet been written to danielebarchiesi@0: * the database, because a database transaction is still in progress. The danielebarchiesi@0: * transaction is not finalized until the save operation is entirely completed danielebarchiesi@0: * and node_save() goes out of scope. You should not rely on data in the danielebarchiesi@0: * database at this time as it is not updated yet. You should also note that any danielebarchiesi@0: * write/update database queries executed from this hook are also not committed danielebarchiesi@0: * immediately. Check node_save() and db_transaction() for more info. danielebarchiesi@0: * danielebarchiesi@0: * @param $node danielebarchiesi@0: * The node that is being updated. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_update($node) { danielebarchiesi@0: db_update('mytable') danielebarchiesi@0: ->fields(array('extra' => $node->extra)) danielebarchiesi@0: ->condition('nid', $node->nid) danielebarchiesi@0: ->execute(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on a node being indexed for searching. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked during search indexing, after node_load(), and after the danielebarchiesi@0: * result of node_view() is added as $node->rendered to the node object. danielebarchiesi@0: * danielebarchiesi@0: * @param $node danielebarchiesi@0: * The node being indexed. danielebarchiesi@0: * danielebarchiesi@0: * @return string danielebarchiesi@0: * Additional node information to be indexed. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_update_index($node) { danielebarchiesi@0: $text = ''; danielebarchiesi@0: $comments = db_query('SELECT subject, comment, format FROM {comment} WHERE nid = :nid AND status = :status', array(':nid' => $node->nid, ':status' => COMMENT_PUBLISHED)); danielebarchiesi@0: foreach ($comments as $comment) { danielebarchiesi@0: $text .= '