Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
Todd Nienkerk CreditAttribution: Todd Nienkerk commentedprovinciadicuneo: 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:
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 patternblock-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.
Comment #2
Todd Nienkerk CreditAttribution: Todd Nienkerk commentedComment #3
klonosThanx 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? ;)
Comment #4
klonos@Todd:
...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.Comment #5
Todd Nienkerk CreditAttribution: Todd Nienkerk commented@klonos: This issue is a duplicate of #863554: Add ability to set a block's CSS ID.
Comment #6
Anonymous (not verified) CreditAttribution: Anonymous commentedthis module may be helpful, by using this module you may add separate block id.
https://www.drupal.org/project/block_id