Drupal 分类块、词汇表

发布于 2024-09-25 10:32:32 字数 72 浏览 11 评论 0原文

我正在寻找实现列出特定词汇表的所有术语的块的最佳方法。每个术语应链接到列出与该术语关联的所有节点的页面。任何帮助将不胜感激。谢谢!

I looking for the best way to implement a block that lists all terms of a certain vocabulary. Each term should link to page that lists all nodes associated with that term. Any help would be greatly appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

埋葬我深情 2024-10-02 10:32:32

解决这个问题的最简单方法可能是使用 Views,并简单地创建一个类型为“的新视图”学期”。下面是一个简单的示例,假设您对视图 UI 有一定的了解:

  1. 访问视图 >添加(构建/视图/添加),为新视图命名,然后从“视图类型”单选按钮中选择术语
  2. 在下一页上,首先添加分类:词汇过滤器并在过滤器设置中选择您的词汇表。
  3. 添加分类:术语字段,并在字段设置中启用将此字段链接到其分类术语页面选项。您可能还想删除该字段的标签,因为这只是一个简单的列表。
  4. 您可能希望显示屏显示词汇表中的所有术语,因此将“要显示的项目”更改为0(无限制)。默认情况下,新视图一次仅显示 10 个项目。
  5. 查看下面的实时预览,看看它是否输出了您需要的内容。
  6. 使用视图 UI 左侧的下拉菜单添加新的阻止显示。
  7. 在“块设置”区域中为您的新块命名。这是将出现在 Drupal 的块管理页面上的描述。
  8. 保存您的视图并访问 admin/build/block 来放置和配置您的块。

值得注意的是,视图确实有一些开销,但根据我的经验,它的灵活性和易用性远远超过了相对较小的性能影响。

如果您想避免使用视图,您可以使用 hook_block() 编写一个非常简单的自定义模块并调整 http:// /drupal.org/node/247472。如果您愿意,我可以使用基于此的示例模块来编辑此答案。

The easiest way to approach this would probably be to use Views, and simply create a new view of the type "term". Here's a quick example which assumes that you have some basic familiarity with the Views UI:

  1. Visit Views > Add (build/views/add), give your new view a name, and select Term from the "View type" radio buttons.
  2. On the next page, start by adding a Taxonomy: Vocabulary filter and selecting your vocabulary in the filter settings.
  3. Add a Taxonomy: Term field and enable the Link this field to its taxonomy term page option in the field settings. You might also want to remove the field's label, since this is just a simple listing.
  4. You probably want your display to display all terms in your vocabulary, so change the "Items to display" 0 (unlimited). By default, new views only display 10 items at a time.
  5. Check out the Live preview below to see if it's outputting what you need.
  6. Add a new Block display using the dropdown on the left side of the Views UI.
  7. Give your new block a name in the "Block settings" area. This is the description that will appear on Drupal's block admin page.
  8. Save your view and visit admin/build/block to place and configure your block.

It's worth noting that Views does indeed have some overhead, but in my experience, its flexibility and ease-of-use far outweigh the relatively minor performance hit.

If you'd like to avoid using Views, you could write a pretty simple custom module using hook_block() and adapting http://drupal.org/node/247472. If you'd like, I can edit this answer with an example module based on that.

分開簡單 2024-10-02 10:32:32

(将其作为另一个答案发布,因为这是与我的第一个答案不同的方法。)

正如我上面提到的,这是另一种涉及基于 http://drupal.org/node/247472。您也可以将该代码放入选择“PHP”输入格式的自定义块中,但这通常被认为是不好的做法。

在sites/all/modules 中添加一个名为vocabulary_block 的新文件夹。自定义并添加以下两个文件:

vocabulary_block.module

<?php
/**
 * @file
 * Exposes a block with a simple list of terms from [vocabulary]. 
 * Each term is linked to its respective term page.
 */

/**
* Lists terms for a specific vocabulary without descriptions.
* Each term links to the corresponding /taxonomy/term/tid listing page.
*/
function vocabulary_block_get_terms($vid) {
  $items = array();

  $terms = taxonomy_get_tree($vid,  0, -1, 1);
  foreach($terms as $term) {
    $items[]= l($term->name, "taxonomy/term/$term->tid");
  }
  if(count($items)) {
    return theme('item_list', $items);
  }
}

/**
 * Implementation of hook_block().
 */
function vocabulary_block_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {

    case 'list':
      $blocks[0]['info'] = t('List of [vocabulary] terms');
      return $blocks;

    case 'view':
      if ($delta == 0) {
        $vid = 43;
        $block['subject'] = t('[Vocabulary]');
        $block['content'] = vocabulary_block_get_terms($vid);
      }

      return $block;
  }    
}

vocabulary_block.info

name = Vocabulary Block
description = Exposes a block with a simple list of terms from [vocabulary]. Each term is linked to its respective term page.

; Core version (required)
core = 6.x

; Package name (see http://drupal.org/node/101009 for a list of names)
package = Taxonomy

; Module dependencies
dependencies[] = taxonomy

Notes

  • 请务必更改 $vid = 43 ;
    反映词汇表的ID
    你想加载。您可以找到
    访问视频
    管理/内容/分类并查看
    编辑的目的地
    您的词汇表
    链接
    词汇。 VID 将是最后一个
    该 URL 的片段:
    管理/内容/分类/编辑/词汇/[vid]

    我通常不会硬编码
    $vid 到模块本身。然而,
    设置必要的 Drupal
    变量和管理形式(以
    允许用户选择词汇
    来自 Drupal 界面)将是
    这个答案有点过头了。

  • 出于您自己的文档目的,
    不要忘记搜索/替换
    [词汇] 在这两个文件中和
    使用你自己的词汇名称
    相反。

  • 此方法不一定性能更高
    比我描述的 Views 方法
    早些时候,特别是当你开始考虑缓存时,
    优化等
    由于性能是优先考虑的,
    我建议彻底测试
    此页面上的各种不同方法以及
    选择最适合您的方法。

(Posting this as another answer, since this is a different approach than my first answer.)

As I mentioned above, here's another approach involving a custom module based on the code at http://drupal.org/node/247472. You could also just drop that code into a custom block with the "PHP" input format selected, but that's generally considered to be bad practice.

Add a new folder in sites/all/modules called vocabulary_block. Customize and add the following two files:

vocabulary_block.module

<?php
/**
 * @file
 * Exposes a block with a simple list of terms from [vocabulary]. 
 * Each term is linked to its respective term page.
 */

/**
* Lists terms for a specific vocabulary without descriptions.
* Each term links to the corresponding /taxonomy/term/tid listing page.
*/
function vocabulary_block_get_terms($vid) {
  $items = array();

  $terms = taxonomy_get_tree($vid,  0, -1, 1);
  foreach($terms as $term) {
    $items[]= l($term->name, "taxonomy/term/$term->tid");
  }
  if(count($items)) {
    return theme('item_list', $items);
  }
}

/**
 * Implementation of hook_block().
 */
function vocabulary_block_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {

    case 'list':
      $blocks[0]['info'] = t('List of [vocabulary] terms');
      return $blocks;

    case 'view':
      if ($delta == 0) {
        $vid = 43;
        $block['subject'] = t('[Vocabulary]');
        $block['content'] = vocabulary_block_get_terms($vid);
      }

      return $block;
  }    
}

vocabulary_block.info

name = Vocabulary Block
description = Exposes a block with a simple list of terms from [vocabulary]. Each term is linked to its respective term page.

; Core version (required)
core = 6.x

; Package name (see http://drupal.org/node/101009 for a list of names)
package = Taxonomy

; Module dependencies
dependencies[] = taxonomy

Notes

  • Be sure to change $vid = 43; to
    reflect the ID of the vocabulary that
    you'd like to load. You can find the
    VID by visiting
    admin/content/taxonomy and looking at
    the destination of the edit
    vocabulary
    link for your
    vocabulary. The VID will be the last
    fragment of that URL:
    admin/content/taxonomy/edit/vocabulary/[vid].

    I wouldn't normally hard-code the
    $vid into the module itself. However,
    setting up the necessary Drupal
    variable and administration form (to
    allow users to select a vocabulary
    from the Drupal interface) would be
    overkill for this answer.

  • For your own documentation purposes,
    don't forget to search/replace
    [vocabulary] in those two files and
    use your own vocabulary's name
    instead.

  • This method may not necessarily be more performant
    than the Views method I described
    earlier, especially once you start considering caching,
    optimization, etc.
    Since performance is a priority,
    I recommend thoroughly testing a
    variety of different methods on this page and
    choosing whichever one is fastest for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文