- <?php
- * @file
- * Hook implementations for the Node Hooks Example module.
- */
-
- * @defgroup node_hooks_example Example: Node Hooks
- * @ingroup examples
- * @{
- * This example demonstrates how to use node hooks.
- *
- * This is an example demonstrating how a module can be used to extend existing
- * content types.
- * We will add the ability for each node to have a rating, a number from one to
- * five. The rating will also be tracked using the revision system, so every
- * revision may have different rating values.
- */
-
- * Implements hook_form_alter().
- *
- * By implementing this hook, we're able to modify any form. We'll only make
- * changes to two types: a node's content type configuration and edit forms.
- *
- * We need to have a way for administrators to indicate which content types
- * should have our rating field added. This is done by inserting radios in the
- * content type's configuration page.
- *
- * Changes made by this hook will be shown when editing the settings of any
- * content type.
- *
- * Optionally, hook_form_FORM_ID_alter() could be used.
- */
- function node_hooks_example_form_alter(&$form, $form_state, $form_id) {
- $config = config('node_hooks_example.settings');
-
- if ($form_id == 'node_type_form') {
-
- $form['rating'] = array(
- '#type' => 'fieldset',
- '#title' => t('Rating settings'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#group' => 'additional_settings',
- '#weight' => -1,
- );
- $form['rating']['node_hooks_example_node_type'] = array(
- '#type' => 'radios',
- '#title' => t('Node Hooks Example rating'),
- '#default_value' => $config->get('node_hooks_example_node_type_' . $form['#node_type']->type),
- '#options' => array(
- FALSE => t('Disabled'),
- TRUE => t('Enabled'),
- ),
- '#description' => t('Should this node have a rating attached to it?'),
- );
-
- $form['#submit'][] = 'node_hooks_example_node_type_form_submit';
- }
- elseif (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id) {
-
-
- $node = $form['#node'];
- if ($config->get('node_hooks_example_node_type_' . $form['type']['#value'])) {
- $form['node_hooks_example_rating'] = array(
- '#type' => 'select',
- '#title' => t('Rating'),
- '#default_value' => isset($node->node_hooks_example_rating) ? $node->node_hooks_example_rating : '',
- '#options' => array(0 => t('Unrated'), 1, 2, 3, 4, 5),
- '#required' => TRUE,
- '#weight' => 0,
- );
- }
- }
- }
-
- * A custom submit handler.
- *
- * This function runs when the 'node_type_form' is saved because we added it as
- * a submit handler.
- */
- function node_hooks_example_node_type_form_submit(&$form, $form_state) {
- config_set('node_hooks_example.settings', 'node_hooks_example_node_type_' . $form['#node_type']->type, $form_state['values']['node_hooks_example_node_type']);
- }
-
- * Implements hook_node_validate().
- *
- * Check that the rating attribute is set in the form submission, since the
- * field is required. If not, send error message.
- */
- function node_hooks_example_node_validate($node, $form) {
- if (config_get('node_hooks_example.settings', 'node_hooks_example_node_type_' . $node->type)) {
- if (isset($node->node_hooks_example_rating) && !$node->node_hooks_example_rating) {
- form_set_error('node_hooks_example_rating', t('You must rate this content.'));
- }
- }
- }
-
- * Implements hook_node_load().
- *
- * Loads the rating information if available for any of the nodes in the
- * argument list.
- */
- function node_hooks_example_node_load($nodes, $types) {
- $config = config('node_hooks_example.settings');
-
-
- $our_types = array();
- foreach ($types as $type) {
- if ($config->get('node_hooks_example_node_type_' . $type)) {
- $our_types[] = $type;
- }
- }
-
-
-
- if (!count($our_types)) {
- return;
- }
-
-
- foreach ($nodes as $node) {
-
- if ($config->get('node_hooks_example_node_type_' . $node->type)) {
- $vids[] = $node->vid;
- }
- }
-
- if (!isset($vids) || !count($vids)) {
- return;
- }
-
-
-
- $result = db_select('node_hooks_example', 'e')
- ->fields('e', array('nid', 'vid', 'rating'))
- ->where('e.vid IN (:vids)', array(':vids' => $vids))
- ->execute();
-
- foreach ($result as $record) {
- $nodes[$record->nid]->node_hooks_example_rating = $record->rating;
- }
- }
-
- * Implements hook_node_insert().
- *
- * As a new node is being inserted into the database, we need to do our own
- * database inserts.
- */
- function node_hooks_example_node_insert($node) {
- if (config_get('node_hooks_example.settings', 'node_hooks_example_node_type_' . $node->type)) {
- db_insert('node_hooks_example')
- ->fields(array('nid' => $node->nid, 'vid' => $node->vid, 'rating' => $node->node_hooks_example_rating))
- ->execute();
- }
- }
-
- * Implements hook_node_delete().
- *
- * When a node is deleted, we need to remove all related records from our table,
- * including all revisions. For the delete operations we use node->nid.
- */
- function node_hooks_example_node_delete($node) {
-
- db_delete('node_hooks_example')
- ->condition('nid', $node->nid)
- ->execute();
- }
-
- * Implements hook_node_update().
- *
- * As an existing node is being updated in the database, we need to do our own
- * database updates.
- *
- * This hook is called when an existing node has been changed. We can't simply
- * update, since the node may not have a rating saved, thus no database field.
- * We first check the database for a rating. If there is one, we update it;
- * otherwise, we call node_hooks_example_node_insert() to create one.
- */
- function node_hooks_example_node_update($node) {
- if (config_get('node_hooks_example.settings', 'node_hooks_example_node_type_' . $node->type)) {
-
- $rating = db_select('node_hooks_example', 'e')
- ->fields('e', array('rating'))
- ->where('e.vid = (:vid)', array(':vid' => $node->vid))
- ->execute()->fetchField();
-
- if ($rating) {
-
- db_update('node_hooks_example')
- ->fields(array('rating' => $node->node_hooks_example_rating))
- ->condition('vid', $node->vid)
- ->execute();
- }
- else {
-
- node_hooks_example_node_insert($node);
- }
- }
- }
-
- * Implements hook_node_view().
- *
- * This is a typical implementation that simply runs the node text through
- * the output filters.
- *
- * Finally, we need to take care of displaying our rating when the node is
- * viewed. This operation is called after the node has already been prepared
- * into HTML and filtered as necessary, so we know we are dealing with an
- * HTML teaser and body. We will inject our additional information at the front
- * of the node copy.
- *
- * Using node API 'hook_node_view' is more appropriate than using a filter here,
- * because filters transform user-supplied content, whereas we are extending it
- * with additional information.
- */
- function node_hooks_example_node_view($node, $build_mode = 'full') {
- if (config_get('node_hooks_example.settings', 'node_hooks_example_node_type_' . $node->type)) {
-
-
- $rating = isset($node->node_hooks_example_rating) ? $node->node_hooks_example_rating : 0;
- $node->content['node_hooks_example'] = array(
- '#markup' => theme('node_hooks_example_rating', array('rating' => $rating)),
- '#weight' => -1,
- );
- }
- }
-
- * Implements hook_theme().
- *
- * This lets us tell Backdrop about our theme functions and their arguments.
- */
- function node_hooks_example_theme() {
- return array('node_hooks_example_rating' => array('variables' => array('rating' => NULL)));
- }
-
- * A custom theme function.
- *
- * By using this function to format our rating, themes can override this
- * presentation if they wish; for example, they could provide a star graphic
- * for the rating. We also wrap the default presentation in a CSS class that
- * is prefixed by the module name. This way, style sheets can modify the output
- * without requiring theme code.
- */
- function theme_node_hooks_example_rating($variables) {
- $options = array(
- 0 => t('Unrated'),
- 1 => t('Poor'),
- 2 => t('Needs improvement'),
- 3 => t('Acceptable'),
- 4 => t('Good'),
- 5 => t('Excellent'),
- );
- $output = '<div class="node_hooks_example_rating">';
- $output .= t('Rating: %rating', array('%rating' => $options[(int) $variables['rating']]));
- $output .= '</div>';
- return $output;
- }
-
- * Implements hook_config_info().
- */
- function node_hooks_example_config_info() {
- $prefixes['node_hooks_example.settings'] = array(
- 'label' => t('Node Hooks Example settings'),
- 'group' => t('Configuration'),
- );
- return $prefixes;
- }
-
- * @} End of "defgroup node_hooks_example".
- */