Drupal 8怎样创建一个当前文章所含术语相关的文章列表区块

By admin, 14 二月, 2015

An example of view block with related nodes based on 'tags'.
1. Create a block (take a look at screenshots below)
2. Add a relationship: Taxonomy term: Content using: Tags
3. Add contextual filter: (field_tags) Content: Nid

When the filter value is NOT available
Provide default value -> Content ID from URL

MORE
check: Exclude
this contextual filter will remove current node from the view
4. Add contextual filter: Taxonomy term: Term ID

When the filter value is NOT available
Provide default value -> Taxonomy term ID from URL
check: Load default filter from term page
check: Load default filter from node page, that's good for related taxonomy blocks
choose the vocabulary (tags in this case)
Multiple-value handling -> Filter to items that share any term

When the filter value IS available or a default is provided
Specify validation criteria -> Basic validation

MORE
check: Allow multiple values
5. Define the fields (only titles in this case)
6. Make the view display only distinct items

Other -> Query settings:
check: Distinct
check: Pure distinct

参考:http://drupal.stackexchange.com/questions/79746/recommend-nodes-based-o…

标签

评论2

Restricted HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id> <img src>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。
验证码
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
请输入"Drupal10"

admin

9 years 2 months 之前

把下面代码放到自定义的模块目录里的src/Plugin/Block/SimilarContentBlock.php可实现等价功能:

 

<?php
namespace Drupal\howto\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
 * Provides Similar content by taxonomy terms Block
 *
 * @Block(
 *   id = "similar_content_block",
 *   admin_label = @Translation("Similar Content"),
 * )
 */
class SimilarContentBlock extends BlockBase {
  /**
   * {@inheritdoc}
   */
  public function build() {
    $node = \Drupal::routeMatch()->getParameter('node');
    if ($node) {
      $nid = $node->id();
      $result = db_query_range('SELECT DISTINCT n.nid, n.title
          FROM node__field_tags t1
          INNER JOIN node__field_tags t2
          ON t1.entity_id = :nid AND t1.field_tags_target_id = t2.field_tags_target_id
          INNER JOIN node_field_data n
          ON t2.entity_id = n.nid
          WHERE n.nid != :nid
          ORDER BY n.created DESC', 0, 5,
          array(':nid' => $nid))->fetchAll();
      
      if (count($result) > 0) {
        $html = '<div class="block-views">';
        foreach ($result as $row) {
          $html .= '<div class="views-row">' . \Drupal::l($row->title,
              \Drupal\Core\Url::fromUri('entity:node/' . $row->nid)) .
              '</div>';
        }
        $html .= '</div>';

        return array(
            '#markup' => $html,
            '#cache' => array('contexts' => array('url')),
        );
      }
    }
    
    return array();
  }
}

最新评论