To add an ID in your block, install Block Class then modify the file block.tpl.php:

<div id="block-<?php print $block->module .'-'. $block->delta; ?>" class="clear-block block block-<?php print $block->module ?>">
  <div id="<?php print block_class($block); ?>"> <!-- Open Id <div id="IdName"> -->
    <?php if (!empty($block->subject)): ?>
    <h2><?php print $block->subject ?></h2>
    <?php endif;?>
    <div class="content"><?php print $block->content ?></div>
  </div> <!-- Close Id </div> -->
</div>

Comments

Todd Nienkerk’s picture

provinciadicuneo: The Block Class module can indeed be used to provide an ID instead of a class. However, it's preferable from a theming standards standpoint to stick to the ID already provided by block.tpl.php. See the first line:

<div id="block-<?php print $block->module .'-'. $block->delta; ?>" [...]

This will create an ID pattern of block-MODULE_NAME-DELTA_VALUE. For example, user-generated blocks (i.e., custom blocks created in the Admin > Site Building > Blocks interface) use the ID pattern block-block-2, where "2" is the delta value (unique identifier) for that block.

You can use the ID pattern described above to style your block with CSS.

Also, I should note that the technique you describe may cause problems in the long run. If more than one block is assigned the same ID, you run the risk of having two elements with the same ID appear simultaneously on a page. This will result in invalid markup and may cause problems in some browsers.

Todd Nienkerk’s picture

Component: User interface » Documentation
klonos’s picture

Thanx for sharing this info. I'd like to add these three links here:

Create block id attributes based on block title
Give your Drupal blocks a more descriptive HTML ID attribute
Core Block CSS IDs (...shows how core block IDs have changed in D7 compared to the ones in D6)

I would like to kindly suggest this to become a feature of this module, but I'm not changing this issue's category from 'task' though. Thanx in advance for considering.

PS: ...perhaps my request belongs to a separate, new module. A 'Custom block IDs' perhaps? ;)

klonos’s picture

Title: Block Id » Block ID

@Todd:

Also, I should note that the technique you describe may cause problems in the long run. If more than one block is assigned the same ID, you run the risk of having two elements with the same ID appear simultaneously on a page. This will result in invalid markup and may cause problems in some browsers.

...unless there's an extra check to see if there's an ID with the same value in the document of course. In that case the $block->delta could be concatenated to the new id to avoid duplicate IDs. In D7 this could be done by checking the entered ID value against the core blocks' IDs + against all enabled contrib blocks' IDs before saving the new block ID.

Todd Nienkerk’s picture

Assigned: provinciadicuneo » Unassigned
Status: Closed (works as designed) » Closed (duplicate)

@klonos: This issue is a duplicate of #863554: Add ability to set a block's CSS ID.

Anonymous’s picture

Issue summary: View changes

this module may be helpful, by using this module you may add separate block id.

https://www.drupal.org/project/block_id