I'm working on some theme stuff, and wanted to know if it's possible to test for the condition if Right or Left menu blocks exist to be rendered. Basically, just want to check if any blocks are active, and set to be on the Left or Right side. I searched the site, but didn't easily come up with anything I plugged in. I've been pouring through other themes, for an example, but again didn't find anything.

I also check the Drupal Documentation, but was unable to locate anything. I suppose I could implement an SQL query through the tablespace, but that'd be pretty ugly to do. Is there a cleaner method of determining whether there are any Left or Right blocks that need to be rendered?

TIA.

-Shane

Comments

shane’s picture

I figured the SQL syntax is basically:

$right_result = db_query("SELECT * FROM `blocks` WHERE status = 1 AND region = 1");
  $left_result = db_query("SELECT * FROM `blocks` WHERE status = 1 AND region = 0");

Then simply compare if $right_result or $left_result is greater than zero. If so, then we've got a left/right set to render.

But - still would love a cleaner way - so I'm not imposing a DB Query overhead for each and every single page load for this silly a need...

-Shane

Dries’s picture

I'm afraid it is not cleaner but you could call theme_blocks("left") and capture the data using output buffering. If you captured some data, you know there is at least one block to display.

I guess the clean solution is to make theme_blocks() return it blocks in an associative array with (title, content)-tuples; if the array is empty there are no blocks. In your theme, you can then use this array to render each block.

(Update: just return an array of block objects.)

Feel free to think this through a little and to work up a patch.

ax’s picture

that does this better Only local images are allowed.

shane’s picture

I ultimately ended up doing this:

     // Determines if we have right or left blocks to build - if zero on either
     // then we set our COLSPAN in our primary table layout to 2, instead of 3
     $R = db_query("SELECT * FROM `blocks` WHERE (status = '1' OR custom = '1') AND region = '1'");
     $L = db_query("SELECT * FROM `blocks` WHERE (status = '1' OR custom = '1') AND region = '0'");
     $left_result = db_num_rows($L);
     $right_result = db_num_rows($R);
     ( $left_result > 0 && $right_result > 0 ) ? $COLS = "3" :  $COLS = "2";
     ( $COLS == "3" ) ? $WIDTH = "60%" : $WIDTH = "80%";

COLS sets my table TD fields to either 2 or 3. If 2, main content TD gets 80%, while if 3 COLS (eg left/right and content) then the main content TD is only 60% wide.

Then I test my "$left_results" and "$right_results" to determine if I should actually build the TD sections either in the header for the left columns, or the TD sections in the footer for the right columns.

The net effect is right. Basically if anything is set to left or right under Blocks admin - then it auto-magically builds the container table and does it all correctly.

I'll look forward to the "better" way of doing it in future releases.

-Shane

adrian’s picture

  function box($title, $content, $region = "main") {
    if ($region =='main') {
      $this->nodes[] = new myNode($title, $content  );
    } else {
      $this->blocks[$region][] = new myBlock($title, $content, $region);
    }
  }

  function node($node, $main = 0) {
    if (module_exist("taxonomy")) {
      $terms = taxonomy_link("taxonomy terms", $node);
    }
    $taxo = $this->links($terms);

    $this->nodes[] = new myNode(
      $node->title,
      $node->body,
      $this->links(link_node($node,$main)),
      format_date($node->created,"large")
    );

  }

  function footer() {
    theme_blocks("left",$this);
    theme_blocks("right",$this);

    $this->template->set('leftitem', $this->blocks['left']);
    $this->template->set('boxitem', $this->nodes);
    $this->template->set('rightitem', $this->blocks["right"]);
    echo $this->template->execute() ;
  }

this is mostly due to how phptal (the template engine for the theme i am working on) does it's assignments .. so it generally wouldnt work for other things that echo right now.