Here is the example:

Parent Content Type: Author
- field1: Name
- field2: Age
- field3: Picture
- field4: Entity Reference to Books

Child Content Type: Books
- field1: Title
- field2: Description

Author has entity reference to Books using Inline Entity Form. So when creating an "Author", we can add "Books" they have written inline.

When a user visits an individual "Book" page, how can I create a field that links back to the "Author" who wrote it?

I assume I have to create a Book field3 that is an Entity Reference to Author. But how do I make that reference automatic?

Comments

bojanz’s picture

You can't make it automatic.
You add an entityreference field to the child, then implement hook_entity_insert to set the field on the child when the parent entity is first created.

/**
 * Implements hook_entity_insert().
 *
 * When a node of type "author" has been created, set its id
 * on the child book.
 * Done this way because IEF saves the book before the author, making it
 * impossible to know the author nid inside the IEF controller.
 */
function mymodule_entity_insert($entity, $type) {
  if ($type != 'node' || $entity->type != 'author') {
    return;
  }

  $author_wrapper = entity_metadata_wrapper('node', $entity);
  $book_wrapper = $author_wrapper->field_book;
  $book_wrapper->field_author = $author_wrapper->nid->value();
  $book_wrapper->save();
}
hoff331’s picture

@bojanz

I have changed the code to reflect my nodes and field names, however, it is still not working. To better understand my use case, a user creates an "Auction" node. Within the "Auction" parent node, the user can add "IFS" (Items For Sale) child nodes using IEF.

Parent Node = "Auction"
- IEF field called "field_IFS_add"
- user can create multiple "IFS" using IEF

Child Node = "IFS" (Items For Sale)
- entity reference field called "field_ifs_reference"

<?php
/**
 * Implements hook_entity_insert().
 *
 * When a node of type "Auction" has been created, set its id
 * on the child "IFS".
 * Done this way because Iinline Entity Field saves the child entity before the parent entity, making it
 * impossible to know the parent nid inside the Inline Entity Field controller.
 */
function customItemForSale_entity_insert($entity, $type) {
  if ($type != 'node' || $entity->type != 'auction') {
    return;
  }

  $auction_wrapper = entity_metadata_wrapper('node', $entity);
  $IFS_wrapper = $auction_wrapper->field_IFS_add;
  $IFS_wrapper->field_ifs_reference = $auction_wrapper->nid->value();
  $IFS_wrapper->save();
}
?>

However, I receive the following Error:

EntityMetadataWrapperException: Unknown data property field_IFS_add. in EntityStructureWrapper->getPropertyInfo() (line 339 of /home/content/00/2705600/html/.../sites/all/modules/entity/includes/entity.wrapper.inc)

bojanz’s picture

Well, the error means it can't find field_IFS_add on your auction node. Maybe it's field_ifs_add? Case matters

hoff331’s picture

@bojanz

You were correct (IFS should have been "ifs"). However, the module does not perform as anticipated.

Now, if you add child nodes and click "save" on the parent node, the node/add form refreshes and clears all user input. The parent node is not saved.

The child nodes, however, are saved and available in the content. The child nodes do not contain references to the parent node (how could they, the parent node does not exist).

EmanueleQuinto’s picture

Just to refer to a previous discussion that solved my issue related to backreferences similar to the Chris's one.

Sometimes you need a back reference because you need to expose some content from the back reference. As pointed out by Damien in the referenced issue (oh references again) you can use EVA as well.
It works, it's stable and fit the needs for complex visualization of data. It does the job.

bojanz’s picture

Status: Active » Fixed

Nothing more to be done here.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.