0

I wrote this poorly the first time, more succinctly:

I need a custom node.tpl for the same content type in the front page teasers, the node itself, and a views listing of that node in the sidebar on every page. The problem is the front page uses teasers, while the nodes use full view, so I can't edit the markup of the sidebar nodes with just $page or $teaser because they would share the markup with the main content of either page.


The front page uses teasers to display nodes. I want different markup in the sidebar for the "most popular" views block, so I set it to use full view. This works fine on the front page.

The problem is on the individual nodes. I can't target the views block's markup in node.tpl. I made an identical views block with teaser view (because nodes use full view) instead but

<?php if ($page): ?>
<?php if ($view_mode == 'full'): ?>
// Node
<?php endif; ?>
<?php if ($view_mode == 'teaser'): ?>
// Views list
<?php endif; ?>
<?php endif; ?>

doesn't work. The opposite works fine on the front page:

<?php if ($is_front): ?>
<?php if ($view_mode == 'teaser'): ?>
// Node
<?php endif; ?>
<?php if ($view_mode == 'full'): ?> 
// Views list
<?php endif; ?>
<?php endif; ?>

I tried <?php if ($page == 1): ?> as well, though I'm not sure if that's used.

Either way, why I can't just use views for everything is because 1. it breaks the AJAX of the rate module. Which is fine in the sidebar, but not on the front page. And 2. I'm using a link from within a field like this:

    <a class="full-link" href="<?php echo $node->field_download_link['und'][0]['value']; ?>">
        <div class="full-parent-episode-container">
            <div class="full-parent">
                <span><?php print render ($content['field_parent'][0]['#markup']); ?></span>
            </div>
            <div class="full-episode">
                    <span><?php echo $node->title; ?></span>
            </div>
        </div>
    </a>

so I can't have all of the "field-" divs, which break the link. If it matters I'm using $block to get the views list, again, to avoid those "field-" divs.

        <?php $block = module_invoke('views', 'block_view', 'sidebar_popular_full_view-block'); ?>
            <?php print render($block['content']); ?>
3
  • I'm finding this a little hard to follow. Is it just that you are having problems overriding a node.tpl.php file for a node when it is displaying in a view on a node page? Does it only happen if the node in the view is the same as the node on the page you're viewing?
    – rooby
    May 19, 2015 at 0:22
  • @rooby yeah it's difficult to explain. the problem is i can't override the markup of nodes being listed with views via $page, $view_mode == 'teaser', while I can using $is_front, $view_mode == 'full'.
    – James
    May 19, 2015 at 0:27
  • I'm still not entirely understanding however my answer has a few suggestions.
    – rooby
    May 19, 2015 at 0:37

1 Answer 1

1
  1. See template_preprocess_node() for details on how the $page variable is created.

It is this:

$variables ['page'] = $variables ['view_mode'] == 'full' && node_is_page($node);

So it is only for the full view mode and only if you are viewing the node page of the node that is currently being themed.

So something like this will not work because $page will not be true if $view_mode == 'teaser':

<?php if ($page): ?>
  <?php if ($view_mode == 'teaser'): ?>
    // Views list
  <?php endif; ?>
<?php endif; ?>
  1. There is also currently a bug in the views module where if you have a view on a node page and one of the results of that view is the same node as the one whose page you are on then views will break the $page variable in the node template, causing it to always be FALSE.

To see if that is part of your problem try the patch at https://www.drupal.org/node/1574806

  1. I would not theme a view mode differently in different places. For example the teaser would always look the same regardless of what page it was on. Then you just use:

If you are using teaser already for something else then use a different view mode (you could use the entity view mode module for this if you don't want to code it).

2
  • How do I place the new view mode in a block? It's not showing up under display format in views.
    – James
    May 19, 2015 at 0:56
  • Figured it out. It doesn't show up when adding a view but it does when editing. Thank you!
    – James
    May 19, 2015 at 1:39

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.