0

What I would like to do

In Durpal 7, I am using a block view to display our manufacturers name above our node titles. We added a custom region above content, and that is working fine. However, I would like the manufacturer to be display inline with the node title.

How I handled it in Drupal 8

In this related question I asked about a Drupal 8 solution.The drupal 8 solution was a little simpler in that we migrated all manufacturer fields into 1 manufacturer field. which made things much easier to code. In my drupal 7 site, the manufacturer fields are separate making it a little messier to handle with code in that there would need to be an instance of the code for every manufacturer/node combo.

My question

For drupal 7 is it possible to render a block view as an inline prefix to a node title? Placing a block view in this circumstance seems like it would be a much simpler and more front end Drupal approach. Especially since I can have all of the manufacturer fields in the one block view, and they will render according to Taxonomy term ID contextual filter.

6
  • Is it about inline as in you can do that with CSS maybe? Then maybe have a custom block that gets the current node, gets the title and returns it? And then place that block next to manufacturer block and display them inline with just CSS?
    – leymannx
    Dec 24 '19 at 20:53
  • That would be fine but I would have to remove the default drupal node title from the template then? Dec 24 '19 at 22:13
  • Actually that could be accomplished with one view, I just need a way not to render the node title. Tips on that? Dec 24 '19 at 22:20
  • 2
    You can use the Exclude Node Title module to hide the page title, or do it yourself with template_preprocess_page(). I think the combination of a custom view block and the Exclude Node Title module is a good one. It's easy to implement, understand, document, and maintain. Dec 25 '19 at 4:32
  • @runswithscissors – You should drop a proper answer.
    – leymannx
    Dec 25 '19 at 8:35
0

So this is what I did, in case anyone wants to accomplish the something similar.

You first have to add a new region just above the content region in your MY_THEME.info file which in my case was:

regions[manufacturer_prefix_title]    = Manufacturer Prefix Title

Then I overwrote the page.tpl.php which contained:

  <?php if ($title): ?>
    <h1><?php print $title; ?></h1>
  <?php endif; ?>

to each node instance I needed page--MY-NODE.tpl.php which now contains:

  <?php if ($title): ?>
    <?php print render($page['manufacturer_prefix_title']); ?>
  <?php endif; ?>

(The above required a preprocess_page hook.)

Then I created a block view, with the contextual filter set to NID. I also added filter criteria to get only the desired content types.

I added the 3 Content: Manufacturer fields, ticked exclude from display on each, and ticked rewrite the output of this field on each so that I could use the replacement pattern for those fields and wrap each one in the span class I wanted:

<span class="MY-CLASS">[field_MY_FIELD]</span>.

I also made sure to tick hide if empty under no results behavior so that the span class would not get rendered when the field was empty, as these fields are not required.

I then added the Content: Title field once, and ticked rewrite the output of this field and used replacement patterns for each of the manufacturer fields, leaving one space in front of the title:

[field_MY_FIELD_1][field_MY_FIELD_2][field_MY_FIELD_3] [title]

Since the contextual filter was set to NID, the title will only grab the appropriate taxonomy reference field, as only one is allowed per node.

Then all that was left was to simply place the new view block @admin/structure/block into the new region that was created in the MY_THEME.info file. I also configured the block to only show on the same content types that i defined in the filter criteria of the block view.

Hope this helps someone! :)

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.