You are here

finder_node.module in Finder 7

Same filename and directory in other branches
  1. 6 modules/finder_node/finder_node.module

The finder node module.

File

modules/finder_node/finder_node.module
View source
<?php

// $Id: finder_node.module,v 1.1.2.57 2011/02/12 06:55:19 danielb Exp $

/**
 * @file
 * The finder node module.
 */

/**
 * Implements hook_theme().
 *
 * @see hook_theme()
 */
function finder_node_theme() {
  return array(
    'finder_node_result' => array(
      'variables' => array(
        'key' => NULL,
        'result' => NULL,
        'finder' => NULL,
        'form_state' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_finder_base_handlers().
 *
 * @see hook_finder_base_handlers()
 */
function finder_node_finder_base_handlers() {
  return array(
    'node' => array(
      '#title' => t('Node finder'),
      '#module' => 'finder_node',
    ),
  );
}

/**
 * Implements hook_form_alter().
 *
 * @see hook_form_alter()
 */
function finder_node_form_finder_admin_element_edit_alter(&$form, $form_state) {
  $finder =& $form_state['storage']['finder'];
  if ($finder->base == 'node') {
    $element =& $form_state['storage']['finder_element_defaults'];
    $types = finder_node_get_types();
    if ($types) {
      $form['settings']['choices']['node_types'] = array(
        '#type' => 'select',
        '#title' => t('Restrict nodes that can be found to only these content types'),
        '#default_value' => $element->settings['choices']['node_types'],
        '#options' => $types,
        '#description' => t('If no content type is selected, nodes of all types will be displayed.'),
        '#multiple' => TRUE,
        '#size' => min(6, count($types) + 1),
        '#weight' => 10,
      );
    }
    else {
      $form['settings']['choices']['no_types'] = array(
        '#value' => t("<em>There are currently no content types to choose from.</em>"),
        '#weight' => 10,
      );
    }
    $form['settings']['choices']['published'] = array(
      '#type' => 'checkbox',
      '#title' => t('Published'),
      '#default_value' => $element->settings['choices']['published'],
      '#weight' => 90,
      '#description' => t('Only show nodes that are published.'),
    );
    $form['settings']['choices']['per_result'] = array(
      '#type' => 'checkbox',
      '#title' => t('Choices per result'),
      '#default_value' => $element->settings['choices']['per_result'],
      '#weight' => 110,
      '#description' => t('Forces finder to internally process a choice for each potential result node.'),
    );
  }
}

/**
 * Implements hook_finder_fields().
 *
 * @see hook_finder_fields()
 */
function finder_node_finder_fields($finder, $finder_element_id) {

  // specify fields like: 'table.field-name' => t('display title')
  $prefix = t('Node') . ': ';
  $field_array = array(
    'node.nid' => $prefix . t('Nid'),
    'node.vid' => $prefix . t('Vid'),
    'node_type.name' => $prefix . t('Type'),
    'node.language' => $prefix . t('Language'),
    'node.title' => $prefix . t('Title'),
    'users.name' => $prefix . t('Author'),
    'node.status' => $prefix . t('Status'),
    'node.created' => $prefix . t('Created'),
    'node.changed' => $prefix . t('Changed'),
    'node.comment' => $prefix . t('Comment'),
    'node.promote' => $prefix . t('Promote'),
    //'node.moderate' => $prefix . t('Moderate'),
    'node.sticky' => $prefix . t('Sticky'),
    'node.tnid' => $prefix . t('Tnid'),
    'node.translate' => $prefix . t('Translate'),
    //'node_revision.body' => $prefix . t('Body'),

    //'node_revision.teaser' => $prefix . t('Teaser'),
    'node_revision.log' => $prefix . t('Revision log'),
  );
  return $field_array;
}

/**
 * Get an array of content types for use in select forms.
 */
function finder_node_get_types() {

  //node_type_clear();
  $types = node_type_get_types();
  $type_array = array();
  $type_array[''] = t('- all -');

  // this is the 'select none' option in the form
  foreach ((array) $types as $type) {
    $type_array[$type->type] = $type->name;
  }
  if (count($type_array) === 1) {
    return FALSE;
  }
  return $type_array;
}

/**
 * Implements hook_finder_find().
 *
 * @see hook_finder_find()
 */
function finder_node_finder_find($finder, $finder_element_id, $keywords, $mode, $match, $pager) {
  $prequery = array();
  foreach ($keywords as $feid => $keyword_array) {
    $element =& finder_element($finder, $feid);
    $types[$feid] =& $element->settings['choices']['node_types'];
    $published[$feid] =& $element->settings['choices']['published'];

    // restrict by types
    unset($types[$feid]['']);
    if (!empty($types[$feid])) {
      if ($feid == $finder_element_id) {
        $prequery['conditions'][] = (object) array(
          'field' => 'node.type',
          'value' => array_keys($types[$feid]),
          'match' => 'IN',
        );
      }
      else {
        $prequery['conditions']['restrictions'][$feid][] = (object) array(
          'field' => 'node.type',
          'value' => array_keys($types[$feid]),
          'match' => 'IN',
        );
      }
    }
  }

  // restrict to published nodes if required
  if (in_array(1, $published)) {
    $prequery['conditions'][] = array(
      'node.status',
      1,
    );
  }
  $join_ons = array(
    'node_revision' => array(
      'node_revision' => 'node_revision.vid = node.vid',
    ),
    'node_type' => array(
      'node_type' => 'node_type.type = node.type',
    ),
    'users' => array(
      'users' => 'users.uid = node.uid',
    ),
  );
  $base_table = 'node';
  $base_field = 'nid';
  $result = finder_find_query($prequery, $finder, $finder_element_id, $keywords, $mode, $match, $pager, $join_ons, $base_table, $base_field);
  return $result;
}

/**
 * Implements hook_finder_goto().
 *
 * @see hook_finder_goto()
 */
function finder_node_finder_goto($finder, $result) {
  drupal_goto('node/' . $result->nid);
}

/**
 * Implements hook_finder_result().
 *
 * @see hook_finder_result()
 */
function finder_node_finder_result($finder, $keywords, $result_array, $form_state) {
  $output = '';
  foreach ($result_array as $key => $result) {
    $output .= theme('finder_node_result', array(
      'key' => $key,
      'result' => $result,
      'finder' => $finder,
      'form_state' => $form_state,
    ));
  }
  $output .= theme('pager');
  return $output;
}

/**
 * Theme a node finder result.
 *
 * @param $key
 *   The result key.
 * @param $result
 *   A result object.
 * @param $finder
 *   The finder object.
 * @param $form_state
 *   The Forms API form state.
 */
function theme_finder_node_result($variables) {
  extract($variables);
  $output = '';
  $result_node = node_load($result->nid);

  // Open the results wrapper
  $output .= '<div class="finder-node-result-' . $key . '" class="finder-node-result">';

  // Output a teaser of the node
  $build = node_view($result_node, 'teaser');
  $output .= drupal_render($build);

  // Close the results wrapper
  $output .= '</div>';
  return $output;
}

Functions

Namesort descending Description
finder_node_finder_base_handlers Implements hook_finder_base_handlers().
finder_node_finder_fields Implements hook_finder_fields().
finder_node_finder_find Implements hook_finder_find().
finder_node_finder_goto Implements hook_finder_goto().
finder_node_finder_result Implements hook_finder_result().
finder_node_form_finder_admin_element_edit_alter Implements hook_form_alter().
finder_node_get_types Get an array of content types for use in select forms.
finder_node_theme Implements hook_theme().
theme_finder_node_result Theme a node finder result.