1

I have content type called collection that has an entity reference field (multiple) that references another content type called books. One the collection node page, I want to render all of the books as linkable titles, with a button next to them that says "add annotation to book" this will link the user to create a third type (entity) that has a reference field pre - populated.

So my question is this, how do I re- format the books fields of my collection type to meet my use case. So far I have toyed with the following options.

  1. Add a custom node--book.tpl.php file and place code that take the reference field, loops through it, does and entity load on each referenced entity, then adds custom html to the add form of the third content type

  2. Alter the field--type.tpl,php and add it in here - can't figure out this option because I'm not sure how to retrieve the nid, link, and title from this template.

  3. Create a custom display for my book entity and set the field on my collection to render as this new display (this created other problems, I don't know if it's possible to have a separate template for this new display, and this complicated it).

0

I think your information architecture in inverted. It sounds more like Book is in Collection rather than Collection has Book. In other words Collection is an attribute of Book, not the other way around. It would be easier to put the reference field on the Book content type, and create a view of all Book s referencing a Collection to display on the Collection content type, using a Contextual filter to limit the list. No changes required to template files!

Then using the Prepopulate module, you can use the $_REQUEST variable to set your field_book value in the new node, and it is a simple matter to add the Content:Nid to the view and rewrite the field to create the link.

First, install and activate the Prepopulate module. Then put your Entity Reference field on the Book content type and limit it to the Collection content type. Then add an Entity reference field to the Annotation content type and limit it to Book. In other words, you are identifying the Book the annotation belongs to.

Then create a view of Content (Book) with a display type of Block. In the views configuration, create a Contextual filter on Content: Collection (field_collection) and configure it to Provide default value and select Entity ID from URL.

Add the field Content:NID and configure the field. Select Rewrite results and check Rewrite the output of this field. In the text box enter something like:

<a href="/node/add/annotation?edit%5Bfield_collection%5D%5Bund%5D%5B0%5D%5Btarget_id%5D=[nid]">Annotate this book</a>

(%5B = "[" and %5D = "]"). The actual link code may vary depending on how you have implemented the Entity reference field in your content type. The module has good documentation.

Save your view and under /admin/structure/block find the block created by the view and set it to display only on pages of type Collection.

The block will then display a list of book titles (linked to their content) and the custom link to add an Annotation page with field_book prepopulated, and limited to those books which reference the Collection being viewed.

  • A book can belong to more than one collection, I am used to doing it the other way around, but only when it's a one to one relationship ... is there a disadvantage to doing it the way I'm doing it (other than the problem at hand) – user379468 Jun 11 '13 at 19:38
  • 1
    Yes there is a disadvantage. The conceptual data model should look like Collection <- Book <- Annotation. That way the attributes of Book are available when you need them. The Book can reference several Collections but you need the Book information to create the Annotation. It is also easier to maintain (and change) the list of Collections for the book than the reverse. The way you are doing it, you need to know something about the Collection in order to create an Annotation. Annotation should only depend on Book! Much more efficient. Less problems. – Triskelion Jun 11 '13 at 19:54
  • I trust that you are right about modeling data in this fashion, just out of curiosity lets say I am going to have a finite amount of books, say 100, but I might have 1000 plus collections ... (im only using book collection annotation as an example), is this still advisable? – user379468 Jun 11 '13 at 20:31
  • Do you annotate the Book or the Collection? The same model would apply if your Annotation is on the Book, regardless of quantities. You could think of Collection as a key word or tag, describing the book. You see the same sort of thing in eCommerce when a product may be part of many different product types, usually done in Drupal using taxonomy references on the Product. – Triskelion Jun 11 '13 at 20:47
  • I see your point, and thanks, just for balance sake, I found the original video I was basing my scenerio on vimeo.com/18460761 – user379468 Jun 11 '13 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.