You are here

taxonomy_image_node_display.module in Taxonomy Image 6

Display taxonomy images in nodes where and when you want them.

File

contributed/taxonomy_image_node_display/taxonomy_image_node_display.module
View source
<?php

/**
 * @file
 *   Display taxonomy images in nodes where and when you want them.
 */

/**
 * Implementation of hook_menu.
 */
function taxonomy_image_node_display_menu() {
  $items = array();

  // Add a tab to the Admin Settings
  $items['admin/settings/taxonomy_image/node_display'] = array(
    'title' => 'Node Display',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'taxonomy_image_node_display_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'description' => 'Configure Taxonomy_image Node_display functionality.',
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );
  return $items;
}
function taxonomy_image_node_display_form() {
  drupal_add_css(drupal_get_path('module', 'taxonomy_image') . '/taxonomy_image.css');
  $form = array();
  $form['taxonomy_image_node_view'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Display taxonomy images on tagged nodes'),
    '#options' => array_map('check_plain', node_get_types('names')),
    '#default_value' => variable_get('taxonomy_image_node_view', array()),
    '#description' => t('Show images in the selected content types.'),
    '#prefix' => '<div class="taxonomy_image_checkboxes">',
    '#suffix' => '</div><div class="clear-block"></div>',
  );
  $form['taxonomy_image_node_view_teaser'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Taxonomy Image in node teaser view'),
    '#default_value' => variable_get('taxonomy_image_node_view_teaser', TRUE),
  );

  // If we have the blocks module, then default to FALSE for page view.
  $form['taxonomy_image_node_view_page'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Taxonomy Image in node page view'),
    '#default_value' => variable_get('taxonomy_image_node_view_page', !module_exists('taxonomy_image_blocks')),
  );
  $form['taxonomy_image_node_view_link'] = array(
    '#type' => 'checkbox',
    '#title' => t('Link displayed Taxonomy Image to taxonomy/term/n page'),
    '#default_value' => variable_get('taxonomy_image_node_view_link', TRUE),
    '#prefix' => '<div class="clear-block"></div>',
  );
  $form['taxonomy_image_node_show_name'] = array(
    '#type' => 'radios',
    '#title' => t('Link style'),
    '#options' => array(
      0 => t('Image only'),
      1 => t('Image and name'),
    ),
    '#default_value' => variable_get('taxonomy_image_node_show_name', 1),
    '#description' => t('For the selected content types, this determines how the taxonomy links will be displayed.'),
    '#prefix' => '<div class="taxonomy_image_radios">',
    '#suffix' => '</div>',
  );
  if (module_exists('imagecache')) {
    $form['taxonomy_image_node_preset'] = array(
      '#type' => 'radios',
      '#title' => t('Imagecache Preset'),
      '#options' => drupal_map_assoc(_taxonomy_image_presets()),
      '#default_value' => variable_get('taxonomy_image_node_preset', variable_get('taxonomy_image_imagecache_preset', 'ORIGINAL')),
      '#prefix' => '<div class="taxonomy_image_radios">',
      '#suffix' => '</div>',
    );
  }
  $form['taxonomy_image_node_view_weight'] = array(
    '#type' => 'weight',
    '#title' => t('Display weight of taxonomy images'),
    '#description' => t('Determines where the images will be displayed. Less than zero will place it before the content; greater than zero, after.'),
    '#default_value' => variable_get('taxonomy_image_node_view_weight', -5),
    '#prefix' => '<div class="clear-block"></div>',
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_nodeapi().
 */
function taxonomy_image_node_display_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  static $view, $view_teaser, $view_page, $preset, $view_link;
  if ($op == 'view') {
    if (!isset($view)) {
      $view = variable_get('taxonomy_image_node_view', array());
      $view_teaser = variable_get('taxonomy_image_node_view_teaser', TRUE);

      // If we are using the block module, then don't default to showing on a page.
      $view_page = variable_get('taxonomy_image_node_view_page', !module_exists('taxonomy_image_blocks'));
      $preset = variable_get('taxonomy_image_node_preset', variable_get('taxonomy_image_imagecache_preset', 'ORIGINAL'));
      $view_link = variable_get('taxonomy_image_node_view_link', TRUE);
    }
    if ($teaser == TRUE && !$view_teaser) {
      return;
    }
    if ($page == TRUE && !$view_page) {
      return;
    }
    $preset = $preset;
    $valid_nodes = array_filter($view);
    if (isset($valid_nodes[$node->type]) && isset($node->taxonomy)) {
      $images = array();
      foreach ($node->taxonomy as $tid => $term) {
        $image = taxonomy_image_display($tid, NULL, $preset, array(
          'wrapper' => FALSE,
        ));
        if ($view_link && $image) {
          $image = theme('taxonomy_image_display', $image, $term);
        }
        $images[] = $image;
      }
      $node->content['taxonomy_image'] = array(
        '#value' => theme('taxonomy_image_display_images', $images),
        '#weight' => variable_get('taxonomy_image_node_view_weight', -5),
      );
    }
  }
}

/**
 * Implementation of hook_theme().
 */
function taxonomy_image_node_display_theme() {
  return array(
    'taxonomy_image_display' => array(
      'arguments' => array(
        'image',
        'term',
      ),
    ),
    'taxonomy_image_display_images' => array(
      'arguments' => array(
        'images',
      ),
    ),
  );
}

/*
 * Theming function to allow overrides at theme level
 */
function theme_taxonomy_image_display($image, $term) {
  $name = variable_get('taxonomy_image_node_show_name', TRUE) ? taxonomy_image_tt("taxonomy:term:{$term->tid}:name", $term->name) : NULL;
  return l($image, 'taxonomy/term/' . $term->tid, array(
    'html' => TRUE,
    'attributes' => array(
      'class' => 'taxonomy-image-links',
    ),
  )) . $name;
}
function theme_taxonomy_image_display_images($images) {
  if (!is_array($images) || count($images) == 0) {
    return NULL;
  }
  $output = '<div class="taxonomy-images">';
  $output .= implode('', $images);
  $output .= '</div>';
  return $output;
}