简体   繁体   English

将唯一的ID添加到Drupal Views列表分组

[英]Add a unique ID to Drupal Views list groupings

I'm using Views 3 in Drupal 7 to output a list of fields and using a grouping field to generate separate lists. 我正在使用Drupal 7中的Views 3输出字段列表,并使用分组字段来生成单独的列表。 I need each of the groupings to have a unique ID attribute applied to the < ul > but they don't by default. 我需要每个分组都将唯一的ID属性应用于<ul>,但默认情况下不是。

As far as I'm aware I would need to edit the views-view-list.tpl.php template but I don't know how to achieve a unique ID per iteration. 据我所知,我需要编辑views-view-list.tpl.php模板,但我不知道如何在每次迭代中获得唯一的ID。

Can anyone help please? 有人可以帮忙吗?

For future reference: Put a div around everyting in view-views-list.tpl.php. 供将来参考:将div放在view-views-list.tpl.php中。 You can (ab-)use the $title to generate unique (but consistent) id's. 您可以(ab-)使用$ title生成唯一(但一致)的ID。

Do it like this: 像这样做:

<?php $id = str_replace('FOR ALL UNWANTED CHARS','',$title); ?>

<div id="<?php print strtolower($id); ?>">

You can use the $view->dom_id variable. 您可以使用$ view-> dom_id变量。 It is a unique id for that views instance. 这是该视图实例的唯一ID。

In your .tpl.php file: 在您的.tpl.php文件中:

<?php print $view->dom_id; ?>

From comments in modules\\views\\theme\\theme.inc: 从modules \\ views \\ theme \\ theme.inc中的注释中:

<?php
    // It is true that the DIV wrapper has classes denoting the name of the view
    // and its display ID, but this is not enough to unequivocally match a view
    // with its HTML, because one view may appear several times on the page. So
    // we set up a hash with the current time, $dom_id, to issue a "unique" identifier for
    // each view. This identifier is written to both Drupal.settings and the DIV
    // wrapper.
?>

easiest way I can think of off the top of my head... 我可以想到的最简单的方法...

<?php print $wrapper_prefix; ?>
  <?php if (!empty($title)) : ?>
    <h3><?php print $title; ?></h3>
  <?php endif; ?>
  <ul id="<?php echo uniqid(); ?>">
    <?php foreach ($rows as $id => $row): ?>
      <li class="<?php print $classes_array[$id]; ?>"><?php print $row; ?></li>
    <?php endforeach; ?>
  </ul>
<?php print $wrapper_suffix; ?> 

that would go in your views-view-list.tpl.php file. 它将放在您的views-view-list.tpl.php文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM