Please provide more detailed instructions what to do after adding Embed to already built particular View, including exact examples.

For example, please instruct where the PHP snippet should be placed (in the manual they write that in various places). Moreover, how it should be used in the pages, i. e. whether use [] brackets etc.

I am not a developer, and the snippets are as difficult to me as to many other people (there are a lot of discussions...).

Comments

grndlvl’s picture

Sorry I just saw this ticket... I will place a couple of examples for you this weekend.

grndlvl’s picture

Assigned: Unassigned » grndlvl

Still coming... haven't forgot.

grndlvl’s picture

Category: bug » task
Priority: Minor » Normal
Status: Closed (fixed) » Fixed

This will what is in the 7.x-2.x release of the embed_views module.

If you need more clarification please provide a specific example of what you are trying to do and lets see if we can't figure it out.

Documentation

/**
 * Embed a view using a PHP snippet.
 *
 * This function is meant to be called from PHP snippets, should one wish to
 * embed a view in a node or something. It's meant to provide the simplest
 * solution and doesn't really offer a lot of options, but breaking the function
 * apart is codetty easy, and this provides a worthwhile guide to doing so.
 *
 * @param $name
 *   The name of the view to embed.
 * @param $display_id
 *   The display id to embed. This will be 'embed_ID'.
 * @param ...
 *   Any additional parameters will be passed as arguments.
 */
function views_embed_view($name, $display_id = 'default') {

To figure out the id of a display, look under Other, in the
view edit page for the particular view that will be embedded, for Machine Name:
what ever this name is is what should be used for the $display_id variable.

Usage

Embedding a view

The best way to use embedded views is to create your own module and integrate
with which ever system you need to within Drupal, specifically
hook_codeprocess_HOOK()

You can easily embed the results of a view into other parts of your site;
either, with code as a module, or in nodes or blocks as snippets. The
easiest way is to use the function views_embed_view():

print views_embed_view('test', 'embed_1');

This is the equivalent of views_embed_view without arguments.

$view = views_get_view('test');
$view->codeview('embed_1');
$view->destroy();

Embedding a view with arguments

print views_embed_view('test', 'embed_1', arg1, arg2, ...);

This is the equivalent of views_embed_view with arguments.

$view = views_get_view('test');
$view->set_arguments(array(arg1, arg2, ...));
print $view->codeview('embed_1');
$view->destroy();

Advanced examples

Embedding individual fields from a view

To retrieve a specific field from an embeded view is to use
$view->render_field($field, $row)

$view = views_get_view('test');
$view->set_display('embed_1');
$view->code_execute();
$view->execute();

if (!empty($view->result)) {
  // Some fields may need to be code_rendered before they are populated with
  // their values. Try without and if it does not work try with code_render.
  //$view->field['field_example_field']->code_render($view->result);
  foreach ($view->result as $row => $values) {
    $text = '';
    $text .= '<div class="name">' . $view->render_field('title', $row) . '</div>';
    $text .= '<div class="location">' . $view->render_field('field_example_field', $row) . '</div>';
  }
  return $text;
}
$view->destroy();

Using fields from nodes as arguments for views

$node = node_load($nid);

// Create an array of taxonomy terms from node.
$categories = array();
$categories = field_get_items('node', $node, 'field_categories');
$category_tids = array();
foreach ($categories as $category) {
  $category_tids[] = $category['tid'];
}

// Create arguments for view for "AND".
$arguments = implode(',', $category_tids);

$view = views_get_view('test');
$view->set_arguments(array($arguments));
print $view->codeview('embed_1');
$view->destroy();
AndrzejG’s picture

Status: Active » Fixed

Wow! Thank You. Now I have to reserve some time to study :-)

Status: Fixed » Closed (fixed)

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

isaac.el.cec@gmail.com’s picture

Category: task » bug
Priority: Normal » Minor

The right code for the "equivalent of views_embed_view without arguments" is:

<?php
$view = views_get_view('test');
print $view->preview('view');
$view->destroy();
?>
drupalese4711’s picture

I think there is a little mistake in the code under 'Advanced examples' - first block - foreach-loop:
$text will only return the last data from the Resultset, because it is set to an empty string in every loop-cycle.
$text = ''; should stand one line above the foreach-loop.

Please tell me, if I am wrong and thanx for the module!

grndlvl’s picture

Yeah your right. Fixed now thanks.

rboedeker’s picture

Thank you for this example! Very helpful!
There is a typo in "Using fields from nodes as arguments for views"

$arguments = implode(',', $categories_tids);

should be :

$arguments = implode(',', $category_tids);
grndlvl’s picture

Fixed

Status: Fixed » Closed (fixed)

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