1

The Problem

I have a couple of entity reference fields that references a hierarchy of nodes. For the sake of simplicity let's use just the top level example Video Game -> Video Game Release

So when I look at the video game "Assassin's Creed 2" I see two releases "Assassin's Creed 2" and "Assassin's Creed Ezio Trilogy".

Background info that isn't necessarily important to the question:

! The reason for the entity reference is on "Assassin's Creed Brotherhood" I would also have two releases "Assassin's Creed Brotherhood" and "Assassin's Creed Ezio Trilogy".

  • Assassin's Creed 2 (Node: game, ID: 1)
    • Assassin's Creed 2 (Node: release, ID: 2)
    • Assassin's Creed Ezio Trilogy (Node: release, ID: 3)
  • Assassin's Creed Brotherhood (Node: game, ID: 4)
    • Assassin's Creed Brotherhood (node: release, ID: 5)
    • Assassin's Creed Ezio Trilogy (Node: release, ID: 3)

When I view the node "Assasin's Creed 2" I'm currently seeing both releases one after the other. I styled these using display suite and they look fine. However I want to display only one of them at a time with a tab, select list, or something of the like to allow me to switch between them.

My thoughts

  • I was unable to find a module that could perform this but I feel one may exist.
  • I could use a simple jquery tab ajax but wasn't sure if their was already a drupal way to do this.
  • Perhaps I could use exposed views and a views embed field to show all the referenced nodes but then the performance would suffer significantly.

The question

How do I display an entity reference field as tabbed or select list content? Preferably without the performance drop of an embedded view.

0

You have 2 relatively simple options without creating your own module and field display code (in a module).

  1. use Views to generate the select lists you want. You can do this for example with a ctools dropdown or jumpto displays. While you're using a view you can always set Views to cache the results of your view so its not cumbersome after the 1st page load for all other visitors. You can even define views and attach them to your node if you want with field_views_field module.

  2. Use Display Suite on your site and create a Custom Field in DS for any fields you want custom output for, like your select list. Then in the PHP area of the custom field you can access the currently loaded $entity fields and generate whatever HTML you want for your field output. You would then hide the default field display of your field, and show your custom DS created field for your content type.

I'm about to do (2) above with a slideshow content type where per-slide the content editor can select the text foreground and background color using the field_color module which would best match an uploaded image to the Node. Then in DS I will make a custom "slide_text" dyanamic field that examines the color values and basically outputs:

<?php
// This is pseudocode and untested.
$wrapper = entity_metadata_wrapper('node' $entity);
$text_color = $wrapper->field_foreground_color->value();
$bg_color = $wrapper->field_background_color->value();
$text = $wrapper->field_slide_text->value();
echo <<<EOFHTML
<span class="ds-slide-content-field"
      style="color: $text_color;
             background-color: $bg_color">
    $text
</span">
EOFHTML;
?>
  • Neat idea. Though on #2 couldn't you just use js instead of php. All the content is already on the page so you could just use the select list as a trigger to hide all then reveal the selected item. – danielson317 Jun 29 '16 at 15:45
  • Sure you can use JS ... if you want your site to not function if the client has javascript disabled. The idea is to get Drupal (not the client side) to emit whatever markup you want via a field template override, or in this case a pseudo custom field ... :) – tenken Jun 29 '16 at 15:50
  • lol. If my user doesn't have js enabled I will be very happy to give them the worst experience possible. The only people who don't have js enabled is privacy freaks and hackers and I care about neither demographic. Welcome to the year 2005+ if you don't have js enabled you don't belong on the internet. – danielson317 Jun 29 '16 at 15:52
  • ok. drupal core makes use of jquery and javascript, but it always provides a no-js url as a safe fallback. I work in HigherEd so these are considerations I need to take into account. My main point is simply that my solution(s) leverage drupal and it's API's without incurring a huge overhead persay. If you want to just create some JS then you need not interact with nor extend drupal at all and have it. – tenken Jun 29 '16 at 15:58

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.