Reply to comment

Drupal: Even und Odd in Listen, insbesondere Benutzerlisten in Blöcken hinzufügen

Oft ist es (zumindest bis CSS3) sinnvoll, Listeneinträgen zeilenweise unterschiedliche Klassen zu geben (Even/Odd) um diese ansprechen zu können.

Diesen Wunsch hatten wir beispielsweise bei den Benutzerlisten in den Blöcken unserer Rollerseiten.

Da Drupal das Konzept nicht konsistent durchzieht, hier unsere Lösung:

  1. template.php im entsprechenden Design öffnen
  2. Folgende Funktion einfügen und entsprechend benennen (THEME durch gewählten Themenamen ersetzen

  1. /**
  2.  * Return a themed list of items.
  3.  *
  4.  * @param $items
  5.  *   An array of items to be displayed in the list. If an item is a string,
  6.  *   then it is used as is. If an item is an array, then the "data" element of
  7.  *   the array is used as the contents of the list item. If an item is an array
  8.  *   with a "children" element, those children are displayed in a nested list.
  9.  *   All other elements are treated as attributes of the list item element.
  10.  * @param $title
  11.  *   The title of the list.
  12.  * @param $type
  13.  *   The type of list to return (e.g. "ul", "ol")
  14.  * @param $attributes
  15.  *   The attributes applied to the list element.
  16.  * @return
  17.  *   A string containing the list output.
  18.  */
  19. function speedy2web_item_list($items = array(), $title = NULL, $type = 'ul', $attributes = NULL) {
  20.   $output = '<div class="item-list">';
  21.   if (isset($title)) {
  22.     $output .= '<h3>'. $title .'</h3>';
  23.   }
  24.  
  25.   if (!empty($items)) {
  26.     $output .= "<$type". drupal_attributes($attributes) .'>';
  27.     $num_items = count($items);
  28.     foreach ($items as $i => $item) {
  29.       $attributes = array();
  30.       $children = array();
  31.       if (is_array($item)) {
  32.         foreach ($item as $key => $value) {
  33.           if ($key == 'data') {
  34.             $data = $value;
  35.           }
  36.           elseif ($key == 'children') {
  37.             $children = $value;
  38.           }
  39.           else {
  40.             $attributes[$key] = $value;
  41.           }
  42.         }
  43.       }
  44.       else {
  45.         $data = $item;
  46.       }
  47.       if (count($children) > 0) {
  48.         $data .= theme_item_list($children, NULL, $type, $attributes); // Render nested list
  49.       }
  50.       //#JP190409: Even und Odd immer in Listen:
  51.       if ($i&1) {
  52.         $attributes['class'] = empty($attributes['class']) ? 'odd' : ($attributes['class'] .' odd');
  53.       } else {
  54.         $attributes['class'] = empty($attributes['class']) ? 'even' : ($attributes['class'] .' even');
  55.       }
  56.       //#JP190409: Ende
  57.      
  58.       if ($i == 0) {
  59.         $attributes['class'] = empty($attributes['class']) ? 'first' : ($attributes['class'] .' first');
  60.       }
  61.       if ($i == $num_items - 1) {
  62.         $attributes['class'] = empty($attributes['class']) ? 'last' : ($attributes['class'] .' last');
  63.       }
  64.       $output .= '<li'. drupal_attributes($attributes) .'>'. $data ."</li>\n";
  65.     }
  66.     $output .= "</$type>";
  67.   }
  68.   $output .= '</div>';
  69.  
  70.   return $output;
  71. }

Drupal - Version ist Drupal 6.10, die Vorgehensweise wird aber wohl bei zukünftigen Versionen identisch sein.

Einordnung:

Reply

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Internal paths in single or double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or path. Paths to files in single or double quotes, written as "files:somefile.ext", for example, are replaced with the appropriate URL that can be used to download the file.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><blockquote>
  • Lines and paragraphs break automatically.
  • Pairs of<blockquote> tags will be styled as a block that indicates a quotation.
  • You can enable syntax highlighting of source code with the following tags: <c>, <cpp>, <css>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <php>, <python>, <ruby>, <smarty>, <xml>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Textual smileys will be replaced with graphical ones.
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.