Using multiple views with ddblock module
webjomo - March 18, 2009 - 12:45
| Project: | Dynamic display block |
| Version: | 6.x-1.0-rc6 |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
| Issue tags: | Advanced slideshow, content |
Description
Getting this error:
warning: Invalid argument supplied for foreach() in /var/www/sites/all/themes/fourseasons/custom/modules/ddblock/ddblock-cycle-block-content-upright50.tpl.php on line 47.
When viewing a second instance of Dynamic Display Block.
The second instance is created from a View, that works fine with the first block but pulls in a different result set due to a Taxonomy filter.

#1
My bad. Just found this in the FAQ, will try the recommendations
#2
Have tried the recommendations in the FAQ, still have the problem
The debug code is:
'buyside_home_page'stdClass::__set_state(array('nid' => '1532',
'node_title' => 'Test page 1',
'node_data_field_pager_item_text_field_pager_item_text_value' => 'Test page 1',
'node_data_field_pager_item_text_nid' => '1532',
'node_type' => 'ddblock_news_item',
'node_data_field_pager_item_text_field_slide_text_value' => 'Test page 1',
'node_data_field_pager_item_text_field_image_fid' => '1029',
'node_data_field_pager_item_text_field_image_list' => '1',
'node_data_field_pager_item_text_field_image_data' => 'a:3:{s:11:"description";s:0:"";s:3:"alt";s:0:"";s:5:"title";s:0:"";}',
'node_revisions_body' => 'Test page 1',
'node_revisions_format' => '1',
'node_created' => '1237374439',
))
And the error is:
warning: Invalid argument supplied for foreach() in /var/www/sites/all/themes/fourseasons/custom/modules/ddblock/ddblock-cycle-block-content-upright50.tpl.php on line 47.#3
I think the issue is that I am attempting to use two different views for two different instances.
How do I handle template.php if I want one instance to show one view, and the other instance to show a different view?
#4
When you want to use two views for different slideshow you need to create another if statement for the other view in the preprocess functions:
Like:
function [THEME_NAME]_preprocess_ddblock_cycle_block_content(&$vars) {if ($vars['output_type'] == 'view_fields') {
$content = array();
// Add slider_items for the template
// If you use the devel module uncomment the following line to see the theme variables
// dsm($vars['settings']['view_name']);
// dsm($vars['content'][0]);
// If you don't use the devel module uncomment the following line to see the theme variables
// drupal_set_message('<pre>' . var_export($vars['settings']['view_name'], true) . '</pre>');
// drupal_set_message('<pre>' . var_export($vars['content'][0], true) . '</pre>');
if ($vars['settings']['view_name'] == '[Name_of_view_1]') {
if (!empty($vars['content'])) {
foreach ($vars['content'] as $key1 => $result) {
// add slide_image variable
if (isset($result->node_data_field_image_field_image_fid)) {
// get image id
$fid = $result->node_data_field_image_field_image_fid;
// get path to image
$filepath = db_result(db_query("SELECT filepath FROM {files} WHERE fid = %d", $fid));
// use imagecache (imagecache, preset_name, file_path, alt, title, array of attributes)
if (module_exists('imagecache') && is_array(imagecache_presets()) && $vars['imgcache_slide'] <> '<none>'){
$slider_items[$key1]['slide_image'] =
theme('imagecache',
$vars['imgcache_slide'],
$filepath,
$result->node_title);
}
else {
$slider_items[$key1]['slide_image'] =
'<img src="' . base_path() . $filepath .
'" alt="' . $result->node_title .
'"/>';
}
}
// add slide_text variable
if (isset($result->node_data_field_pager_item_text_field_slide_text_value)) {
$slider_items[$key1]['slide_text'] = $result->node_data_field_pager_item_text_field_slide_text_value;
}
// add slide_title variable
if (isset($result->node_title)) {
$slider_items[$key1]['slide_title'] = $result->node_title;
}
// add slide_read_more variable and slide_node variable
if (isset($result->nid)) {
$slider_items[$key1]['slide_read_more'] = l('Read more...', 'node/' . $result->nid);
$slider_items[$key1]['slide_node'] = 'node/' . $result->nid;
}
}
}
}
if ($vars['settings']['view_name'] == '[Name_of_view_2]') {
if (!empty($vars['content'])) {
foreach ($vars['content'] as $key1 => $result) {
// add slide_image variable
if (isset($result->node_data_field_image_field_image_fid)) {
// get image id
$fid = $result->node_data_field_image_field_image_fid;
// get path to image
$filepath = db_result(db_query("SELECT filepath FROM {files} WHERE fid = %d", $fid));
// use imagecache (imagecache, preset_name, file_path, alt, title, array of attributes)
if (module_exists('imagecache') && is_array(imagecache_presets()) && $vars['imgcache_slide'] <> '<none>'){
$slider_items[$key1]['slide_image'] =
theme('imagecache',
$vars['imgcache_slide'],
$filepath,
$result->node_title);
}
else {
$slider_items[$key1]['slide_image'] =
'<img src="' . base_path() . $filepath .
'" alt="' . $result->node_title .
'"/>';
}
}
// add slide_text variable
if (isset($result->node_data_field_pager_item_text_field_slide_text_value)) {
$slider_items[$key1]['slide_text'] = $result->node_data_field_pager_item_text_field_slide_text_value;
}
// add slide_title variable
if (isset($result->node_title)) {
$slider_items[$key1]['slide_title'] = $result->node_title;
}
// add slide_read_more variable and slide_node variable
if (isset($result->nid)) {
$slider_items[$key1]['slide_read_more'] = l('Read more...', 'node/' . $result->nid);
$slider_items[$key1]['slide_node'] = 'node/' . $result->nid;
}
}
}
}
$vars['slider_items'] = $slider_items;
}
}
You need to change [THEME_NAME] to the name of the theme you use, e.g. garland.
You need to change [Name_of_view_1] to the name of the first view you use, e.g news_item.
You need to change [Name_of_view_2] to the name of the second view you use, e.g top_products.
same for the pager preprocess function.
Hope this helps you further, please let me know
If not please send a link to your Internet site, your template.php file and all the results of the drupal_set_message debug lines in the preprocess functions, so I can help you better.
#5
I followed this code snippet to have multiple instances of slideshow blocks from different content-type so we could show the slideshow on different pages with different nodes from different section pulled in.
It works fine but the only problem is that the read more button link doesn't link to the actual full node? what's more, when clicking on the whole slideshow it only changes the slides.
The first Views isn't a problem, it only happens with the second views (ie, Name_of_view_2), like in the example code and i'm not sure what it wrong with the read more link.
Thanks,
--- EDITED ---
Okay, found the answer, seems like it was the 'next' in block config which gave me the problem, see: http://drupal.org/node/406754
#6
Changed text of issue to be more useful for other users.
Added tags
Set status to fixed.
#7
I tried to implement this code, but my pre-process seems to be different from the one above. Inserting the code above and changing the theme and view names breaks both of my slide shows and returns the foreach() error.
I know I need to add another if statement for the new view (named 'news_items_e3') but every time I try to insert it i break my php :(
Here is my existing preprocess code any help would be great. I'll keep plugging away at it in the mean time.
function pixture_reloaded_preprocess_ddblock_cycle_pager_content(&$vars) {if (($vars['output_type'] == 'view_fields') && ($vars['pager_settings']['pager'] == 'custom-pager')){
$content = array();
// Add pager_items for the template
// If you use the devel module uncomment the following lines to see the theme variables
// dsm($vars['pager_settings']['view_name']);
// dsm($vars['content'][0]);
// If you don't use the devel module uncomment the following lines to see the theme variables
//drupal_set_message('<pre>' . var_export($vars['pager_settings'], true) . '</pre>');
//drupal_set_message('<pre>' . var_export($vars['content'][0], true) . '</pre>');
if ($vars['pager_settings']['view_name'] == 'news_items') {
foreach ($vars['content'] as $key1 => $result) {
// add pager_item_image variable
if (isset($result->node_data_field_pager_item_text_field_image_fid)) {
$fid = $result->node_data_field_pager_item_text_field_image_fid;
$filepath = db_result(db_query("SELECT filepath FROM {files} WHERE fid = %d", $fid));
// use imagecache (imagecache, preset_name, file_path, alt, title, array of attributes)
if (module_exists('imagecache') &&
is_array(imagecache_presets()) &&
$vars['imgcache_pager_item'] <> '<none>'){
$pager_items[$key1]['image'] =
theme('imagecache',
$vars['pager_settings']['imgcache_pager_item'],
$filepath,
$result->node_data_field_pager_item_text_field_pager_item_text_value);
}
else {
$pager_items[$key1]['image'] =
'<img src="' . base_path() . $filepath .
'" alt="' . $result->node_data_field_pager_item_text_field_pager_item_text_value .
'"/>';
}
}
// add pager_item _text variable
if (isset($result->node_data_field_pager_item_text_field_pager_item_text_value)) {
$pager_items[$key1]['text'] = $result->node_data_field_pager_item_text_field_pager_item_text_value;
}
}
}
$vars['pager_items'] = $pager_items;
}
}
#8
@DjC4
You need to insert the if statement like below.
I added the debug lines also per view so you can see what settings and field names you have per view.
I changed the view_name already in the second if statement to: news_items_e3
You still need to rename the fields in this view.
This is the pager content preprocess function.
You need to do the same with the content preprocess function.
function pixture_reloaded_preprocess_ddblock_cycle_pager_content(&$vars) {if (($vars['output_type'] == 'view_fields') && ($vars['pager_settings']['pager'] == 'custom-pager')){
$content = array();
// Add pager_items for the template
// If you use the devel module uncomment the following lines to see the theme variables
// dsm($vars['pager_settings']['view_name']);
// dsm($vars['content'][0]);
// If you don't use the devel module uncomment the following lines to see the theme variables
//drupal_set_message('<pre>' . var_export($vars['pager_settings'], true) . '</pre>');
//drupal_set_message('<pre>' . var_export($vars['content'][0], true) . '</pre>');
if ($vars['pager_settings']['view_name'] == 'news_items') {
//drupal_set_message('<pre>' . var_export($vars['pager_settings'], true) . '</pre>');
//drupal_set_message('<pre>' . var_export($vars['content'][0], true) . '</pre>');
foreach ($vars['content'] as $key1 => $result) {
// add pager_item_image variable
if (isset($result->node_data_field_pager_item_text_field_image_fid)) {
$fid = $result->node_data_field_pager_item_text_field_image_fid;
$filepath = db_result(db_query("SELECT filepath FROM {files} WHERE fid = %d", $fid));
// use imagecache (imagecache, preset_name, file_path, alt, title, array of attributes)
if (module_exists('imagecache') &&
is_array(imagecache_presets()) &&
$vars['imgcache_pager_item'] <> '<none>'){
$pager_items[$key1]['image'] =
theme('imagecache',
$vars['pager_settings']['imgcache_pager_item'],
$filepath,
$result->node_data_field_pager_item_text_field_pager_item_text_value);
}
else {
$pager_items[$key1]['image'] =
'<img src="' . base_path() . $filepath .
'" alt="' . $result->node_data_field_pager_item_text_field_pager_item_text_value .
'"/>';
}
}
// add pager_item _text variable
if (isset($result->node_data_field_pager_item_text_field_pager_item_text_value)) {
$pager_items[$key1]['text'] = $result->node_data_field_pager_item_text_field_pager_item_text_value;
}
}
}
if ($vars['pager_settings']['view_name'] == 'news_items_e3') {
//drupal_set_message('<pre>' . var_export($vars['pager_settings'], true) . '</pre>');
//drupal_set_message('<pre>' . var_export($vars['content'][0], true) . '</pre>');
foreach ($vars['content'] as $key1 => $result) {
// add pager_item_image variable
if (isset($result->node_data_field_pager_item_text_field_image_fid)) {
$fid = $result->node_data_field_pager_item_text_field_image_fid;
$filepath = db_result(db_query("SELECT filepath FROM {files} WHERE fid = %d", $fid));
// use imagecache (imagecache, preset_name, file_path, alt, title, array of attributes)
if (module_exists('imagecache') &&
is_array(imagecache_presets()) &&
$vars['imgcache_pager_item'] <> '<none>'){
$pager_items[$key1]['image'] =
theme('imagecache',
$vars['pager_settings']['imgcache_pager_item'],
$filepath,
$result->node_data_field_pager_item_text_field_pager_item_text_value);
}
else {
$pager_items[$key1]['image'] =
'<img src="' . base_path() . $filepath .
'" alt="' . $result->node_data_field_pager_item_text_field_pager_item_text_value .
'"/>';
}
}
// add pager_item _text variable
if (isset($result->node_data_field_pager_item_text_field_pager_item_text_value)) {
$pager_items[$key1]['text'] = $result->node_data_field_pager_item_text_field_pager_item_text_value;
}
}
}
$vars['pager_items'] = $pager_items;
}
}
Hope this helps you further, please let me know.
#9
Oops my bad with the "content" pre-process!
When you say to edit the fields in this view, i'm not sure what I need to change. Both views are pulling from the same node type, just displaying different content. "All News" VS "E3 News" kind of thing (not sure if that makes a difference for the fields).
I tried pasting the above code in and my existing DDB would only show the pager images, not the content (just white / blank)
When running the debug the only difference I can see between them right now is the "delta" variable. the existing (working) DDB shows as 2 and my new "news_items_e3" is 4.
here is the debug code from my "news_items_e3" DDB:
*
array (
'delta' => '4',
'output_type' => 'view_fields',
'pager' => 'custom-pager',
'pager_container' => NULL,
'pager_event' => NULL,
'pager_height' => 63,
'pager_width' => 195,
'imgcache_pager_item' => '',
'pager_position' => 'bottom',
'template' => 'upright40',
'custom_template' => NULL,
'view_name' => 'news_items_E3',
)
*
stdClass::__set_state(array(
'nid' => '682',
'node_title' => 'Mini Ninja Gameplay Video ',
'node_data_field_pager_item_text_field_pager_item_text_value' => NULL,
'node_type' => 'ddblock_news_item',
'node_vid' => '682',
'node_data_field_pager_item_text_field_slide_text_value' => 'The Latest Mini Ninja Game play Action: Inotamanojitsu !!!! ',
'node_data_field_pager_item_text_field_image_fid' => '924',
'node_data_field_pager_item_text_field_image_list' => '1',
'node_data_field_pager_item_text_field_image_data' => 'a:3:{s:11:"description";s:0:"";s:3:"alt";s:0:"";s:5:"title";s:0:"";}',
'node_revisions_body' => '
We covered the developer forum launch of this game a few months back. In the beginning it looked like a great little pick up and go game. With the latest game play video, released by Joystiq yesterday. The game is looking like a whole lot more than just your average weekend rental. With tactics, side missions, and the ability to possess animals of the forest. You can bet your gonna love some aspect of this game. This fills a void in my life that Tenchu left, just minus the senseless killing. Check the game play video here. As well don\'t forget to check the developer forum for latest updates and screen shots.
',
'node_revisions_format' => '1',
'node_created' => '1243680026',
))
*
'news_items_E3'
*
stdClass::__set_state(array(
'nid' => '682',
'node_title' => 'Mini Ninja Gameplay Video ',
'node_data_field_pager_item_text_field_pager_item_text_value' => NULL,
'node_type' => 'ddblock_news_item',
'node_vid' => '682',
'node_data_field_pager_item_text_field_slide_text_value' => 'The Latest Mini Ninja Game play Action: Inotamanojitsu !!!! ',
'node_data_field_pager_item_text_field_image_fid' => '924',
'node_data_field_pager_item_text_field_image_list' => '1',
'node_data_field_pager_item_text_field_image_data' => 'a:3:{s:11:"description";s:0:"";s:3:"alt";s:0:"";s:5:"title";s:0:"";}',
'node_revisions_body' => '
We covered the developer forum launch of this game a few months back. In the beginning it looked like a great little pick up and go game. With the latest game play video, released by Joystiq yesterday. The game is looking like a whole lot more than just your average weekend rental. With tactics, side missions, and the ability to possess animals of the forest. You can bet your gonna love some aspect of this game. This fills a void in my life that Tenchu left, just minus the senseless killing. Check the game play video here. As well don\'t forget to check the developer forum for latest updates and screen shots.
',
'node_revisions_format' => '1',
'node_created' => '1243680026',
))
#10
Like I said in the #8
This is the pager content preprocess function.
You need to do the same with the content preprocess function.
Also in the content preprocess function you need to add a second if like in the pager-content preprocess function.
If you are not able to add the second if statement for the second view, you can send the contnet preprocess function and I will add the second if statement.
As you mention: the fields are from the same content, so no need to change fieldnames like I said before.
Hope this helps you further, please let me know.
#11
Here is both my Content and Pager pre process. I keep breaking my php when I try to add the if hahah. I'll keep plugging away at it in the mean time. Thanks so much for all your extra support and help with this fantastic module!
function pixture_reloaded_preprocess_ddblock_cycle_block_content(&$vars) {if ($vars['output_type'] == 'view_fields') {
$content = array();
// Add slider_items for the template
// If you use the devel module uncomment the following line to see the theme variables
// dsm($vars['settings']['view_name']);
// dsm($vars['content'][0]);
// If you don't use the devel module uncomment the following line to see the theme variables
// drupal_set_message('<pre>' . var_export($vars['settings']['view_name'], true) . '</pre>');
// drupal_set_message('<pre>' . var_export($vars['content'][0], true) . '</pre>');
if ($vars['settings']['view_name'] == 'news_items') {
foreach ($vars['content'] as $key1 => $result) {
// add slide_image variable
if (isset($result->node_data_field_pager_item_text_field_image_fid)) {
// get image id
$fid = $result->node_data_field_pager_item_text_field_image_fid;
// get path to image
$filepath = db_result(db_query("SELECT filepath FROM {files} WHERE fid = %d", $fid));
// use imagecache (imagecache, preset_name, file_path, alt, title, array of attributes)
if (module_exists('imagecache') && is_array(imagecache_presets()) && $vars['imgcache_slide'] <> '<none>'){
$slider_items[$key1]['slide_image'] =
theme('imagecache',
$vars['imgcache_slide'],
$filepath,
$result->node_title);
}
else {
$slider_items[$key1]['slide_image'] =
'<img src="' . base_path() . $filepath .
'" alt="' . $result->node_title .
'"/>';
}
}
// add slide_text variable
if (isset($result->node_data_field_pager_item_text_field_slide_text_value)) {
$slider_items[$key1]['slide_text'] = $result->node_data_field_pager_item_text_field_slide_text_value;
}
// add slide_title variable
if (isset($result->node_title)) {
$slider_items[$key1]['slide_title'] = $result->node_title;
}
// add slide_read_more variable and slide_node variable
if (isset($result->nid)) {
$slider_items[$key1]['slide_read_more'] = l('Read more...', 'node/' . $result->nid);
$slider_items[$key1]['slide_node'] = 'node/' . $result->nid;
}
}
$vars['slider_items'] = $slider_items;
}
}
}
/**
* Override or insert variables into the ddblock_cycle_pager_content templates.
* Used to convert variables from view_fields to pager_items template variables
* Only used for custom pager items
*
* @param $vars
* An array of variables to pass to the theme template.
*
*/
function pixture_reloaded_preprocess_ddblock_cycle_pager_content(&$vars) {
if (($vars['output_type'] == 'view_fields') && ($vars['pager_settings']['pager'] == 'custom-pager')){
$content = array();
// Add pager_items for the template
// If you use the devel module uncomment the following lines to see the theme variables
// dsm($vars['pager_settings']['view_name']);
// dsm($vars['content'][0]);
// If you don't use the devel module uncomment the following lines to see the theme variables
// drupal_set_message('<pre>' . var_export($vars['pager_settings'], true) . '</pre>');
// drupal_set_message('<pre>' . var_export($vars['content'][0], true) . '</pre>');
if ($vars['pager_settings']['view_name'] == 'news_items') {
foreach ($vars['content'] as $key1 => $result) {
// add pager_item_image variable
if (isset($result->node_data_field_pager_item_text_field_image_fid)) {
$fid = $result->node_data_field_pager_item_text_field_image_fid;
$filepath = db_result(db_query("SELECT filepath FROM {files} WHERE fid = %d", $fid));
// use imagecache (imagecache, preset_name, file_path, alt, title, array of attributes)
if (module_exists('imagecache') &&
is_array(imagecache_presets()) &&
$vars['imgcache_pager_item'] <> '<none>'){
$pager_items[$key1]['image'] =
theme('imagecache',
$vars['pager_settings']['imgcache_pager_item'],
$filepath,
$result->node_data_field_pager_item_text_field_pager_item_text_value);
}
else {
$pager_items[$key1]['image'] =
'<img src="' . base_path() . $filepath .
'" alt="' . $result->node_data_field_pager_item_text_field_pager_item_text_value .
'"/>';
}
}
// add pager_item _text variable
if (isset($result->node_data_field_pager_item_text_field_pager_item_text_value)) {
$pager_items[$key1]['text'] = $result->node_data_field_pager_item_text_field_pager_item_text_value;
}
}
}
$vars['pager_items'] = $pager_items;
}
}
#12
Hereby the preprocess functions with the added if statements.
Hope this helps you further, please let me know.
#13
Thanks so much! I'm still getting the error foreach() on line 47 in the upright .tpl. But my existing DDB is fine and still working great.
I'm not sure what I should look at next to fix this up. Is this likely a problem with how I setup the view? they should be identical except one has an additional "filter" in it.
I attatched a screen shot of how it's displaying right now. Just the pager theme appears.
#14
Switched this back to fixed! Because it is! I had a minor typo that was throwing off the view (I didn't know they were case sensitive) I accidentally used a "Cap" when naming the view.
Works perfectly with the code you wrote above. Thank you so much I'm so glad I have this done in time for Monday. I'll be donating to this module next pay cheque yet again. You deserve a beer my good man!
Thanks again! I can repeat this for other sorting I want to do now!
#15
Automatically closed -- issue fixed for 2 weeks with no activity.
#16
thanks for this - very helpful. Sorry to reopen but I want to confirm that it's still the most current code to handle this -- I'm getting "unexpected end" errors when I add a second if statement.
(It works great with just one, but I have two nodequeues I'm using.)
#17
Did you have a look at the template.php file in #12?
Can you add your template.php file, so I can help you better.
#18
Set status to closed, assume poster solved the issue, more then 4 weeks no activity.