1

I have been unsuccessful in accomplishing the following:

I am using the Nodeblocks module and trying to follow its documentation for using it with a custom content type. My custom content type machine name is content_block. I want a client to be able to create a new content_block and then be able to place it wherever he wants using the Admin > Blocks page.

The content_block content type has these fields (machine names - purpose):

  • title - Title of the block (nodeblock) that will be seen on the Blocks administration page. Will be hidden from output.
  • field_headline_level - stores a value like, "h2", "h3", "h4", etc. To be used to customize the HTML Headline tag. Will be hidden from output.
  • field_headline - The actual words for the headline
  • field_content - Content to be displayed below the headline

I've been trying to follow the Nodeblocks documentation (linked above) as well as the Drupal 7 Template Suggestions but I can not get it to work. I keep running into nested template problems, such as the default block template wrapping around the node type template and getting double headlines. I also can't figure out the correct syntax to display the various content_block fields.

Can anyone out there help me out or point me to a "Node Templates for Dummies" article? I've done lots of searches on Google, but can't find anything very helpful.

1

Figured it out by getting my head around what Drupal calls "Template Suggestions". The Theme Developer Module helped.

For my particular use case I had to create a template file called block--nodeblock.tpl.php to override the default block output. I basically stripped everything out of the default except the actual content so that this was all that was left: <?php print $content ?>.

Then I made a node--content_block.tpl.php template to override this particular content type's display the way I wanted it. I had to add some trim() methods to strip out some leading spaces that Drupal insisted on inserting.

<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>

    <?php if($content['field_headline']): ?>
        <?php print render($title_prefix); ?>
        <<?php print trim(render($content['field_headline_level'])) . ' ' . $title_attributes; ?>><?php print trim(render($content['field_headline'])); ?></<?php print trim(render($content['field_headline_level']));?>>
        <?php print render($title_suffix); ?>
    <?php endif ?>

    <div class="content"<?php print $content_attributes; ?>>
        <?php print trim(render($content['field_content'])); ?>
    </div>

</div>

The last thing I had to do was create a template to strip out all the extra tags that the Drupal default field comes with. I did so with this file, named field--content_block.tpl.php:

<?php foreach ($items as $delta => $item) : ?>
      <?php print render($item); ?>
<?php endforeach; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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