OK, I can make Drupal do what I want to do, but, well, my gut instinct tells me it's not elegant and there should be a better solution.

I am embedding a view in theme so that each page of a certain CCK content type shows the view after the main content.

I know it should look like this:

    <?php if ($child_destinations): ?>
      <div class="child_destinations">
        <?php print $child_destinations; ?>
      </div>
    <?php endif; ?>

That is elegant, it's strictly about presentation; that is what should be in the template.

However, this is the code I have put in in the template.

  <div class="content">
    <?php print $content; ?>
  </div>
  
  <div class="child-destinations">
    <?php
    
    //load the view by name
    $view = views_get_view('dynamic_child_node_view');
    
    // Create a filter to select the nodes that have the terms that are children 
    // of the term associated with this node
    if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
  	  $nid = (int)arg(1);
  	  $vid = 1;    // Hard coding a constant here; bad form!
  	  $terms =  taxonomy_node_get_terms_by_vocabulary($nid, $vid);
  	  foreach ($terms as $tid=>$term) {  // Yeah, I know this is a bad way to do it.  
  	  } 
	  }
	
  	// Set a filter
  	$table = 'term_node';
  	$field = 'tid';
  	$value = array(0 => $tid);
  	// views_view_add_filter(&$view, $table, $field, $operator, $value, $option);
  	views_view_add_filter($view, $table, $field, 'OR', $value, '1');
  	views_view_add_filter($view, $table, $field, 'NOR', $value, '0');
  	
  	// Generate a Views 'result' instead of and 'embed' and I will add the HTML wrapping the list myself
  	views_load_cache();
      views_sanitize_view($view);
      $info = views_build_view('result', $view); 
    
      // Loop through the views result and spew out 3 column nested divs, 
      // the three contianer divs float left to make 3 colums for the child destinations.
      $child_count = 0;
      print "<div class=\"child-destination-container\">\r\n";
      while ($row = mysqli_fetch_assoc($info['result'])) {
        print "      <div class=\'child-destination\"><a href=\"/".drupal_get_path_alias("node/".$row['nid'])."\">".$row['node_title']."</a></div>\r\n";
        $child_count++;
        if ($child_count == ceil($view->num_rows/3)) {
          print "    </div>\r\n    <div class=\"child-destination-container\">\r\n"; 
          $child_count = 0;
        }
      }    
      ?>
      </div>
      <div class="clear-float"></div>   
    </div>
  
    <?php if ($links): ?>
      <div class="links">
        <?php print $links; ?>
      </div>
    <?php endif; ?>

Doing it the way I am doing, even though it works, is a violation of the principal of separating code from presentation.

I know how to separate the code from the presentation, but I don't know out WHERE I should be putting the code, according to Drupal best practices.

  • Does the View creation and theming code all logically get tucked in one place--or are there two logical places for it? (One for generating the list and one for theming it?)
  • I am doing so much coding to make Views do what I want and display it the way I need, is it even practical to use Views? Should I just query the tables directly?

Enlighten me. Or at least point me in the right direction....

Comments

jfxberns’s picture

Or would it be a smarter move to append the whole thing to $content?

John Berns
Travel Guide
Travel Photographer

michelle’s picture

Create a function in template.php, call the function from _phptemplate_variables, pass the result as a variable to the node/page, use the code you said you'd like to use at the top.

Michelle

--------------------------------------
See my Drupal articles and tutorials or come check out life in the Coulee Region.

jfxberns’s picture

Thanks Michelle. That's the kind of simple, to the point advice I have been looking for.

Any thoughts on whether it's more efficient to use Views for something like this--or to just write code to query the DB directly?

John Berns
Travel Guide
Travel Photographer

michelle’s picture

http://drupal.org/node/242311 explains it better than I can.

Michelle

--------------------------------------
See my Drupal articles and tutorials or come check out life in the Coulee Region.

jfxberns’s picture

I tried what you suggested, Michelle, but it's not working for me.

I ran the code in a debugger and _phptemplate_variables() is never executed. (That is the _phptemplate_variables function in the template.php file in the current theme folder.)

Am I missing something here? Shouldn't _phptemplate_variables() always be executed when a page is built or is there some action I need to take to initiate a call to _phptemplate_variables()?

Sorry, still new to this.

John Berns
Travel Guide
Travel Photographer

michelle’s picture

Erm... _phptemplate_variables() is called by the theme engine. I'm assuming you're using a phptemplate based theme. If you are, it should be getting called. If it isn't, I have absolutely no idea why, sorry.

Michelle

--------------------------------------
See my Drupal articles and tutorials or come check out life in the Coulee Region.

ericcorriel’s picture

did you ever figure this out? i'm encountering the same problem.