Hi Guys,

Feel kind of dumb here, but working on my first Drupal and I hit a bump. I created a view and cannot figure out how to insert it to the front page. The view is a block view and I used the theme generator to provide code for my themes template file and to create a tpl.php file to format the look of the view. Now where I am stuck is figuring out how to insert that into my front page? I believe I call the function from the template file correct? I am using front_page, so I would have to call the PHP variable or use some type of include? Here is the function that was generated:

/**
 * views template to output a view.
 * This code was generated by the views theming wizard
 * Date: Tue, 07/03/2007 - 17:42
 * View: news_block
 *
 * This function goes in your template.php file
 */
function phptemplate_views_view_list_news_block($view, $nodes, $type) {
  $fields = _views_get_fields();

  $taken = array();

  // Set up the fields in nicely named chunks.
  foreach ($view->field as $id => $field) {
    $field_name = $field['field'];
    if (isset($taken[$field_name])) {
      $field_name = $field['queryname'];
    }
    $taken[$field_name] = true;
    $field_names[$id] = $field_name;
  }

  // Set up some variables that won't change.
  $base_vars = array(
    'view' => $view,
    'view_type' => $type,
  );

  foreach ($nodes as $i => $node) {
    $vars = $base_vars;
    $vars['node'] = $node;
    $vars['count'] = $i;
    $vars['stripe'] = $i % 2 ? 'even' : 'odd';
    foreach ($view->field as $id => $field) {
      $name = $field_names[$id];
      $vars[$name] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
      if (isset($field['label'])) {
        $vars[$name . '_label'] = $field['label'];
      }
    }
    $items[] = _phptemplate_callback('views-list-news_block', $vars);
  }
  if ($items) {
    return theme('item_list', $items);
  }
}

Comments

inforeto’s picture

The function you posted is merely to format the view, you can insert it directly in the frontpage.
You can do it in the theme, but it is probably an overkill.

If it is a block view, see if it appears in the list of blocks, then edit the block to assign it to the frontpage.
I see you posted here: http://drupal.org/node/154072, but inserting a block is different than a inserting a view.

You can insert a view anywhere you can run php.
Search the site for the functions to insert views (http://drupal.org/node/48816)
or use http://drupal.org/project/insert_view
In frontpage, you just use a node of the page type and make it sticky and promoted and insert there.
Or make a custom block, put it in the frontpage, and insert there with php.

Also, you can replace the whole frontpage in the settings.

bfdexp’s picture

Hi thanks for the reply. I am trying to customize the views layout, so I need to do that with CSS in a tpl.php file. I did that and inserted the above code in the frontpage and it displays nothing. Any suggestions?

inforeto’s picture

Yes, the above code is needed, but you now need to add the function to insert the view.

Find the codes here: http://drupal.org/node/48816

//load the view by name
$view = views_get_view('sample_view');
//output the view
print views_build_view('embed', $view);
bfdexp’s picture

OK, thanks that worked OK. Of course another issue. I am using a List View to generate a form of teasers. I have them all laid out OK, but one speed bump. The body field is showing in full view. I even have the teaser format selected for the handler in the views UI. I am thinking maybe because it is list view, instead of teaser view for the view type? If so is there a way to get a teaser view of the body in list view? Thanks again for the help.

inforeto’s picture

Yes, you need to select teaser view.

For more advanced usage, you can print anything you want using php code.
This can't be done in the views UI, but in the templates. It is called "theming" the view.
(search for theme views)