You are here

function nodeblock_block_info in Nodeblock 7

Implements hook_block_info().

File

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

Code

function nodeblock_block_info() {
  $blocks = array();
  if (_nodeblock_table_exists()) {
    $types = node_type_get_types();

    // Deselect types that aren't nodeblock enabled.
    foreach ($types as $key => $type) {
      if (!nodeblock_type_enabled($type->type)) {
        unset($types[$key]);
      }
    }
    if ($types) {

      // Fetch all nodes of the selected types, excluding translations.
      $request = db_select('node', 'n')
        ->fields('n', array(
        'title',
        'nid',
        'type',
      ))
        ->condition('n.type', array_keys($types), 'IN')
        ->condition(db_or()
        ->where('n.nid = n.tnid')
        ->condition('n.tnid', 0));
      $request
        ->addJoin('INNER', 'nodeblock', 'nb', 'nb.nid = n.nid');
      $request
        ->fields('nb', array(
        'machine_name',
      ))
        ->condition('nb.enabled', 1);
      $results = $request
        ->execute();
      foreach ($results as $node) {

        // Check if someone different then the node module
        // implements hook_node_grants or hook_node_access.
        $no_node_access_hooks = count(module_implements('node_grants')) == 0 && count(module_implements('node_access')) == 1;
        $blocks[$node->machine_name] = array(
          'info' => t('(Nodeblock: @type): @title', array(
            '@type' => node_type_get_name($node),
            '@title' => $node->title,
          )),
          'nid' => $node->nid,
          'cache' => variable_get('nodeblock_dangerous_force_block_caching', $no_node_access_hooks) ? DRUPAL_CACHE_PER_ROLE : DRUPAL_NO_CACHE,
        );
      }
    }
  }
  return $blocks;
}