0

I want to embed a View (or a display of a View) inside another display of View when these view has no results. I figure out that I can embed a View inside the 'Empty Text' filed. I must say that I can show some php echo "hello world"; using this 'Empty Text' filed, but when I try to embed a View, I can not embed a another View or display of View.

this is one example that I used, but without results:

$viewName = 'test_view';
$display_id = 'page_1';
print views_embed_view($viewName, $display_id);
3
  • one thing to note, when you embed a view inside another views empty result you will get extra div/span tags that are extremely ugly.
    – iStryker
    Commented Aug 17, 2011 at 16:51
  • hello iStryker, I guess that extra div/span can be resolved, with CSS or/and preprocessing the string. Do you know how to embed a view inside Empty Text of another View?
    – MWt
    Commented Aug 17, 2011 at 18:04
  • extra div/span are hard to get rid off with views. Views have to many levels of templates (one for field, one for fields, one for row, one for style, etc). I don't have time to fully research this. I believe you have 2 options. #1 Research Attached display. This allows you to attach a view to another view. You may have an option to display/not display if empty text. #2, I solve this with panels. using separate views, and php code for visiblity, kidna like Jukebox code
    – iStryker
    Commented Aug 18, 2011 at 5:01

3 Answers 3

1

In D7, Views 3 you can enable "NO RESULTS BEHAVIOR" which is under the "Advanced" tab. Here you can add "Global: View area" (or just "hello world" as a text area for testing) which will allow you to display a view when there isn't any results.

If you want this is code, you can always export the View after it is configured and see how it was done.

3
  • very thanks for your question, I use Views 2, but will be an option if I can not reach this requirement with Views 2.
    – MWt
    Commented Aug 17, 2011 at 16:49
  • np, I'm not sure if you can do this in Views 2 (jumped into Drupal w/ Views 3) so sorry I can't help more.
    – nmillin
    Commented Aug 17, 2011 at 16:58
  • in my case, jump to Views 3 implies review lots of other views. I will stay pending of answers related with Views 2, but of course your answer is very useful to Views 3.
    – MWt
    Commented Aug 17, 2011 at 17:59
1

nMill's answer is great for Views 3.

I put together a solution that involves a bit more work for Views 2 (and there might be an even better way to do this). I'm using hook_views_pre_render and the views_embed_view function to do this.

In your custom module, insert

function mymodule_views_pre_render(&$view) {
    switch($view->name) {
        case 'parent_view_name':
            if (empty($view->result)) {
                            $attachment = views_embed_view('view_to_insert', 'default');
                $view->attachment_after = $attachment;
            }
        break;
    }
}

Clear the cache and your new view should appear if there are no results in the parent view.

2
  • thank you for your suggestion. for some reason your code is not working in my specific case. the output still is empty text. I tested using two differents views and also two differents displays from the same view. I will review your suggestion with more careful, for detect my mistakes.
    – MWt
    Commented Aug 18, 2011 at 1:06
  • What I like to do in these situations is insert a drupal_set_message('testing') just to make sure that the code is being called in the first place. Place that before the $view->attachment_after line and see if your testing function is called.
    – Jukebox
    Commented Aug 18, 2011 at 5:24
1

This has been added in Views 3 and can be found in 'No results behavior' where you can add 'View area' which would be inserted on empty results.

Here is example how to do it programatically by using hook_views_pre_view:

/**
 * Implements hook_views_pre_view().
 */
function MYMODULE_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == 'my_view') {
    // Unset: Global: Text area
    $view->display_handler->set_option('empty', NULL);

    /* Programatically update the 'No results behavior' by inserting another view. */

    // Example 1
    $options = array(
        'id' => 'view',
        'table' => 'views',
        'field' => 'view',
        'empty' => TRUE,
        'view_to_insert' => 'new_view:page',
    );
    $view->display_handler->set_option('empty', array('view' => $options));

    // Example 2
    $view->set_item_option($display_id, 'empty', 'view', 'id', 'view');
    $view->set_item_option($display_id, 'empty', 'view', 'table', 'views');
    $view->set_item_option($display_id, 'empty', 'view', 'field', 'view');
    $view->set_item_option($display_id, 'empty', 'view', 'empty', TRUE);
    $view->set_item_option($display_id, 'empty', 'view', 'view_to_insert', 'new_view:page');
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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