You are here

function nodeblock_block in Nodeblock 6

Same name and namespace in other branches
  1. 5 nodeblock.module \nodeblock_block()

Implementation of hook_block().

File

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

Code

function nodeblock_block($op = 'list', $delta = 0, $edit = array()) {
  $types = node_get_types();
  if ($op == 'list') {
    $blocks = array();
    foreach ($types as $type) {
      if (nodeblock_type_enabled($type)) {

        // Fetch all nodes of this type, excluding translations.
        $result = db_query("SELECT nid, title FROM {node} WHERE type = '%s' AND status = 1 AND (nid = tnid OR tnid = 0)", $type->type);
        while ($node = db_fetch_object($result)) {
          $blocks[$node->nid] = array(
            'info' => $node->title . ' (nodeblock)',
          );
        }
      }
    }
    return $blocks;
  }
  elseif ($op == 'configure') {
    $defaults = variable_get('nodeblock_block_' . $delta, array(
      'teaser' => 0,
      'links' => 1,
    ));
    $form['teaser'] = array(
      '#title' => t('Show only node teaser'),
      '#type' => 'checkbox',
      '#default_value' => $defaults['teaser'],
    );
    $form['links'] = array(
      '#type' => 'checkbox',
      '#default_value' => $defaults['links'],
      '#title' => t('Include node links for "add comment", "read more" etc.'),
    );
    return $form;
  }
  else {
    if ($op == 'save') {
      variable_set('nodeblock_block_' . $delta, array(
        'teaser' => $edit['teaser'],
        'links' => $edit['links'],
      ));
    }
    elseif ($op == 'view') {
      $node = node_load($delta);
      if (!node_access('view', $node)) {
        return;
      }
      $nodeblock_display = variable_get('nodeblock_block_' . $delta, array(
        'teaser' => 0,
        'links' => 1,
      ));

      // 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)) {
        global $language;
        $translations = translation_node_get_translations($node->tnid);
        if ($translations[$language->language]) {
          $node = node_load($translations[$language->language]->nid);
        }
        elseif (!$node->nodeblock_translation_fallback && $node->language !== $language->language) {

          // if no translation was found, the node's language is not the current language,
          // and not using the fallback option return nothing, so the block doesn't display.
          return;
        }

        // otherwise we just use the main node
      }

      // Set a flag so that themes have more context.
      $node->nodeblock = TRUE;
      $block['subject'] = check_plain($node->title);
      $block['content'] = node_view($node, $nodeblock_display['teaser'], FALSE, $nodeblock_display['links']);
      return $block;
    }
  }
}