2

How do you allow a content type to publish an option that changes views options of that page?

I have created 2 views slideshows block with different settings - I want the user to choose how to view the images on any page of a certain content type BUT DONT WANT TO SAVE THE PAGES TWICE - the images must be shown programmatically depending on which view option is chosen.

I saw this tutorial on rendering views via a tpl.php file...

I do not know how to create and where to place this tpl.php file -- Could this be implemented as a module instead? What do you place in the $args function - are there any useful examples?

3

Add a new field to your content type:

  • Name: Pick a view
  • Type: boolean
  • Widget: [doesn't matter]

Then add this code to your block or node or page tpl.php file.

<?php

if ($node = menu_get_object()) {
  // Get the nid
  $nid = $node->nid;

$node = node_load($nid);
$wrapper = entity_metadata_wrapper('node', $node);
$field_value = $wrapper->field_pick_a_view->value();

if ($field_value == 1) {
  print views_embed_view('my_view', 'block_1'); 
  } else { 
  print views_embed_view('my_view', 'block_2');
  }
}

?>
  • replace view_name with your view name
  • replace block_1 or block_2 with your machine name

So if your field is check-marked, it prints view 1. If it's not checked-marked, it prints view 2.

  • Thanks! - I have added this to my /modules/system/page.tpl.php file - is that correct? Do I change anything in the boolean field settings? Also, in the manage display I have changed the label to 'above' and format to 'default' – user54721 Mar 3 '16 at 11:07
  • @ldvt5 incorrect, as when you update Drupal core your file will get overriden. You need to put it in /sites/all/themes/[your theme]/templates folder. If there is no page.tpl.php file there, copy over the one from system and add the changes. – No Sssweat Mar 3 '16 at 11:12
  • If you want you could create an empty block, then create a tpl file for it block--block--[block-id].tpl.php (you can get the block id number from the configure block url). Example: admin/structure/block/manage/block/8/configure would be block--block--8.tpl.php – No Sssweat Mar 3 '16 at 11:20
  • I have added the code to the my theme's page.php.tpl file but no luck (I have made sure the views and block names are correct) I have also tried to add that code to a blank block using the php filtering in the WYSIWYG (drupal.org/node/1046700) If I go ahead with the creation of 'block--block--[block-id].tpl.php' do I place it under the same /sites/all/themes/[your theme]/templates folder? Anything else I can check to troubleshoot? – user54721 Mar 3 '16 at 11:49
  • /sites/all/themes/[your theme]/templates folder yep same place, all your templates should go there. Anything else I can check to troubleshoot? try using this code alone print views_embed_view('my_view', 'block_1'); to see if you can print the view. Also, are you using a contextual filter in your view? – No Sssweat Mar 3 '16 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy