How to use text from a block view as a prefix to a node title 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.
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:

  &lt;?php if ($title): ?&gt;
    &lt;h1&gt;&lt;?php print $title; ?&gt;&lt;/h1&gt;
  &lt;?php endif; ?&gt;


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

  &lt;?php if ($title): ?&gt;
    &lt;?php print render($page['manufacturer_prefix_title']); ?&gt;
  &lt;?php endif; ?&gt;


(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:

&lt;span class="MY-CLASS"&gt;[field_MY_FIELD]&lt;/span&gt;. 

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! :)