0

I'm using the entityreference module to provide my "Concert" content type with a "Program" field (machine name "field_program"), containing multiple "Song" nodes. Each Song just has two fields: Title and Composer (machine names "title" and "field_composer").

What's the best way to control the display of a Program field within a Concert node? I tried creating a "node--field_program.tpl.php" file, but that didn't seem to have an effect. I also created a View that lists Song nodes in basically the way I'd want, but I don't know how to use that in the context of displaying a Concert node.

1 Answer 1

1

I'd recommend replacing program field with a custom View of Songs. That will allow you to pull any data that you'd need from the Song content type.

The least cumbersome (and I'd say flexible) way of doing involves two steps:

  1. Create a View of Songs that will take a Node ID (NID as a contextual filter)
  2. Override the field template for the Program field and instead of rendering the default template of the field - render a View crated in Step 1.

I'll detail each step, creating a View for this purpose can be somewhat complex, but that will give you great control over how you want to present the information.

STEP 1 - CREATE A VIEW

  1. Create new View of "Content" of the type "Concert". Select Block instead of Page. Make it a view of "Unformatted List" of "Fields". In the number of items put 0 to display all items.
  2. Add new "Contextual Filter" - "Content: NID". Provide default value: "content ID from URL"
  3. Add new Relationship to "Entity Reference: field_program", which will give you a connection to each song selected in the filed Program for the particular node. Make relationship required.
  4. Now remove all default fields that were placed in your View and add fields from the Songs content type (Title and Composer). Select the relationship that you added in previous step to each field (this is important, otherwise View won't work).
  5. You can test your View by entering a Node Id from any existing Concert nodes into Views Preview placeholder for contextual filters. Without given contextual filter, preview will not show you any results (that's expected)

STEP 2 - OVERRIDE PROGRAM FIELD TEMPLATE

  1. Copy field.tpl.php from modules/field/theme and place it into your current theme template folder.

  2. Rename the file. Now here you need to decide if you wish to override the display of the field "Program" everywhere on your website or only when it is displayed in content type Concert.

    field--field-name--content-type.tpl.php - Will target a specific field name within a specific content type. field--field-name.tpl.php - Will target all fields of a specific machine name.

I'd recommend starting with the first choice - field--field-program--concert.tpl.php

Clear cache to let Drupal see your new template.

Now remove everything that was originally in that template, you can leave just the top comments for the sake of maintainability, etc. And add the code below - which will embed the view you create in the STEP 1.

<?php 
print views_embed_view('machine_name_of_the_view', 'machine_name_of_the_block', $element['#object']->nid);
?>

$element['#object']->nid - is an argument that you can to your View. It is not absolutely necessary to place it here, because our View already has contextual filter that takes Content ID from Url by default. So additional argument just gives another chance to override that argument, or make sure that it will be passed appropriately.

That's it. Give it a whirl. Don't forget to test.

1
  • That sounds fantastic, thanks for the suggestion. I'll give it a whirl. Commented May 23, 2016 at 3:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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