0

I've got a huge problem with my template preview. For all of my post (page, article and so on), I have not the good region in my page.

My theme is extended Classy.

If I go to see the page (not from the preview), I have the good page. But if I want to see the preview of this page, I've got some missing region (I guess this is a region).

See my page--post-type.html.twig

        {% include '@theme/layout/header.html.twig' %}
    {% include '@theme/includes/page-head.html.twig' %}
    {% include '@theme/includes/page-breadcrumb.html.twig' %}
    {% include '@theme/includes/page-entry.html.twig' %}

    <section id="page-content">
      <div class="container">
        <div class="row"> {{ some displayed data }}
    </div></div></section>
{% include '@theme/layout/footer.html.twig' %}

My included files a working (for both preview and view), but the preview isn't in this page is guess.

The last included block is this for the preview :

BEGIN OUTPUT from 'themes/theme/templates/block/block.html.twig'

And for some reason, after I have this :

BEGIN OUTPUT from 'core/themes/classy/templates/content/node.html.twig'

if it helps, my .theme file :

    /**
 * Implements hook_theme_suggestions_page_alter().
 */
function theme_theme_suggestions_page_alter(&$suggestions, $variables) {
//    $node = \Drupal::routeMatch()->getParameter('node');
    if ($node = \Drupal::routeMatch()->getParameter('node_preview')){
        $node = \Drupal::routeMatch()->getParameter('node_preview');
//        if ($node instanceof \Drupal\node\NodeInterface) {
//            $suggestions[] = 'page__' . $node->bundle() . '_preview';
//            $suggestions[] = 'page__node_' . $node->bundle() . '_preview';
//        }

    } else {
        $node = \Drupal::routeMatch()->getParameter('node');
        if ($node instanceof \Drupal\node\NodeInterface) {
            $suggestions[] = 'page__' . $node->bundle();
            $suggestions[] = 'page__node_' . $node->bundle();
        }
    }

//    dump($suggestions);

}

And I have also this function alstef_preprocess_node() which I took from this : Place block region in node template

I've been stuck for hours, any help you be appreciated.

PS : I'm working on drupal 8 with a custom Theme (here called 'theme').

EDIT : I've just made a dump of my node, and the node is null is i'm on my page--post-type.html.twig or page--post-type-preview.html.twig with the preview (change thanks to suggestions)

1 Answer 1

1

While previewing a node, the node parameter is not "upcasted" from an integer to a node object. But you can easily do this yourself in the preprocess function, just add those lines before:

  $router = \Drupal::routeMatch();
  $node = $router->getParameter('node');
  if (is_numeric($node)) {
    $node = \Drupal\node\Entity\Node::load($node);
  }
  //now you always have "real" node object (instanceOf Drupal\node\NodeInterface -> true), 
  //no matter if preview or normal viewing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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