1

I'm writing a module for Drupal and a bespoke theme. The blocks of the modules either float to the left or the right of the region their in. Currently I do that by hard coding specific css styles, e.g.

#block-myModuleName-myBlockName {float:left; background: red;}
#block-myModuleName-anotherBlockName {float:right; background: blue;}

I want to be able to set left/right (and maybe something else like background colour). So I've created an admin form that saves a persistent variable, such as variable_get('myBlockName-left', 0).

I can use that in the block content. I can wrap the content in a div that is based on the var such as <div class="myModulesetToLeft">, but that will not include the title, and it's a bit messy because it's deeper into the DOM of the block.

Do I need to create a template file for blocks? But that code will need to know what block it is displaying, so that it can check the right variable to know which class to add to the block div.

2 Answers 2

1

I'd probably just use the Block Class module and assign a classes like block-left and block-right.

Block Class allows users to add classes to any block through the block's configuration interface. By adding a very short snippet of PHP to a theme's block.tpl.php file, classes can be added to the parent <div class="block ..."> element of a block. Hooray for more powerful block theming!

1

You should really do three things:

  1. Instead of using an admin form and variable_set / variable_get, you should use a theme settings form.

  2. You would then make a template_preprocess_block in your theme, which reads a theme variable with theme_get_setting and sets something in $variables.

  3. In your block template, use the variable that you set.

If you can accomplish all of this with just CSS classes, then in your preprocess, you can just tack classes onto $variables['classes_array'][] and skip the third step.

2
  • That sounds like I would have one variable set for all blocks. Or will one of those steps add the variable to the configuration of every block? e.g. I make the variable var_colour, and I can specify a value for block_1, block_2, etc.
    – Gregory
    Jun 14, 2012 at 12:58
  • @TheG, I outline the basic process. You would need to adapt it for your particular situation. In your case, I think you would need to be creative with the theme variables. I suspect you could have one each for left and right, and then assign the IDs that get floated. Off the top of my head, I am not sure how I would handle the color thing.
    – mpdonadio
    Jun 14, 2012 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.