1

I've created a block using Views that I want to display only on nodes that have a specified value for a taxonomy field (no taxonomy info in the URL). The catch is that I want to display it within the main content (between certain fields), not above or below the main content. I've tried doing this in various ways, but none of my attempts have quite gotten what I needed.

Using one method, I managed to use pre-processing functions and the specific field template for the field (field--body--story.tpl.php) to get the block to show up in right place, but it showed for all the nodes of that content type. I tried evaluating for other fields' values within mytheme_preprocess_field, but those field values don't seem to be available within that function? (They don't show at all using dsm($vars) on the page, I think because the display of that field is hidden.)

Using another method, I used the Context module to get the block to show up only on the nodes of that content type that had that field set to the desired value, but then I could only get it to show above or below the main content, not between specified fields within the main content.

I'm new to Drupal (but am otherwise an experienced web developer), so I'm probably over-complicating this and/or not understanding some major concept. I'm sure there is an elegant way to do this, but I just can't get it (at all, much less elegantly). What am I missing?

0

you can do what i did before :

-1 install computed field module and enable it.

-2 you have to call the specific views that you want in computed field like this way

<?php
$view = views_get_view('your_views_name');
$view->set_exposed_input();
$display_id = 'default';
$view->set_display($display_id);
$view->render();
$entity_field[0]['value'] = ($view->preview());
?>
  • Huzzah! That worked! Thank you so much! I'd heard of the computed field module earlier, but didn't think I needed it yet. It didn't occur to me to use it in this way -- this will be so useful. (BTW, sorry for the delay in responding; got sick and only this morning felt ready to look at a screen again.) – sa3z Apr 17 '12 at 19:34
0

computed field module really great (if you are ok with PHP in the DB). Just wanted to help refine / simplify the example code, and cover a likely use case.

In the computed field to embed a views block just use this in Display Code (PHP)

$display_output = views_embed_view('view_name', 'display_id');

Or to add a common use case, say I have a Field who's value is what I want to pass as an context argument to the view. The inline instructions for Computed Code (PHP) cover how to pull that field value into the computed field. And to pass that to the view use.

$display_output = views_embed_view('view_name', 'block',$entity_field_item['value']);
-1

Did you tried Entity View Attachment (EVA) and adding a PHP Condition (that returns true only on your desired case) to the view? Then you add that whenever the condition is NOT satisfied the view doesn't display anything.

Maybe it would be the way to go.

  • EVA also sounds useful and like it could work, but I decided to use the computed field module instead just because it has a recommended release for D7, and this is for a site already in production (and I'm too much of a Drupal newbie to be taking chances like that). Thanks for the suggestion though! I'll keep an eye on EVA for potential future use when I'm feeling more daring. – sa3z Apr 17 '12 at 19:39
  • Nice, I use EVA on D7 but have never used computed field... so I'll be keeping an eye on it, it may be a better fit for some cases :) Good luck! – versvs Apr 18 '12 at 10:41

Your Answer

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

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