31

I created a view block, whose machine name is "news."

How do I display it using module_invoke() and the machine name of the view?

63

Use views_embed_view():

echo views_embed_view('view_machine_name', 'block_1');

You can find out the display name by clicking the tab and check its name in the address bar of your browser: #views-tab-block_1 gives you block_1.

| improve this answer | |
  • what about drupal 8? do you know how does it work in drupal 8? – shekoufeh May 15 '19 at 13:25
  • For Drupal 8 we use Twig Tweak and this syntax {{ drupal_view('view_machine_name', 'block_1') }} – tostinni Jul 29 '19 at 21:20
22

If you want to pass a parameter to the view use:

echo views_embed_view('view_machine_name', 'block_1', $param);

For example:

echo views_embed_view('view_machine_name', 'block_1', $node->nid);

or:

echo views_embed_view('view_machine_name', 'block_1', $nid, $uid);
| improve this answer | |
8

tostinni's answer is the correct one in most cases. However, if you want to include the View title, it is a bit more ugly.

From the views_embed_view() documentation:

Note that this function does NOT display the title of the view. If you want to do that, you will need to do what this function does manually, by loading the view, getting the preview and then getting $view->get_title().

For example:

$name = 'view_machine_name';
$display_id = 'block_1';
$view = views_get_view($name);
if (!$view || !$view->access($display_id)) {
  return;
}
$view_content = $view->preview($display_id);
$title = $view->get_title();

Updated: @rojosnow noted that the docs point out you must get the preview before calling get_title() so I've flipped those two lines around.

| improve this answer | |
1

This Worked for me :

$view = views_get_view('view_machine_name');
$view->set_display('Master');
$view->get_title();
$response = $view->preview('You_Block_id');

//and called it in html like this//

<div class="col-md-4 col-sm-6">
<div class="blocksOuterWrapper companyNewsWrapper">
'.$response.'
</div>
</div>
| improve this answer | |
1

Note, for me the default block (with no name) is called block rather than block_1:

<?php echo views_embed_view('news_archive', 'block'); ?>
| improve this answer | |
0

Also building on tostinni's answer:

If you don't want any HTML to show up if the view has no results. Also, add a title, but only if there are results.

if (array_filter(views_get_view_result('view_machine_name', 'block_1'))) {
    print '<h2 class="my-view-block-title">Special Title</h2>';
    print views_embed_view('view_machine_name', 'block_1');
}
| improve this answer | |
0

You could render your block instead of rendering the views by the following code.

<?php
 $block = block_load($module, $delta);
 $render_array = _block_get_renderable_array(_block_render_blocks(array($block)));
 $output = render($render_array);
 print $output;
?>
| improve this answer | |
0

From my own experience, you can transform a path to a view title or content with a function like the following.

<?php
function view_path_to_title($path)
{
  // only if your path is a URL alias
  $sql = "SELECT src FROM {url_alias} WHERE dst = '%s'";
  $src = db_result(db_query($sql, $path));

  // if you do not use URL aliases, you probably use $path here instead of $src
  $menu_item = menu_get_item($src);

  // make sure it worked as expected
  if(!empty($menu_item)
  && $menu_item["page_callback"] == "views_page"
  && count($menu_item["page_arguments"]) >= 2)
  {
    $args = $menu_item["page_arguments"];
    $view = views_get_view($args[0]);
  }
  else
  {
    $view = null;
  }

  if ($view)
  {
    // just loading a view is not quite enough, we have to setup the
    // arguments, including the display, and the "execute" the view
    $view->set_display($args[1]);
    array_shift($args); // view name
    array_shift($args); // display name
    $view->set_arguments($args);
    $view->execute();
    $title = $view->get_title();
    if($title)
    {
      return $title;
    }
  }

  // some "random" default
  return $path;
}

To render the view, use $view->render() instead of $view->get_title().

For sure, until I properly called the $view->execute(), nothing worked right. I would get empty strings (maybe null) as return values.

| improve this answer | |

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.