Hoping someone can set me on the right path.

I have a handful of instances in a D7 site where I'm rendering the entire content of a region inside a node template. I've been using something from the context module to do it inside a preprocess_node function:

// Grab sidebar content
if ($plugin = context_get_plugin('reaction', 'block')) {
  $vars['sidebar_first'] = $plugin->block_get_blocks_by_region('sidebar_first');
  $vars['sidebar_second'] = $plugin->block_get_blocks_by_region('sidebar_second');

What I'm achieving here is being able to mix region level content and template level content inside the same structure. So like, the "sidebar" region might look something like:

<div class="wrapper">
  <div class="main-stuff">
    ... output main page content here ...

  <div class="sidebar-stuff">

    ... output blocks placed in the layout at the system level ...

    ... output node-specific stuff here as well ...


I looked very quickly at the Field Blocks feature and while it's cool I'm not sure I can "hand up" my individual fields to the layout system, because the "node-specific stuff" in my pseudohtml up there might require multiple fields to work, or there's just extra custom logic involved that depends on what's going on inside the page. (So, like, I might have a full hero-esque chunk of content above ".wrapper" that exists inside the node, pulling in multiple fields from the node, depending on if certain specific conditions are met. And then there might just be a whole bunch of stuff that comes after ".wrapper" as well. Feels like to feed everything up to the layout I'd have to code up everything as custom module blocks, which feels ... wrong?)

I went ahead and pulled in the D7 Context module (and the D7 ctools) and got the module to at least turn on,  but I haven't successfully made my preprocessor snippet above work yet. And that all feels pretty heavy-handed anyway. I've also looked at a couple forum topics (one, two), but I'm not sure the answer's in there.

Any ideas? Happy to adapt to new ways of doing this...



indigoxela's picture

Hi darby3,

it might be that I don't fully get what you're after, but I suspect, you don't even need any contrib module for that.

You should be able to achieve what you need with the core Layout module.

Layouts can have contexts and visibility conditions. A layout with the path node/% for instance has all individual fields available as blocks - so these can get placed in any region.

You can also put other nodes as blocks in any layout.

Layouts are very powerful, but because of this power aren't the easiest thing to understand when coming from other CMSes.

I think what I'm running into is—if I have three fields that I want to render as a single unit in a separate region, I have to hand them up as three separate blocks into the layout system. And then I'd have to "theme" them back together inside the layout system, yes?

Right now inside a node template in D7 we're set up to render out the values of most fields without any HTML wrappers, and then all the HTML is provided by the theme template files. This allows me to match up with our existing pattern library easily. And I can just "pull in" additional blocks that the D7 layout system equivalent provides (typically using Context module). Those additional blocks are more an exception, I guess.

If I start sending fields out to the layout system as blocks...then I think I have to start writing individual template files for every field I need to do that for inside the theme, and then the layout is going to be taking on some of the HTML I really think of as more theme-specific.

Again, I might not be understanding how this works yet, but I think I'm trying to avoid replacing my current, fairly simple solution, with something that feels way more complex, out of the gate.

indigoxela's picture

It might depend...

Do you want to get rid of the wrappers provided by the Layout module (that's easy), or do you also want to get rid of all field markup (the divs provided by the field module)?

To get rid of the Layout wrappers, open the block dialog, then expand the "Style settings", switch the Style to "dynamic" and turn off the wrappers you don't need.

Both. The theme and pattern library I've developed provides all the HTML and styling, so today, in D7, I strip out almost everything before it hits the node--xyz.tpl.php files (and paragraph template files, and field collection files, and and and...).

To strip out field markup today in D7 we have Semantic Fields installed, which provides a "naked" option. In experimenting with porting the theme to Backdrop I've already realized that, since basically every single field is set to "naked," I can get rid of that module and use a simple template function in the theme to output the field value with no markup. So that part's already been simplified. :D

I'm imagining to get rid of layout wrappers maybe there's a way to set it to default to the behavior you describe above? Otherwise I guess I would have to go in and set that on every field on the content type.

Maybe I'm just not a fan of having to break up my template file in the theme; right now I have all my "pieces" of the page together, for certain well-structured content types. For certain pages if I have to hand up fields like this to the layout system I think I'm going to be breaking up large numbers of fields into different layout regions. Maybe it won't be that bad, and there's advantages I haven't recognized it, but if there's an easy way to keep the same basic solution I have today, that would make moving the theme over much easier.