You are here

function cck_blocks_block_view in CCK Blocks 7

Implements hook_block_view().

@todo Properly test and implement fieldgroup support. See http://drupal.org/node/1720502 and commented out code from http://drupal.org/node/1067708, below). @todo investigate conflicting 'user' token types in token replacement (from patch at http://drupal.org/node/1067708) @todo save entity data in $block for use by contextual links for entity types other than node.

File

./cck_blocks.module, line 98

Code

function cck_blocks_block_view($delta = '') {
  $field = field_info_field($delta);
  $block = array();
  $field_data = FALSE;
  $built_nodes =& drupal_static(__FUNCTION__);
  $nid = 0;

  // First, try to extract a node object form current menu router item
  // using menu_get_object().
  // If that doesnt work, we'll try to extract a term object and
  // then finally a user object.
  $entity_object = menu_get_object('node', 1);
  $entity_type = 'node';
  if (!$entity_object) {
    $entity_object = menu_get_object('taxonomy_term', 2);
    $entity_type = 'term';
    if (!$entity_object) {
      $entity_object = menu_get_object('user', 1);
      $entity_type = 'user';
    }
  }
  if ($entity_object) {
    $block['entity_type'] = $entity_type;
    switch ($entity_type) {
      case 'node':

        /* 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($entity_object->nid) && $display_nodecontent && $field) {
          $nid = $entity_object->nid;

          // build the node using the cck_blocks view mode if that hasn't been done yet
          if (!isset($built_nodes[$nid])) {

            // This alternate method from the patch at #1067708 may be needed for fieldgroup support:
            // node_build_content($entity_object, 'cck_blocks');
            // $built_nodes[$nid] = $entity_object;
            field_attach_prepare_view('node', array(
              $entity_object->nid => $entity_object,
            ), 'cck_blocks');
            entity_prepare_view('node', array(
              $entity_object->nid => $entity_object,
            ));
            $built_nodes[$nid] = field_attach_view('node', $entity_object, 'cck_blocks');
          }

          // look directly for the cck_field in the content array
          if (isset($built_nodes[$nid][$delta])) {
            $field_data = $built_nodes[$nid][$delta];
          }

          // @todo patch from #1067708 used  following Entity based approach, with fieldgroup support:
          // This needs to be tested thorougly before use, see http://drupal.org/node/1720502
          // for work on this in the 6.x branch.  The two methods must be tested / combined.
          //

          //if (isset($entity_object->content[$delta])) {

          //  $field_data = $entity_object->content[$delta];

          //}

          //else {

          //  // cycle through all content data arrays looking for cck groups
          //  // the cck_field may be within a group
          //  foreach ($entity_object as $key => $data) {
          //    if (is_array($data) && (strpos($key, 'group_') == 0) && isset($data['group'][$delta])) {
          //    $field_data = $data['group'][$delta];
          //  }

          //}
        }
        break;
      case 'term':
        if (isset($entity_object->tid) && $field) {
          $built_terms =& drupal_static(__FUNCTION__);
          $tid = $entity_object->tid;

          // build the node in cck_blocks mode if that hasn't been done yet
          if (!isset($built_terms[$tid])) {
            $built_terms[$tid] = taxonomy_term_view($entity_object, 'cck_blocks');
          }

          // look directly for the cck_field in the term array
          // TODO: deal with fieldgroups for Terms
          if (isset($built_terms[$tid][$delta])) {
            $field_data = $built_terms[$tid][$delta];
          }
        }
        break;
      case 'user':
        if (isset($entity_object->uid) && $field) {
          $uid = $entity_object->uid;
          $built_users =& drupal_static(__FUNCTION__);
          if (!isset($built_users[$uid])) {
            user_build_content($entity_object, 'cck_blocks');
            $built_users[$uid] = $entity_object->content;
          }

          // look directly for the cck_field in the term array
          // TODO: deal with fieldgroups for Users
          if (isset($built_users[$uid][$delta])) {
            $field_data = $built_users[$uid][$delta];
          }
        }
        break;
    }
    if ($field_data) {
      $block_content = drupal_render($field_data);
      if (trim($block_content) != '') {

        // Evaluate tokens in a user-defined title, if token module is installed
        // We load tokens according to the entity context: nodes for node pages and terms for term pages
        // TODO: leaving user field tokens out for now, in case they conflict with current user tokens
        // These should be tested and added back in.
        global $user;
        $data = array(
          'user' => $user,
        );
        if ($entity_type != 'user') {
          $data[$entity_type] = $entity_object;
        }
        $title = db_query("SELECT title FROM {block} WHERE delta = :delta", array(
          ':delta' => $delta,
        ))
          ->fetchField();
        if ($title) {
          $block['title'] = token_replace($title, $data);
        }

        /* 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($field_data['#title']);

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

        // Pass entity & field info to anyone who wants to modify stuff.
        switch ($entity_type) {
          case 'node':
            $block['node'] = $built_nodes[$nid];
            $block['nid'] = $nid;
            $block['field'] = $field_data;
            break;
        }
      }
    }
  }
  return $block;
}