【问题标题】:Entity reference use other fields in view modes实体引用在视图模式中使用其他字段
【发布时间】:2013-02-10 09:31:32
【问题描述】:

我在 drupal 7 中有一个学生书籍产品,他们有一个同伴教师的书籍产品。我想制作一个视图模式,显示学生书籍(产品展示)以及对教师书籍的实体引用,这也是书籍产品。 问题是我可以显示 id、title 或呈现的实体,但不能显示其他实体字段。我要显示的是这样的:

学生的 ISDN:______

教师的 ISDN:______

...其他产品领域(学生)...

我已经尝试了几个模块,比如显示套件,但没有,你能帮忙吗?我错过了什么?

【问题讨论】:

    标签: drupal drupal-7 drupal-modules drupal-commerce


    【解决方案1】:

    一个快速的解决方案是为您的内容类型创建一个新的节点模板。例如:node--student.tpl.php,则以如下代码为例:

    $referenced_node = node_load($node->field_ref[LANGUAGE_NONE]['0']['target_id']);
    print node_view($referenced_node, "teaser");
    

    希望这会有所帮助。

    【讨论】:

    • 我不使用视图,我只是使用视图模式来显示单个节点。
    • 不确定 node_load 是否适用于实体,我正在尝试使用 hook_node_view() 通过模块来完成
    • 此代码从引用节点检索nid,然后使用node_load 加载节点对象。没什么奇怪的:)
    • 我使用实体引用。我以这种方式获得实体 $teachers_book_entity = $node->field_teacher_book[LANGUAGE_NONE][0]['entity'];此外,我不需要打印视图模式(预告片),只需一个字段:ISBN。
    • 如果您使用上面的 node_load 方法加载整个教科书节点,那么您应该能够访问任何字段...
    【解决方案2】:

    我是这样做的:

      // Initial weight
      $weight = 2;
      // Student's book entity
      $student_book_entity = $node->field_student_book[LANGUAGE_NONE][0]['entity'];
    
      // Get Student's book ISBN and alter some attributes
      $student_isbn_field = array_merge(field_view_field('commerce_product', $student_book_entity, 'field_book_isbn'), array(
          '#field_name' => 'field_students_book_isbn',
          '#title' => t('Student\'s Book ISBN'),
          '#weight' => $weight++,
        )
      );
      $node->content['field_students_book_isbn'] = $student_isbn_field;
    
      // Teacher's book entity
      $teachers_book_entity = $node->field_teacher_book[LANGUAGE_NONE][0]['entity'];
    
      // Get Teacher's book ISBN and alter some attributes
      $teacher_isbn_field = array_merge(field_view_field('commerce_product', $teachers_book_entity, 'field_book_isbn'), array(
          '#field_name' => 'field_teachers_book_isbn',
          '#title' => t('Teacher\'s Book ISBN'),
          '#weight' => $weight++,
        )
      );
      $node->content['field_teachers_book_isbn'] = $teacher_isbn_field;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-19
      • 2014-08-30
      • 2021-01-19
      • 1970-01-01
      相关资源
      最近更新 更多