I got this working in D7 and I can set up a custom node template for a content type using node--my-content-type.tpl.php but I can't get it to work for say page--my-content-type.tpl.php or page--my-path.tpl.php in Backdrop

 

I created a sub theme for basis (haven't tried with out a sub theme but I guess I should) and this is what I got in my template.php file which I just copied from my D7 site and adjusted.

 

<?php
/**
 * @file
 * Foss Basis preprocess functions and theme function overrides.
 */

function foss_basis_preprocess_page(&$variables, $hook) {
  if(isset($variables['page']['content']['system_main']['no_content'])) {
    unset($variables['page']['content']['system_main']['no_content']);
  }

   // Page template suggestions based off of content types
   if (isset($variables['node'])) { 
                $variables['theme_hook_suggestions'][] = 'page__type__'. $variables['node']->type;
                $variables['theme_hook_suggestions'][] = "page__node__" . $variables['node']->nid;
   }
   
   // Page template suggestions based off URL alias
   if (module_exists('path')) {
    $alias = backdrop_get_path_alias(str_replace('/edit','',$_GET['q']));
    if ($alias != $_GET['q']) {
      $template_filename = 'page';
      foreach (explode('/', $alias) as $path_part) {
        $template_filename = $template_filename . '__' . $path_part;
        $variables['theme_hook_suggestions'][] = $template_filename;
      }
    }
  }
}

Most helpful answers

https://api.backdropcms.org/layouts says:

Additional .php processing files
If a layout template requires additional conditional logic and data processing of the output, a template processing file should be included rather than performing complex logic in the .tpl.php files. This generally means it is used to hold preprocessors for generating variables before they are merged with the markup inside the layout .tpl.php file. Custom functions, overriding layout functions or any other customization of the raw output should also be done here.

There is also an example:

; Include file for special preprocessing of this layout's variables.
file = three_three_four_column.php

So I think you can "tailor a content type via HTML/PHP code" in a Backdrop way.

I've been working with Backdrop over a year and a half. At the beginning I was also annoyed that some techniques with which I was accustomed to Drupal 7 did not work as before, but gradually got used to the new way and now I find that it is more comfortable and filled with many new possibilities.

 

Instead of creating custom page template for specific content type, you may create new (or use existing) layout that is different from Default Layout ( Default Layout renders all pages by default).

Go to /admin/structure/layouts

Click Add layout

Give Name for that Layout, for example "News content type"

Choose Layout template

Specify  Path (node/%)

Click Add visibility condition

Select Node: Type

... and you will have different layout for nodes with this content type.

You will see that there are many other useful visibility conditions:

  • Front page
  • Node: NID
  • Site language
  • URL Path
  • User account: UID
  • User: Permission
  • User: Role

I think this way of customization is quite more powerful than D7 way with page--my-content-type.tpl.php or page--my-path.tpl.php.

More readings:

https://backdropcms.org/user-guide/layouts-and-templates

https://backdropcms.org/user-guide/deep-dive-advanced-layout-options

 

 

 

 

The page.tpl.php template in Backdrop is equivalent to what was named html.tpl.php in Drupal 7, and I don't think it was possible to get a html--page.tpl.php there, either.

If you are attempting to port a Drupal 7 theme, what you probably want instead is to use layout.tpl.php -- or better yet -- use the layout system in Backdrop as it was intended, and use the UI to select a different page layout (and block placements!) for each content type.

Comments

drop's picture

The page.tpl.php template in Backdrop is equivalent to what was named html.tpl.php in Drupal 7, and I don't think it was possible to get a html--page.tpl.php there, either.

If you are attempting to port a Drupal 7 theme, what you probably want instead is to use layout.tpl.php -- or better yet -- use the layout system in Backdrop as it was intended, and use the UI to select a different page layout (and block placements!) for each content type.

If I understood correctly, the question is: how to use the template system to customize a content type?

I have the same question. In D7 it would be page--my-content-type.tpl.php

Can this be done in Backdrop, or is the layout system the only option. Also, is there documentation on how to override a layout with tpl files?

I found this:
https://api.backdropcms.org/layouts

Seems to be for creating layouts (as opposed to customizing existing ones via theme tpl). Following this idea, if I needed a fairly custom PHP/HTML layout for a particular content type, would the right approach be to create a new layout for backdrop and put all my custom PHP code to handle the content type within the layout files?

drop's picture

...the question is: how to use the template system to customize a content type?

The "template system" can be used in many ways to customize a content type. Node templates can be overridden by type, Field templates can be overridden by field name or field type. But see more below about how to change the layout of the whole page (what page.tpl.php did in Drupal 7).

In D7 it would be page--my-content-type.tpl.php

When converting a Drupal 7 theme to backdrop, any file that was previously a variant of page.tpl.php becomes it's own layout. Rename page--my-content-type.tpl.php to layout--shape-one.tpl.php and page--my-other-content-type.tpl.php to layout--shape-two.tpl.php. (Each layout should be placed into it's own directory under /layouts, and will also need it's own .info file to specify the regions, and perhaps a .css file to define the layout, or a .php file for preprocessing.)

In backdrop, the layout template file is determined by which layout was selected (in the UI) for that content type. If you choose the Moscone layout for the Page content type, then the template you would override is layout--moscone.tpl.php. However, it's important to note that modifying the Moscone template file will modify it for every page where the Moscone layout is used, not only for nodes of type Page.

Can this be done in Backdrop, or is the layout system the only option.

Yes this can be done in Backdrop. How it's done In Backdrop, however, requires a bit of a mental-model shift, as the layout system is responsible for rendering every single page on your site. It would take a whole lot of work to "avoid using the layout system" but you could add a node-type specific template suggestion, by doing something like the following in your theme's template.php file:

/**
 * Prepares variables for layout templates.
 */
function THEMENAME_preprocess_layout(&$variables) {
  if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
    $layout_name = $variables['layout_info']['name'];
    $contexts = $variables['layout']->getContexts();
    $node = $contexts[1]->data;
    $suggestion = 'layout__' . $layout_name . '__' . $node->type;
    $variables['theme_hook_suggestions'][] = $suggestion;
  }
}

Also, is there documentation on how to override a layout with tpl files?

Not specifically, but overriding layouts shouldn't be any different than overriding other items on the page like nodes, comments, or views.

The default template file for a layout is layout.tpl.php (located in the core layout module), but any other layout has a variant of that base template named layout--variant-name.tpl.php (and that file is located in the layout's own directory). Copy these files to your theme to override them. 

if I needed a fairly custom PHP/HTML layout for a particular content type, would the right approach be to create a new layout for backdrop and put all my custom PHP code to handle the content type within the layout files?

Yes. In most cases the "custom PHP code to handle the content type" could be replaced with blocks within a standard layout, but there are cases where a custom layout is the best tool for this job :)

Instead of creating custom page template for specific content type, you may create new (or use existing) layout that is different from Default Layout ( Default Layout renders all pages by default).

Go to /admin/structure/layouts

Click Add layout

Give Name for that Layout, for example "News content type"

Choose Layout template

Specify  Path (node/%)

Click Add visibility condition

Select Node: Type

... and you will have different layout for nodes with this content type.

You will see that there are many other useful visibility conditions:

  • Front page
  • Node: NID
  • Site language
  • URL Path
  • User account: UID
  • User: Permission
  • User: Role

I think this way of customization is quite more powerful than D7 way with page--my-content-type.tpl.php or page--my-path.tpl.php.

More readings:

https://backdropcms.org/user-guide/layouts-and-templates

https://backdropcms.org/user-guide/deep-dive-advanced-layout-options

 

 

 

 

> I think this way of customization is quite more powerful than D7

Yes absolutely, in terms of the point and click UI. But there will still be limitations. In code you can essentially do anything.

So what happens if what I want to do cannot be done within the new and more powerful layouts? What are my options to custom tailor a content type via HTML/PHP code?

https://api.backdropcms.org/layouts says:

Additional .php processing files
If a layout template requires additional conditional logic and data processing of the output, a template processing file should be included rather than performing complex logic in the .tpl.php files. This generally means it is used to hold preprocessors for generating variables before they are merged with the markup inside the layout .tpl.php file. Custom functions, overriding layout functions or any other customization of the raw output should also be done here.

There is also an example:

; Include file for special preprocessing of this layout's variables.
file = three_three_four_column.php

So I think you can "tailor a content type via HTML/PHP code" in a Backdrop way.

I've been working with Backdrop over a year and a half. At the beginning I was also annoyed that some techniques with which I was accustomed to Drupal 7 did not work as before, but gradually got used to the new way and now I find that it is more comfortable and filled with many new possibilities.

 

Ok so the layut files control page.tpl.ph which I would have modified per path in Drupal before and I can still modify node.tpl.php which I seem to be able to still override like in Drupal?

drop's picture

Ok so the layout files control page.tpl.php

Well, not exactly. The layout files control layout.tpl.php (and it's variants). All layout files are replacements for the single file that was named page.tpl.php (and it's variants) in Drupal 7.

The goal is to have many options for the blocks-and-regions for any page. Layout module handles when to deliver which set of blocks-and-regions. Which set can be determined by path, yes, but also by things like content type, user role, taxonomy vocabulary or term, etc.

Backdrop also has a file named page.tpl.php, (which is all that makes your assumption above incorrect). Below is the contents of that file:

<!DOCTYPE html>
<html<?php print backdrop_attributes($html_attributes); ?>>
  <head>
    <?php print backdrop_get_html_head(); ?>
    <title><?php print $head_title; ?></title>
    <?php print backdrop_get_css(); ?>
    <?php print backdrop_get_js(); ?>
  </head>
  <body class="<?php print implode(' ', $classes); ?>"<?php print backdrop_attributes($body_attributes); ?>>
    <?php print $page; ?>
    <?php print $page_bottom; ?>
    <?php print backdrop_get_js('footer'); ?>
  </body>
</html>

This file replaces the file that was named html.tpl.php in Drupal 7.

which I would have modified per path in Drupal 

In Backdrop, you'll build a Layout per-path in the Layouts User Interface. Then you'll override the template file for the layout-template you've selected.

For example, if you want to use a three-column layout-template at '/blog` you'll create a new Layout at the path '/blog' and select "Geary" as the layout-template to use. Then, if you need custom code in the template file, you'd copy layout--geary.tpl.php into your theme, and override that file as needed. 

and I can still modify node.tpl.php which I seem to be able to still override like in Drupal?

Yes, exactly. And most other templates should be nearly the same as well.