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 .= '

' . check_plain($comment->subject) . '

' . check_markup($comment->comment, $comment->format, '', TRUE); danielebarchiesi@0: } danielebarchiesi@0: return $text; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Perform node validation before a node is created or updated. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from node_validate(), after a user has has finished danielebarchiesi@0: * editing the node and is previewing or submitting it. It is invoked at the danielebarchiesi@0: * end of all the standard validation steps, and after the type-specific danielebarchiesi@0: * hook_validate() is invoked. danielebarchiesi@0: * danielebarchiesi@0: * To indicate a validation error, use form_set_error(). danielebarchiesi@0: * danielebarchiesi@0: * Note: Changes made to the $node object within your hook implementation will danielebarchiesi@0: * have no effect. The preferred method to change a node's content is to use danielebarchiesi@0: * hook_node_presave() instead. If it is really necessary to change the node at danielebarchiesi@0: * the validate stage, you can use form_set_value(). danielebarchiesi@0: * danielebarchiesi@0: * @param $node danielebarchiesi@0: * The node being validated. danielebarchiesi@0: * @param $form danielebarchiesi@0: * The form being used to edit the node. danielebarchiesi@0: * @param $form_state danielebarchiesi@0: * The form state array. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_validate($node, $form, &$form_state) { danielebarchiesi@0: if (isset($node->end) && isset($node->start)) { danielebarchiesi@0: if ($node->start > $node->end) { danielebarchiesi@0: form_set_error('time', t('An event may not end before it starts.')); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on a node after validated form values have been copied to it. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked when a node form is submitted with either the "Save" or danielebarchiesi@0: * "Preview" button, after form values have been copied to the form state's node danielebarchiesi@0: * object, but before the node is saved or previewed. It is a chance for modules danielebarchiesi@0: * to adjust the node's properties from what they are simply after a copy from danielebarchiesi@0: * $form_state['values']. This hook is intended for adjusting non-field-related danielebarchiesi@0: * properties. See hook_field_attach_submit() for customizing field-related danielebarchiesi@0: * properties. danielebarchiesi@0: * danielebarchiesi@0: * @param $node danielebarchiesi@0: * The node object being updated in response to a form submission. danielebarchiesi@0: * @param $form danielebarchiesi@0: * The form being used to edit the node. danielebarchiesi@0: * @param $form_state danielebarchiesi@0: * The form state array. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_submit($node, $form, &$form_state) { danielebarchiesi@0: // Decompose the selected menu parent option into 'menu_name' and 'plid', if danielebarchiesi@0: // the form used the default parent selection widget. danielebarchiesi@0: if (!empty($form_state['values']['menu']['parent'])) { danielebarchiesi@0: list($node->menu['menu_name'], $node->menu['plid']) = explode(':', $form_state['values']['menu']['parent']); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Act on a node that is being assembled before rendering. danielebarchiesi@0: * danielebarchiesi@0: * The module may add elements to $node->content prior to rendering. This hook danielebarchiesi@0: * will be called after hook_view(). The structure of $node->content is a danielebarchiesi@0: * renderable array as expected by drupal_render(). danielebarchiesi@0: * danielebarchiesi@0: * When $view_mode is 'rss', modules can also add extra RSS elements and danielebarchiesi@0: * namespaces to $node->rss_elements and $node->rss_namespaces respectively for danielebarchiesi@0: * the RSS item generated for this node. danielebarchiesi@0: * For details on how this is used, see node_feed(). danielebarchiesi@0: * danielebarchiesi@0: * @see blog_node_view() danielebarchiesi@0: * @see forum_node_view() danielebarchiesi@0: * @see comment_node_view() danielebarchiesi@0: * danielebarchiesi@0: * @param $node danielebarchiesi@0: * The node that is being assembled for rendering. danielebarchiesi@0: * @param $view_mode danielebarchiesi@0: * The $view_mode parameter from node_view(). danielebarchiesi@0: * @param $langcode danielebarchiesi@0: * The language code used for rendering. danielebarchiesi@0: * danielebarchiesi@0: * @see hook_entity_view() danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_view($node, $view_mode, $langcode) { danielebarchiesi@0: $node->content['my_additional_field'] = array( danielebarchiesi@0: '#markup' => $additional_field, danielebarchiesi@0: '#weight' => 10, danielebarchiesi@0: '#theme' => 'mymodule_my_additional_field', danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Alter the results of node_view(). danielebarchiesi@0: * danielebarchiesi@0: * This hook is called after the content has been assembled in a structured danielebarchiesi@0: * array and may be used for doing processing which requires that the complete danielebarchiesi@0: * node content structure has been built. danielebarchiesi@0: * danielebarchiesi@0: * If the module wishes to act on the rendered HTML of the node rather than the danielebarchiesi@0: * structured content array, it may use this hook to add a #post_render danielebarchiesi@0: * callback. Alternatively, it could also implement hook_preprocess_node(). See danielebarchiesi@0: * drupal_render() and theme() documentation respectively for details. danielebarchiesi@0: * danielebarchiesi@0: * @param $build danielebarchiesi@0: * A renderable array representing the node content. danielebarchiesi@0: * danielebarchiesi@0: * @see node_view() danielebarchiesi@0: * @see hook_entity_view_alter() danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_view_alter(&$build) { danielebarchiesi@0: if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) { danielebarchiesi@0: // Change its weight. danielebarchiesi@0: $build['an_additional_field']['#weight'] = -10; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Add a #post_render callback to act on the rendered HTML of the node. danielebarchiesi@0: $build['#post_render'][] = 'my_module_node_post_render'; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Define module-provided node types. danielebarchiesi@0: * danielebarchiesi@0: * This hook allows a module to define one or more of its own node types. For danielebarchiesi@0: * example, the blog module uses it to define a blog node-type named "Blog danielebarchiesi@0: * entry." The name and attributes of each desired node type are specified in an danielebarchiesi@0: * array returned by the hook. danielebarchiesi@0: * danielebarchiesi@0: * Only module-provided node types should be defined through this hook. User- danielebarchiesi@0: * provided (or 'custom') node types should be defined only in the 'node_type' danielebarchiesi@0: * database table, and should be maintained by using the node_type_save() and danielebarchiesi@0: * node_type_delete() functions. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An array of information defining the module's node types. The array danielebarchiesi@0: * contains a sub-array for each node type, with the machine-readable type danielebarchiesi@0: * name as the key. Each sub-array has up to 10 attributes. Possible danielebarchiesi@0: * attributes: danielebarchiesi@0: * - name: (required) The human-readable name of the node type. danielebarchiesi@0: * - base: (required) The base string used to construct callbacks danielebarchiesi@0: * corresponding to this node type (for example, if base is defined as danielebarchiesi@0: * example_foo, then example_foo_insert will be called when inserting a node danielebarchiesi@0: * of that type). This string is usually the name of the module, but not danielebarchiesi@0: * always. danielebarchiesi@0: * - description: (required) A brief description of the node type. danielebarchiesi@0: * - help: (optional) Help information shown to the user when creating a node danielebarchiesi@0: * of this type. danielebarchiesi@0: * - has_title: (optional) A Boolean indicating whether or not this node type danielebarchiesi@0: * has a title field. danielebarchiesi@0: * - title_label: (optional) The label for the title field of this content danielebarchiesi@0: * type. danielebarchiesi@0: * - locked: (optional) A Boolean indicating whether the administrator can danielebarchiesi@0: * change the machine name of this type. FALSE = changeable (not locked), danielebarchiesi@0: * TRUE = unchangeable (locked). danielebarchiesi@0: * danielebarchiesi@0: * The machine name of a node type should contain only letters, numbers, and danielebarchiesi@0: * underscores. Underscores will be converted into hyphens for the purpose of danielebarchiesi@0: * constructing URLs. danielebarchiesi@0: * danielebarchiesi@0: * All attributes of a node type that are defined through this hook (except for danielebarchiesi@0: * 'locked') can be edited by a site administrator. This includes the danielebarchiesi@0: * machine-readable name of a node type, if 'locked' is set to FALSE. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_info() { danielebarchiesi@0: return array( danielebarchiesi@0: 'blog' => array( danielebarchiesi@0: 'name' => t('Blog entry'), danielebarchiesi@0: 'base' => 'blog', danielebarchiesi@0: 'description' => t('Use for multi-user blogs. Every user gets a personal blog.'), danielebarchiesi@0: ) danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Provide additional methods of scoring for core search results for nodes. danielebarchiesi@0: * danielebarchiesi@0: * A node's search score is used to rank it among other nodes matched by the danielebarchiesi@0: * search, with the highest-ranked nodes appearing first in the search listing. danielebarchiesi@0: * danielebarchiesi@0: * For example, a module allowing users to vote on content could expose an danielebarchiesi@0: * option to allow search results' rankings to be influenced by the average danielebarchiesi@0: * voting score of a node. danielebarchiesi@0: * danielebarchiesi@0: * All scoring mechanisms are provided as options to site administrators, and danielebarchiesi@0: * may be tweaked based on individual sites or disabled altogether if they do danielebarchiesi@0: * not make sense. Individual scoring mechanisms, if enabled, are assigned a danielebarchiesi@0: * weight from 1 to 10. The weight represents the factor of magnification of danielebarchiesi@0: * the ranking mechanism, with higher-weighted ranking mechanisms having more danielebarchiesi@0: * influence. In order for the weight system to work, each scoring mechanism danielebarchiesi@0: * must return a value between 0 and 1 for every node. That value is then danielebarchiesi@0: * multiplied by the administrator-assigned weight for the ranking mechanism, danielebarchiesi@0: * and then the weighted scores from all ranking mechanisms are added, which danielebarchiesi@0: * brings about the same result as a weighted average. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An associative array of ranking data. The keys should be strings, danielebarchiesi@0: * corresponding to the internal name of the ranking mechanism, such as danielebarchiesi@0: * 'recent', or 'comments'. The values should be arrays themselves, with the danielebarchiesi@0: * following keys available: danielebarchiesi@0: * - title: (required) The human readable name of the ranking mechanism. danielebarchiesi@0: * - join: (optional) The part of a query string to join to any additional danielebarchiesi@0: * necessary table. This is not necessary if the table required is already danielebarchiesi@0: * joined to by the base query, such as for the {node} table. Other tables danielebarchiesi@0: * should use the full table name as an alias to avoid naming collisions. danielebarchiesi@0: * - score: (required) The part of a query string to calculate the score for danielebarchiesi@0: * the ranking mechanism based on values in the database. This does not need danielebarchiesi@0: * to be wrapped in parentheses, as it will be done automatically; it also danielebarchiesi@0: * does not need to take the weighted system into account, as it will be danielebarchiesi@0: * done automatically. It does, however, need to calculate a decimal between danielebarchiesi@0: * 0 and 1; be careful not to cast the entire score to an integer by danielebarchiesi@0: * inadvertently introducing a variable argument. danielebarchiesi@0: * - arguments: (optional) If any arguments are required for the score, they danielebarchiesi@0: * can be specified in an array here. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_ranking() { danielebarchiesi@0: // If voting is disabled, we can avoid returning the array, no hard feelings. danielebarchiesi@0: if (variable_get('vote_node_enabled', TRUE)) { danielebarchiesi@0: return array( danielebarchiesi@0: 'vote_average' => array( danielebarchiesi@0: 'title' => t('Average vote'), danielebarchiesi@0: // Note that we use i.sid, the search index's search item id, rather than danielebarchiesi@0: // n.nid. danielebarchiesi@0: 'join' => 'LEFT JOIN {vote_node_data} vote_node_data ON vote_node_data.nid = i.sid', danielebarchiesi@0: // The highest possible score should be 1, and the lowest possible score, danielebarchiesi@0: // always 0, should be 0. danielebarchiesi@0: 'score' => 'vote_node_data.average / CAST(%f AS DECIMAL)', danielebarchiesi@0: // Pass in the highest possible voting score as a decimal argument. danielebarchiesi@0: 'arguments' => array(variable_get('vote_score_max', 5)), danielebarchiesi@0: ), danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Respond to node type creation. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from node_type_save() after the node type is added to danielebarchiesi@0: * the database. danielebarchiesi@0: * danielebarchiesi@0: * @param $info danielebarchiesi@0: * The node type object that is being created. danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_type_insert($info) { danielebarchiesi@0: drupal_set_message(t('You have just created a content type with a machine name %type.', array('%type' => $info->type))); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Respond to node type updates. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from node_type_save() after the node type is updated in danielebarchiesi@0: * the database. danielebarchiesi@0: * danielebarchiesi@0: * @param $info danielebarchiesi@0: * The node type object that is being updated. danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_type_update($info) { danielebarchiesi@0: if (!empty($info->old_type) && $info->old_type != $info->type) { danielebarchiesi@0: $setting = variable_get('comment_' . $info->old_type, COMMENT_NODE_OPEN); danielebarchiesi@0: variable_del('comment_' . $info->old_type); danielebarchiesi@0: variable_set('comment_' . $info->type, $setting); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Respond to node type deletion. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from node_type_delete() after the node type is removed danielebarchiesi@0: * from the database. danielebarchiesi@0: * danielebarchiesi@0: * @param $info danielebarchiesi@0: * The node type object that is being deleted. danielebarchiesi@0: */ danielebarchiesi@0: function hook_node_type_delete($info) { danielebarchiesi@0: variable_del('comment_' . $info->type); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Respond to node deletion. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked only on the module that defines the node's content type danielebarchiesi@0: * (use hook_node_delete() to respond to all node deletions). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from node_delete_multiple() after the node has been danielebarchiesi@0: * removed from the node table in the database, before hook_node_delete() is danielebarchiesi@0: * invoked, and before field_attach_delete() is called. 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_delete($node) { danielebarchiesi@0: db_delete('mytable') danielebarchiesi@0: ->condition('nid', $node->nid) danielebarchiesi@0: ->execute(); 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 only on the module that defines the node's content type danielebarchiesi@0: * (use hook_node_prepare() to act on all node preparations). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from node_object_prepare() before the general danielebarchiesi@0: * hook_node_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_prepare($node) { danielebarchiesi@0: if ($file = file_check_upload($field_name)) { danielebarchiesi@0: $file = file_save_upload($field_name, _image_filename($file->filename, NULL, TRUE)); danielebarchiesi@0: if ($file) { danielebarchiesi@0: if (!image_get_info($file->uri)) { danielebarchiesi@0: form_set_error($field_name, t('Uploaded file is not a valid image')); danielebarchiesi@0: return; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: return; danielebarchiesi@0: } danielebarchiesi@0: $node->images['_original'] = $file->uri; danielebarchiesi@0: _image_build_derivatives($node, TRUE); danielebarchiesi@0: $node->new_file = TRUE; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Display a node editing form. danielebarchiesi@0: * danielebarchiesi@0: * This hook, implemented by node modules, is called to retrieve the form danielebarchiesi@0: * that is displayed to create or edit a node. This form is displayed at path danielebarchiesi@0: * node/add/[node type] or node/[node ID]/edit. danielebarchiesi@0: * danielebarchiesi@0: * The submit and preview buttons, administrative and display controls, and danielebarchiesi@0: * sections added by other modules (such as path settings, menu settings, danielebarchiesi@0: * comment settings, and fields managed by the Field UI module) are danielebarchiesi@0: * displayed automatically by the node module. This hook just needs to danielebarchiesi@0: * return the node title and form editing fields specific to the node type. danielebarchiesi@0: * danielebarchiesi@0: * @param $node danielebarchiesi@0: * The node being added or edited. danielebarchiesi@0: * @param $form_state danielebarchiesi@0: * The form state array. danielebarchiesi@0: * danielebarchiesi@0: * @return danielebarchiesi@0: * An array containing the title and any custom form elements to be displayed danielebarchiesi@0: * in the node editing form. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_form($node, &$form_state) { danielebarchiesi@0: $type = node_type_get_type($node); danielebarchiesi@0: danielebarchiesi@0: $form['title'] = array( danielebarchiesi@0: '#type' => 'textfield', danielebarchiesi@0: '#title' => check_plain($type->title_label), danielebarchiesi@0: '#default_value' => !empty($node->title) ? $node->title : '', danielebarchiesi@0: '#required' => TRUE, '#weight' => -5 danielebarchiesi@0: ); danielebarchiesi@0: danielebarchiesi@0: $form['field1'] = array( danielebarchiesi@0: '#type' => 'textfield', danielebarchiesi@0: '#title' => t('Custom field'), danielebarchiesi@0: '#default_value' => $node->field1, danielebarchiesi@0: '#maxlength' => 127, danielebarchiesi@0: ); danielebarchiesi@0: $form['selectbox'] = array( danielebarchiesi@0: '#type' => 'select', danielebarchiesi@0: '#title' => t('Select box'), danielebarchiesi@0: '#default_value' => $node->selectbox, danielebarchiesi@0: '#options' => array( danielebarchiesi@0: 1 => 'Option A', danielebarchiesi@0: 2 => 'Option B', danielebarchiesi@0: 3 => 'Option C', danielebarchiesi@0: ), danielebarchiesi@0: '#description' => t('Choose an option.'), danielebarchiesi@0: ); danielebarchiesi@0: danielebarchiesi@0: return $form; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Respond to creation of a new node. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked only on the module that defines the node's content type danielebarchiesi@0: * (use hook_node_insert() to act on all node insertions). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from node_save() after the node is inserted into the danielebarchiesi@0: * node table in the database, before field_attach_insert() is called, and danielebarchiesi@0: * before hook_node_insert() is invoked. 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_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 nodes being loaded from the database. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked only on the module that defines the node's content type danielebarchiesi@0: * (use hook_node_load() to respond to all node loads). 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_node_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: * This hook should only be used to add information that is not in the node or danielebarchiesi@0: * node revisions table, not to replace information that is in these tables danielebarchiesi@0: * (which could interfere with the entity cache). For performance reasons, danielebarchiesi@0: * information for all available nodes should be loaded in a single query where danielebarchiesi@0: * possible. danielebarchiesi@0: * danielebarchiesi@0: * @param $nodes danielebarchiesi@0: * An array of the nodes being loaded, keyed by nid. danielebarchiesi@0: * danielebarchiesi@0: * For a detailed usage example, see node_example.module. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_load($nodes) { danielebarchiesi@0: $result = db_query('SELECT nid, foo FROM {mytable} WHERE nid IN (:nids)', array(':nids' => array_keys($nodes))); danielebarchiesi@0: foreach ($result as $record) { danielebarchiesi@0: $nodes[$record->nid]->foo = $record->foo; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Respond to updates to a node. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked only on the module that defines the node's content type danielebarchiesi@0: * (use hook_node_update() to act on all node updates). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from node_save() after the node is updated in the danielebarchiesi@0: * node table in the database, before field_attach_update() is called, and danielebarchiesi@0: * before hook_node_update() is invoked. 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_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: * Perform node validation before a node is created or updated. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked only on the module that defines the node's content type danielebarchiesi@0: * (use hook_node_validate() to act on all node validations). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked from node_validate(), after a user has finished danielebarchiesi@0: * editing the node and is previewing or submitting it. It is invoked at the end danielebarchiesi@0: * of all the standard validation steps, and before hook_node_validate() is danielebarchiesi@0: * invoked. danielebarchiesi@0: * danielebarchiesi@0: * To indicate a validation error, use form_set_error(). danielebarchiesi@0: * danielebarchiesi@0: * Note: Changes made to the $node object within your hook implementation will danielebarchiesi@0: * have no effect. The preferred method to change a node's content is to use danielebarchiesi@0: * hook_node_presave() instead. danielebarchiesi@0: * danielebarchiesi@0: * @param $node danielebarchiesi@0: * The node being validated. danielebarchiesi@0: * @param $form danielebarchiesi@0: * The form being used to edit the node. danielebarchiesi@0: * @param $form_state danielebarchiesi@0: * The form state array. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_validate($node, $form, &$form_state) { danielebarchiesi@0: if (isset($node->end) && isset($node->start)) { danielebarchiesi@0: if ($node->start > $node->end) { danielebarchiesi@0: form_set_error('time', t('An event may not end before it starts.')); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Display a node. danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked only on the module that defines the node's content type danielebarchiesi@0: * (use hook_node_view() to act on all node views). danielebarchiesi@0: * danielebarchiesi@0: * This hook is invoked during node viewing after the node is fully loaded, so danielebarchiesi@0: * that the node type module can define a custom method for display, or add to danielebarchiesi@0: * the default display. danielebarchiesi@0: * danielebarchiesi@0: * @param $node danielebarchiesi@0: * The node to be displayed, as returned by node_load(). danielebarchiesi@0: * @param $view_mode danielebarchiesi@0: * View mode, e.g. 'full', 'teaser', ... danielebarchiesi@0: * @return danielebarchiesi@0: * The passed $node parameter should be modified as necessary and returned so danielebarchiesi@0: * it can be properly presented. Nodes are prepared for display by assembling danielebarchiesi@0: * a structured array, formatted as in the Form API, in $node->content. As danielebarchiesi@0: * with Form API arrays, the #weight property can be used to control the danielebarchiesi@0: * relative positions of added elements. After this hook is invoked, danielebarchiesi@0: * node_view() calls field_attach_view() to add field views to $node->content, danielebarchiesi@0: * and then invokes hook_node_view() and hook_node_view_alter(), so if you danielebarchiesi@0: * want to affect the final view of the node, you might consider implementing danielebarchiesi@0: * one of these hooks instead. danielebarchiesi@0: * danielebarchiesi@0: * @ingroup node_api_hooks danielebarchiesi@0: */ danielebarchiesi@0: function hook_view($node, $view_mode) { danielebarchiesi@0: if ($view_mode == 'full' && node_is_page($node)) { danielebarchiesi@0: $breadcrumb = array(); danielebarchiesi@0: $breadcrumb[] = l(t('Home'), NULL); danielebarchiesi@0: $breadcrumb[] = l(t('Example'), 'example'); danielebarchiesi@0: $breadcrumb[] = l($node->field1, 'example/' . $node->field1); danielebarchiesi@0: drupal_set_breadcrumb($breadcrumb); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: $node->content['myfield'] = array( danielebarchiesi@0: '#markup' => theme('mymodule_myfield', $node->myfield), danielebarchiesi@0: '#weight' => 1, danielebarchiesi@0: ); danielebarchiesi@0: danielebarchiesi@0: return $node; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * @} End of "addtogroup hooks". danielebarchiesi@0: */