2

I'm currently working through self-set tasks to learn Views and pre-processing in Drupal 7. I've created an unformatted view which creates a list of images (which are linked to a content-type but it seems if I replace the fields in view with node:type then I get a link to the node page which is not what I'm after) to place them in a block. I've placed the block into the highlight region and have written a views-view-fields--image.tpl.php file to output the list into various unordered list formats which are then transformed by jCarousel or the featureList jQuery plug-ins.

On top of this, I've created a page--front.tpl.php file to restyle the front page and it appears to work in that the CSS, js and HTML are working correctly and the carousel and lists appear & work in the defined block.

However when I leave the front page to another page where the view should not appear, I get the following error:

Notice: Undefined property: stdClass::$_field_data in iaintest_preprocess_views_view_fields() (line 10 of /Applications/XAMPP/xamppfiles/htdocs/drupal/sites/all/themes/iaintest/template.php).

To pre-process the list that the view outputs, I've written the following pre-process code:

function iaintest_preprocess_views_view_fields (&$variables) {
  if (!$variables['view']->result[0]->_field_data) {
    // Line 10 where the error occurs.
  }
  else {
    drupal_add_css(path_to_theme() . '/jcarousel/skins/tango/skin.css');
    drupal_add_css(path_to_theme() . '/featureList.css');
    $js = "
      jQuery.noConflict();
      jQuery(document).ready(function(){
      jQuery('#mycarousel').jcarousel();

      jQuery.featureList(
        jQuery('#tabs li a'),
        jQuery('#output li'), {
          start_item: 1
        }
      );
    });";

    drupal_add_js($js, 'inline');
    $items = array();

    foreach ($variables['view']->result as $key => $vars) {
      foreach ($variables['view']->result[0]->_field_data['nid']['entity']->field_images['und'] as $k => $v) {
        $variables['carousel'][] = array(
          'uri' => $v['uri'],
          'alt' => $v['alt'], 
          'title' => $v['title']
        );
      }
    }
  }
}

I have tried using drupal_is_front_page() or $variable['is_front'] === TRUE to limit this part of the preprocessing to the front page, which is the only place where the original view appears.

Is this possible at all, or have I misunderstood anything?

2
  • Hello ipe, and welcome on Drupal Answers. I guess your question is then why the error appears in other pages, and not just the front page.
    – apaderno
    Mar 31, 2011 at 0:34
  • It doesn't explain why you see that error in pages that are not the front page, but the nested foreach()-statements you are using seem weird.
    – apaderno
    Mar 31, 2011 at 0:45

2 Answers 2

1

Try:

<?php
if (isset($variables['view']->result[0]->_field_data)) {
// 
?>

Instead of your weird if/else construct. Not really sure what you are trying to accomplish with that code, so there might be a better way to check if you want to execute it or not.

1
  • Cheers, @Berdir. I've been chasing my tail and forgot about isset. I only wanted to check if _field_data was set or not in the section (and if not, ignore the rest of that code).
    – ipe
    Mar 30, 2011 at 18:03
1

To make a summary of what you are experiencing:

  • Your preprocess function is invoked for a view template, which is used only on the front page.
  • When you visit the front page, you don't get any message error, but you see an error message in the page you visit right after.
  • You are wondering why the error message doesn't appear in the front page, but it is shown in the other pages.

What happens is that:

  • Drupal intercept the PHP warning and, if set to show the error/warning messages, calls drupal_set_message() to show it.
  • drupal_set_message() save the error message in a session variable.
  • As the page has been already output, the error messages are not displayed; they are displayed when the next page is rendered.

In other words, the error is caused by the function being invoked for the front page, but the error message is shown in the next page being output.

To explain in details what happen when PHP raises an error, drupal_bootstrap(), the function that is invoked to boot Drupal, calls _drupal_bootstrap_configuration(), which sets _drupal_error_handler() as PHP error handler. _drupal_error_handler() calls _drupal_error_handler_real(), which then calls _drupal_log_error(), the function that then calls drupal_set_message().

Your Answer

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

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