How do I specify which block display to embed? (views 2.0)

fletchgqc - May 28, 2008 - 03:54
Project:Views
Version:6.x-2.0-beta3
Component:block displays
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

I am using views_embed_view($view_name, 'block', $args) to embed a view into a node. My view has a number of different block displays associated with it. How can I specify which of the blocks I would like to embed?

Thanks.

#1

merlinofchaos - May 28, 2008 - 05:45
Status:active» fixed

Each block will have its own display id; this is probably easiest to find in the export, but it'll just be block1, block2, block3, etc, based upon the order you added them to the view.

#2

Anonymous (not verified) - June 11, 2008 - 05:52
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

#3

mariagwyn - July 18, 2008 - 00:47
Status:closed» active

Sorry to reopen this, but I am completely confused regarding how to embed a block from a custom view into a node. What I need is the exact syntax of views_embed_view($view_name, 'block', $args) since I seem unable to figure it out myself. Here is the view info:

View title: 'archive_reflections'
Block title: 'archive_reflections-block_1' (I think, hard to find this, but I hovered over the configure tab.

My failed PHP snippet:

<?php  $output = views_embed_view('archive_reflections', 'block', 'archive_reflections-block_1'); ?>

I also tried 'block_1', '1', 'archive_reflections'...I am at a complete loss and am feeling pretty dumb. Any help?

Thanks,
Maria

#4

merlinofchaos - July 18, 2008 - 01:33
Status:active» fixed

The id of that block is just 'block_1' which is what you'll use for the $display_id argument.

#5

mariagwyn - July 18, 2008 - 01:43

So I tried the following:
<?php  $output = views_embed_view('archive_reflections', 'block_1'); ?>
and
<?php  $output = views_embed_view('archive_reflections', 'block_1', 'block_1'); ?>.
Neither work. So, what am I missing? I am really confused. Obviously.

#6

merlinofchaos - July 18, 2008 - 03:02

Can you please include an export of the view?

#7

mariagwyn - July 18, 2008 - 05:43

Merlin, thanks for looking into this. Here is the export:

$view = new view;
$view->name = 'archive_reflections';
$view->description = 'Browse through Reflections by year.  Date browser attachment adds back/next navigation to the top of the page.';
$view->tag = 'Archives';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Reflections archive', 'default');
$handler->override_option('fields', array(
  'title' => array(
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'label' => 'Title',
    'relationship' => 'none',
    'link_to_node' => 1,
  ),
  'field_reflection_month_value' => array(
    'label' => '',
    'link_to_node' => 0,
    'label_type' => 'widget',
    'format' => 'default',
    'multiple' => array(
      'group' => TRUE,
      'multiple_number' => '',
      'multiple_from' => '',
      'multiple_reversed' => FALSE,
    ),
    'exclude' => 1,
    'id' => 'field_reflection_month_value',
    'table' => 'node_data_field_reflection_month',
    'field' => 'field_reflection_month_value',
    'relationship' => 'none',
  ),
));
$handler->override_option('arguments', array(
  'date_argument' => array(
    'default_action' => 'default',
    'style_plugin' => 'default_summary',
    'style_options' => array(),
    'wildcard' => 'all',
    'wildcard_substitution' => 'All',
    'title' => '',
    'default_argument_type' => 'date',
    'default_argument' => '',
    'validate_type' => 'none',
    'validate_fail' => 'not found',
    'date_fields' => array(
      'node_data_field_reflection_date.field_reflection_date_value' => 'node_data_field_reflection_date.field_reflection_date_value',
    ),
    'date_method' => 'OR',
    'granularity' => 'year',
    'id' => 'date_argument',
    'table' => 'node',
    'field' => 'date_argument',
    'relationship' => 'none',
    'default_argument_user' => 0,
    'default_argument_fixed' => '',
    'default_argument_php' => '',
    'validate_argument_node_type' => array(
      'blog' => 0,
      'image' => 0,
      'forum' => 0,
      'book' => 0,
      'event' => 0,
      'page' => 0,
      'reflection' => 0,
      'story' => 0,
    ),
    'validate_argument_node_access' => 0,
    'validate_argument_nid_type' => 'nid',
    'validate_argument_vocabulary' => array(
      '1' => 0,
      '3' => 0,
      '2' => 0,
      '5' => 0,
      '4' => 0,
    ),
    'validate_argument_type' => 'tid',
    'validate_argument_php' => '',
    'default_options_div_prefix' => '',
  ),
));
$handler->override_option('filters', array(
  'type' => array(
    'operator' => 'in',
    'value' => array(
      'reflection' => 'reflection',
    ),
    'group' => 0,
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'type',
    'table' => 'node',
    'field' => 'type',
    'relationship' => 'none',
  ),
  'status_extra' => array(
    'operator' => '=',
    'value' => '',
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'status_extra',
    'table' => 'node',
    'field' => 'status_extra',
    'relationship' => 'none',
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
  'role' => array(),
  'perm' => '',
));
$handler->override_option('title', 'Reflections Archive');
$handler->override_option('use_pager', '1');
$handler->override_option('use_more', 1);
$handler->override_option('style_plugin', 'list');
$handler->override_option('style_options', array(
  'grouping' => 'field_reflection_month_value',
  'type' => 'ul',
));
$handler->override_option('row_options', array(
  'inline' => array(
    'title' => 'title',
    'field_reflection_date_value' => 'field_reflection_date_value',
    'field_reflection_feastday_value' => 'field_reflection_feastday_value',
  ),
  'separator' => '',
));
$handler = $view->new_display('page', 'Page', 'page');
$handler->override_option('fields', array(
  'title' => array(
    'label' => '',
    'link_to_node' => 1,
    'exclude' => 0,
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'relationship' => 'none',
    'override' => array(
      'button' => 'Use default',
    ),
  ),
  'field_reflection_month_value' => array(
    'label' => '',
    'link_to_node' => 0,
    'label_type' => 'widget',
    'format' => 'default',
    'multiple' => array(
      'group' => TRUE,
      'multiple_number' => '',
      'multiple_from' => '',
      'multiple_reversed' => FALSE,
    ),
    'exclude' => 1,
    'id' => 'field_reflection_month_value',
    'table' => 'node_data_field_reflection_month',
    'field' => 'field_reflection_month_value',
    'relationship' => 'none',
  ),
));
$handler->override_option('items_per_page', 0);
$handler->override_option('use_pager', '1');
$handler->override_option('path', 'reflections/archive');
$handler->override_option('menu', array(
  'type' => 'none',
  'title' => '',
  'weight' => 0,
));
$handler->override_option('tab_options', array(
  'type' => 'none',
  'title' => '',
  'weight' => 0,
));
$handler = $view->new_display('date_nav', 'Date browser', 'date_nav_1');
$handler->override_option('style_plugin', 'date_nav');
$handler->override_option('style_options', array(
  'grouping' => 'field_reflection_month_value',
  'type' => 'ul',
));
$handler->override_option('row_options', array(
  'inline' => array(
    'title' => 'title',
    'field_reflection_date_value' => 'field_reflection_date_value',
    'field_reflection_feastday_value' => 'field_reflection_feastday_value',
  ),
  'separator' => '',
));
$handler->override_option('displays', array(
  'page' => 'page',
  'default' => 0,
));
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('fields', array(
  'title' => array(
    'label' => '',
    'link_to_node' => 1,
    'exclude' => 0,
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'relationship' => 'none',
    'override' => array(
      'button' => 'Use default',
    ),
  ),
));
$handler->override_option('title', 'Reflections');
$handler->override_option('style_plugin', 'list');
$handler->override_option('style_options', array(
  'grouping' => '',
  'type' => 'ul',
));
$handler->override_option('row_options', array(
  'inline' => array(
    'title' => 'title',
    'field_reflection_date_value' => 'field_reflection_date_value',
    'field_reflection_feastday_value' => 'field_reflection_feastday_value',
  ),
  'separator' => '',
));

#8

merlinofchaos - July 18, 2008 - 06:07

The function you're calling appears to be correct based upon the view. But you're assigning the result to $output. Are you later doing a print $output? If not, maybe replace $output = with 'print'.3

#9

mariagwyn - July 18, 2008 - 18:19

I have now tried all of the following, with no success:

First Try

<?php  $output = views_embed_view('archive_reflections', 'block_1'); ?>
<?php print $output['subject']; ?>
<?php print $output['content']; ?>

2nd Try
<?php  $output = views_embed_view('archive_reflections', 'block', 'block_1'); ?>
<?php print $block['subject']; ?>
<?php print $block['content']; ?>

3rd Try
<?php  $output = views_embed_view('archive_reflections', 'block_1', 'block_1'); ?>
<?php print $block['subject']; ?>
<?php print $block['content']; ?>

4th Try
<?php  $print = views_embed_view('archive_reflections', 'block', 'block_1'); ?>
<?php  $print = views_embed_view('archive_reflections', 'block_1'); ?>

Can you actually type the whole function for me? I don't know php that well, and suspect I am missing something very obvious.

Thanks,
Maria

#10

merlinofchaos - July 18, 2008 - 21:00

I think you just want:

<?php
 
print views_embed_view('archive_reflections', 'block_1');
?>

#11

mariagwyn - July 19, 2008 - 00:45

jeez, that was sooooo easy! Thanks.

#12

mariagwyn - July 19, 2008 - 19:22

okay, sorry to ask more questions, but, the code above does embed the block. But it embeds it with all the hidden fields that I have removed in the view display (such as a hidden "label" field). While no text appears in the area, the css creates the space for the text, which makes for very messy looking lists.

How can I embed the block exactly as it appears in a sidebar?

To see what I am talking about, visit: http://dev.mariagwyn.com/newskete/drupal/reflections.

The "Reflections" Block on the right is the view block on the sidebar. In the content area, there is a messy list under "Current Block." It is the reflections block inserted with the code above. What adjustment do I make to the code to call the block so it looks the same as it does in the right sidebar?

And where in the world is the documentation that explains the embed_view function? It is referred to...but I can't find it.

Thanks,
Maria

#13

mariagwyn - July 25, 2008 - 16:56
Status:fixed» active

any help?
m

#14

merlinofchaos - July 25, 2008 - 17:07

Sorry, I've been busy trying to get Panels 2 ported to D6 so I have less time for working Views issues. Not trying to ignore, but the issue queue volume is very high (at least, it is for just one person).

The issue you're having seems rather odd and I don't understand why the block would appear any different when embedded than when actually used. They should be identical. So I'm confused as to what's going on here.

#15

mariagwyn - July 25, 2008 - 18:55

ah, well, I love panels and am hoping to use it on 6 so no worries about the delay.

I am not sure how to better explain the issue, it does seem odd. Did you see the page I referred to? it shows the two versions. If you use firebug, you can see the hidden fields and compare with the reflections block to the right. Is there something slightly wrong in the code?

thanks,
m

#16

merlinofchaos - July 25, 2008 - 18:57

I did see. I don't think there's anything wrong with your code; this may be a bug, but I just can't envision, in my head, where the bug might be.

Are you using the 'exclude' feature to not print certain fields?

#17

mariagwyn - July 25, 2008 - 21:49

Odd. I switched the block to a table view rather than list view, and it is displaying correctly. It appears to be a problem with list view in particular and how it is generated. Here is the difference in code...notice the rather large number of break tags inserted in the incorrect version:

Content Version (incorrect):

<ul>
<li>
  <span class="views-field-title"><br />
          <label class="views-label-title"><br />
        Title:<br />
      </label><br />
          <span class="field-content"><a href="/newskete/drupal/reflections/don%E2%80%99t-ask-%E2%80%93-why-not">Don’t Ask! – Why not?</a></span><br />
  </span>
<p>  <span class="views-field-field-reflection-date-value"><br />
          <label class="views-label-field-reflection-date-value"><br />
        Delivered:<br />
      </label><br />
          <span class="field-content"><span class="date-display-single">07/06/2008 </span></span><br />
  </span>
</p></li>
</ul>

Sidebar Version (correct):
    <ul>
          <li> 
  <span class="views-field-title">
          <label class="views-label-title">
        Title:
      </label>
          <span class="field-content"><a href="/newskete/drupal/reflections/don%E2%80%99t-ask-%E2%80%93-why-not">Don’t Ask! – Why not?</a></span>
  </span>
 
  <span class="views-field-field-reflection-date-value">
          <label class="views-label-field-reflection-date-value">
        Delivered:
      </label>
          <span class="field-content"><span class="date-display-single">07/06/2008 </span></span>
  </span>
</li>
</ul>

Any ideas where this is happening, or where to bump this issue to make it more clear for someone who can actually correct this code within views?

Thanks,
Maria

#18

merlinofchaos - July 25, 2008 - 22:37

Heh, sorry, I'm the Views guy -- there's nowhere to bump this issue to. =)

OH! Looking at this code...I think know what's happening!

It looks like the Views generated block is being passed through the linebreak filter. Do you have the linebreak filter on your PHP filter? If so, that's very likely the problem. Looking at this code, it looks like this is probably the only way those br tags could be getting in there is if they're generated afterward.

Things would appear to work as a table because the br tags would have very little effect amidst table tags.

Further evidence is that you'll also notice that a <p> tag has been added. I'm sure that's the linebreak filter being applied to the Views output after it's already been given to you.

#19

mariagwyn - July 28, 2008 - 05:28

bingo! Thanks for being so consistently helpful (on this and other questions I have had).

Views is great by the way. I love that in 2.0 I can create multiple blocks/pages/etc all within one overarching views. Significantly cuts down on clutter.

Maria

#20

mariagwyn - July 28, 2008 - 05:36
Status:active» fixed

#21

mariagwyn - July 30, 2008 - 02:10
Status:fixed» active

clearly, I believe in some sort of resurrection from the dead, or at least, am trying hard. So, sorry to resurrect, but I just realized...

When I embed the block view, the TITLE does not appear in the block. Is there a way to do this with arguments?

Thanks,
Maria

#22

merlinofchaos - July 30, 2008 - 02:31

The block system (and indeed, most of Drupal) has the title and the content printed separately, meaning that embedding the view you get only the content. To get the title you'll need to do a more complex job:

<?php
  $view
= views_get_view('viewname');
 
$view->set_display('display_id');
 
$view->set_arguments(args...);
 
$output = $view->preview();
 
$title = $view->get_title();
  print
'<div class="your title class">' . $title . '</div>';
  print
$output;
?>

#23

mariagwyn - July 30, 2008 - 21:28
Status:active» fixed

thanks, very helpful! I am closing this again. ha!
maria

#24

Anonymous (not verified) - August 13, 2008 - 21:32
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.