Search code examples
phpdrupaldrupal-7drupal-modulesdrupal-commerce

Entity reference use other fields in view modes


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?


Solution

  • 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;