0

I have a student books products in drupal 7 which they have a companion teacher's book product. I want to make a view mode that presents the student book (product display) along with an entity reference to teacher's book which is also a book product. Thing is that I can display the either id, title, or rendered entity but not other entity fields. I want to display is this:

Student's ISDN: ______

Teacher's's ISDN: ______

... Other product fields (Student) ...

I 've tried several modules like display suite but nothing, can you please help? what I 'm missing?

Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
dotoree
  • 2,983
  • 1
  • 24
  • 29

2 Answers2

1

A quick solution is to create a new node template for your content type. E.g: node--student.tpl.php, then use the following code as example:

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

Hope this helps.

Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
  • I don't use views, I just use view mode for displaying a single node. – dotoree Feb 10 '13 at 09:44
  • Not sure if node_load works with entities, I am trying to do it through module using hook_node_view() – dotoree Feb 10 '13 at 10:14
  • This code retrieves the `nid` from the referencing node, then uses `node_load` to load the node object. Nothing strange :) – Muhammad Reda Feb 10 '13 at 10:44
  • I use entity reference. I get entity this way $teachers_book_entity = $node->field_teacher_book[LANGUAGE_NONE][0]['entity']; Also I don't need to print a view mode (teaser) just a field: ISBN. – dotoree Feb 10 '13 at 11:09
  • If you load the entire teacher book node using the node_load method above, you should then be able to get to any of the fields... – Boriana Ditcheva Feb 11 '13 at 15:09
1

I did it this way:

  // 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;
dotoree
  • 2,983
  • 1
  • 24
  • 29