Hello,

What I'm trying to do is a bit hard for me. Even if I've an idea of the things to do to accomplish what I want, I need the help of a PHP expert ;)

So I've created a block which is a ''list view''. I'd like to add a different class to each <li>

For example the first one will <li class="disc-1">, the second will be <li class="disc-2"> and so on...

I think the same system is used to generate the Primary links and taxonomy classes in Drupal.
Maybe this can be done with a counter or something like that ? I'm not a PHP expert at all :)

If you have an idea to help me. Here's the function generated by the Views Theme Wizard for template.php

<?

function phptemplate_views_view_list_Discs($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-Discs', $vars);
  }
  if ($items) {
    return theme('item_list', $items);
  }
}
?>

And here's the source code of the View

  $view = new stdClass();
  $view->name = 'Discs';
  $view->description = '';
  $view->access = array (
);
  $view->view_args_php = '';
  $view->block = TRUE;
  $view->block_title = 'Ecoutez aussi...';
  $view->block_header = '';
  $view->block_header_format = '1';
  $view->block_footer = '';
  $view->block_footer_format = '1';
  $view->block_empty = '';
  $view->block_empty_format = '1';
  $view->block_type = 'list';
  $view->nodes_per_block = '12';
  $view->block_node_offset = '0';
  $view->block_more = FALSE;
  $view->block_use_page_header = FALSE;
  $view->block_use_page_footer = FALSE;
  $view->block_use_page_empty = FALSE;
  $view->sort = array (
    array (
      'tablename' => 'node',
      'field' => 'random',
      'sortorder' => 'ASC',
      'options' => '',
    ),
  );
  $view->argument = array (
  );
  $view->field = array (
    array (
      'tablename' => 'node_data_field_pochette_du_disque',
      'field' => 'field_pochette_du_disque_fid',
      'label' => '',
      'handler' => 'content_views_field_handler_group',
      'options' => 'small',
    ),
    array (
      'tablename' => 'node',
      'field' => 'link',
      'label' => '',
    ),
    array (
      'tablename' => 'node_data_field_nom_de_lalbum',
      'field' => 'field_nom_de_lalbum_value',
      'label' => '',
      'handler' => 'content_views_field_handler_group',
      'options' => 'default',
    ),
    array (
      'tablename' => 'node_data_field_nom_de_lalbum_0',
      'field' => 'field_nom_de_lalbum_0_value',
      'label' => '',
      'handler' => 'content_views_field_handler_group',
      'options' => 'default',
    ),
  );
  $view->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'status',
      'operator' => '=',
      'options' => '',
      'value' => '1',
    ),
    array (
      'tablename' => 'term_node',
      'field' => 'tid',
      'operator' => 'OR',
      'options' => '',
      'value' => array (
  0 => '1',
),
    ),
  );
  $view->exposed_filter = array (
    array (
      'tablename' => 'node',
      'field' => 'status',
      'label' => '',
      'optional' => '0',
      'is_default' => '0',
      'operator' => '0',
      'single' => '0',
    ),
    array (
      'tablename' => 'term_node',
      'field' => 'tid',
      'label' => '',
      'optional' => '0',
      'is_default' => '0',
      'operator' => '0',
      'single' => '0',
    ),
  );
  $view->requires = array(node, node_data_field_pochette_du_disque, node_data_field_nom_de_lalbum, node_data_field_nom_de_lalbum_0, term_node);
  $views[$view->name] = $view;

Thank you.

Matt

Comments

rl’s picture

I know it is not what you're asking, but I know you can write css that will style each entry differently. There is a notation that takes the list item index and allows styling the even differently from the odd, and so forth.

Rich

Anonymous’s picture

Do you mean a strict css solution ? Can you explain it please ?

vm’s picture

its called zebra striping, there are a few posts around , if i bump into them, ill post em.

Anonymous’s picture

I've found something http://drupal.org/node/56165
Now I'm trying to imagine how to apply this to my list view...

Ihave to say that I need 12 different classes, not only ''odd'' and ''even''

Thank you for the idea ;)

Anonymous’s picture

<?php
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'];
      }

?>

This piece of code is a part of the phptemplate_views function, the zebra system seems to be already in it but these classes ''odd'' and ''even" are not printed.

Anyway, as I said, I need a different class for each item, not only two classes.

Matt

nevets’s picture

As I understand it you want to apply class names to each li item. you can achieve that by over riding the default them function for item_list. Start by adding the two functions below to your template.php file, what they do is extend the concept of a list item similiar to the way the table theme function does for data rows. In this case existing uses of the theme function will work like they always have. But if you take your line

    $items[] = _phptemplate_callback('views-list-Discs', $vars);

and change to

    $items[] = array(
        'data' => _phptemplate_callback('views-list-Discs', $vars),
        'class' => 'some-class-name'
    );

each list item will have class of 'some-class-name'. To get a unique class name you could do

    $items[] = array(
        'data' => _phptemplate_callback('views-list-Discs', $vars),
        'class' => 'disc-' . $i;
    );

and now you will have unique class names per item.

Code to add to template.php

function phptemplate_item_list($items = array(), $title = NULL, $type = 'ul') {
  $output = '<div class="item-list">';
  if (isset($title)) {
    $output .= '<h3>'. $title .'</h3>';
  }

  if (!empty($items)) {
    $output .= "<$type>";
    foreach ($items as $item) {
      $output .= phptemplate_item_list_item($item);
    }
    $output .= "</$type>";
  }
  $output .= '</div>';
  return $output;
}

function phptemplate_item_list_item($item) {
  $attributes = '';

  if (is_array($item)) {
    $data = $item['data'];
    foreach ($item as $key => $value) {
      if ($key != 'data')  {
        $attributes .= " $key=\"$value\"";
      }
    }
  }
  else {
    $data = $item;
  }

  return "<li$attributes>$data</li>";
}
Anonymous’s picture

Nevets, each of your answers are Masterclasses :D

The result is exactly what I've been looking for.

Thank you very much.

Matt

nevets’s picture

Glad I could help.