0

I have a Drupal 7 view 'page' with various fields. One of those is a taxonomy term. I also have a view 'block' that has a Contextual filter set to the same term.

(I'm trying to filter the blocks results by the term in the page view)

I created a new block (under 'blocks') and added custom php to return and display the view.

If I hard code the term id with echo views_embed_view('events','block','1'); it works fine.

My question is; how do I get the value of a field in the page view with php. I've seen lots of example of how to get it from a normal node but not from another view.

Thanks in advance

jeff
  • 87
  • 13

2 Answers2

0

In Drupal, blocks handle contextual filters internally. If you use the following code to print your block, you shouldn't have to worry about passing the term argument, provided you're on the term page or that you explicitly set how to retrieve the term id within the Views Contextual filters parameters.

As seen in a previous answer How to insert a block into a node or template in Drupal 7?
Only exception is that from PHP 5.4, you can only pass a variable to drupal_render()

$block = block_load('views', 'block_name');      
$output = _block_get_renderable_array(_block_render_blocks(array($block)));        
print drupal_render($output);

If you're not on a taxonomy term page or if you can't get the views to retrieve the term id then you should look at your problem from a different perspective, namely find a way to retrieve the term tid yourself and pass it to:

views_embed_view()

A good tool to achieve this would be the Devel module in conjunction with some preprocess functions.

Community
  • 1
  • 1
PatrickS
  • 9,539
  • 2
  • 27
  • 31
  • I'm not on a terms page per se. Just a page created by views that uses Views Contextual filters as input. The block is on the same page and uses the same filters. I was able to work around it by attaching the params to the page url and then in my block php code passing them manually to [code]views_embed_view()[/code] – jeff Dec 02 '13 at 15:40
  • If the page is generated by Views, the block may not get access to the same variables. As mentioned above you can easily check this with hook_preprocess(), if you prefer you could check the variables with hook_preprocess_page() & hook_preprocess_block()... Although your method will work, for many reasons , I tend to prefer a Drupal way solution :) – PatrickS Dec 03 '13 at 06:28
0

print $block = module_invoke('views', 'block_view', 'your block name');

print render($block['content']['#content']);

your can also use print_r() / var_dump() to display your desire field