You are here

function nodeblock_block_view in Nodeblock 7

Implements hook_block_view().

File

./nodeblock.module, line 769
Enables use of specified node types as custom blocks.

Code

function nodeblock_block_view($delta = '') {
  global $language_content;
  if (!_nodeblock_table_exists()) {
    return;
  }
  $node = nodeblock_load_nodeblock($delta);
  if (!$node) {
    return;
  }

  // If the node type is translatable, try to load the node with the appropriate
  // language from the translation set.
  if (module_exists('translation') && translation_supported_type($node->type)) {
    $translations = translation_node_get_translations($node->tnid);
    if (!empty($translations[$language_content->language])) {
      $node = node_load($translations[$language_content->language]->nid);
    }
    elseif (!$node->nodeblock['translation_fallback'] && $node->language != $language_content->language && $node->language != LANGUAGE_NONE) {

      // If no translation was found, and not using the fallback option
      // return nothing, so the block doesn't display.
      return;
    }

    // Otherwise we just use the main node.
  }
  if (!$node || !nodeblock_type_enabled($node->type) || !isset($node->nodeblock) || !node_access('view', $node)) {
    return;
  }

  // Make sure we work on a copy of the node object.
  $node_clone = clone $node;
  $view_mode = $node_clone->nodeblock['view_mode'];
  $node_clone->nodeblock['shown_as_block'] = TRUE;
  if ($view_mode == 'node_block_default') {
    $view_mode = nodeblock_type_view_mode($node_clone->type);
  }
  $langcode = isset($node_clone->nodeblock['real_block_title'][$language_content->language]) ? $language_content->language : $node->language;
  $title = $node_clone->nodeblock['real_block_title'][$langcode];
  $block = array(
    'subject' => $title == '<none>' ? '' : check_plain($title),
    'content' => node_view($node_clone, $view_mode),
    '#node' => $node_clone,
  );
  $node_link = $node_clone->nodeblock['node_link'];
  if ($node_link == 'node_block_default') {
    $node_link = nodeblock_type_node_link($node_clone->type);
  }
  if (!$node_link && isset($block['content']['links']['node'])) {
    unset($block['content']['links']['node']);
    unset($block['content']['links']['translation']);
  }
  $comment_link = $node_clone->nodeblock['comment_link'];
  if ($comment_link == 'node_block_default') {
    $comment_link = nodeblock_type_comment_link($node_clone->type);
  }
  if (!$comment_link && isset($block['content']['links']['comment'])) {
    unset($block['content']['links']['comment']);
  }
  return $block;
}