You are here

function cck_blocks_block in CCK Blocks 6

Same name and namespace in other branches
  1. 5 cck_blocks.module \cck_blocks_block()

Implementation of hook_block().

File

./cck_blocks.module, line 29

Code

function cck_blocks_block($op = 'list', $delta = 0, $edit = array()) {
  static $built_nodes = NULL;
  $fields = module_invoke('content', 'fields');
  switch ($op) {
    case 'list':
      $blocks = array();
      if (count($fields)) {
        foreach ($fields as $field_name => $field_info) {
          if (variable_get('cck_blocks_' . $field_info['field_name'] . '_block_availability', CCK_BLOCKS_FIELD_BLOCK_DISABLED) == CCK_BLOCKS_FIELD_BLOCK_ENABLED) {
            $blocks[$field_name] = array(
              'info' => t('CCK: @field', array(
                '@field' => t($field_info['widget']['label']),
              )),
              'cache' => BLOCK_NO_CACHE,
            );
          }
        }
      }
      return $blocks;
    case 'configure':

      // add token help, if token module is installed
      if (module_exists('token')) {
        $form = array();
        $form['view']['token_help'] = array(
          '#title' => t('Replacement patterns'),
          '#type' => 'fieldset',
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
          '#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'),
        );
        $form['view']['token_help']['help'] = array(
          '#value' => theme('token_help', 'node'),
        );
        return $form;
      }
    case 'view':
      $block = array();

      // Get the currently viewed node.
      $node = menu_get_object();

      /* Determine whether the node's content is being displayed to the user.
       * It is, when any revision is displayed, inlcuding the latest one.
       * For example, the node's content is not visible to the user when he is
       * selecting revisions for comparison or when he is editing the node.
       */
      $display_nodecontent = !arg(2) || arg(2) == 'revisions' && is_numeric(arg(3));

      /* Only create a block, when a node is loaded, the node's content is displayed
       * to the user and the requested field is available in the fields array
       */
      if (isset($node->nid) && $display_nodecontent && $fields[$delta]) {
        $nid = $node->nid;

        // build the node in cck_blocks mode if that hasn't been done yet
        if (!isset($built_nodes[$nid])) {
          $node->build_mode = 'cck_blocks';
          $built_nodes[$nid] = node_build_content($node, FALSE, TRUE);
        }

        // look directly for the cck_field in the content array
        $cck_field_data = false;
        if (isset($built_nodes[$nid]->content[$delta])) {
          $cck_field_data = $built_nodes[$nid]->content[$delta];
        }
        else {

          // cycle through all content data arrays looking for cck groups
          // the cck_field may be within a group
          $cck_field_data = _cck_blocks_find_data_in_group($built_nodes[$nid]->content, $delta);
        }
        if ($cck_field_data) {

          // check the field content for string only containing whitespaces
          $block_content = drupal_render($cck_field_data);
          if (trim($block_content) != '') {

            // Evaluate tokens in a user-defined title, if token module is installed
            if (module_exists('token')) {
              $title = db_result(db_query("SELECT title FROM {blocks} WHERE delta = '%s'", $delta));
              if ($title) {
                $block['title'] = token_replace($title, 'node', $built_nodes[$nid]);
              }
            }

            /* Use the label of the field as the block's title. Only visible,
             * if the user did not enter a custom title for the block as $block->subject
             *  is replaced by $block->title (if set) in block_list().
             */
            $block['subject'] = t($fields[$delta]['widget']['label']);

            // Set the field's data as the content of the block
            $block['content'] = $block_content;
          }
        }
      }
  }

  // return the generated block. Might be an empty array, if the block is not to be displayed.
  return $block;
}