1

I am building a site where I need to display a list of diseases in a view, and I need to be able to display them both by their titles and by another field (an abbreviated title). The two fields need to display as if they were two separate nodes both linking back to the same node though.

For example: My users need to be able to find the "Immunosuppressive Drugs - Organ Transplantation" disease in my view under "Immunosuppressive Drugs - Organ Transplantation" and under "Organ Transplantation - Immunosuppressive Drugs".

When I have tried to just show the abbreviated_title field as well, I get the title and the abbreviated_title showing together when I need them to display as two separate nodes. Does anyone know how I could do that?

Here are pics: I need this node I need this node To show up hereTo show up here and hereand here when I click on one of the letterswhen I click on the appropriate letters in this search box. So it is the same node, but needs to show up two separate times in the view

6
  • Are you familiar with views? drupal.org/project/views
    – Screenack
    Aug 4 '15 at 2:17
  • Yes. Views aren't the problem. The problem is figuring out how to have the same node come up twice in the view(once as it's title and once as it's short title)
    – Eluchel
    Aug 4 '15 at 4:28
  • When you create your view, you'll want to choose a view format that allows you to specify "show fields," such as "unformatted list" then you may add as many fields from a node as you wish to appear simultaneously per view row.
    – Screenack
    Aug 4 '15 at 10:15
  • if I just add both fields to the view and display them they are displayed together, but I need to be able to display the two fields as if they were two different nodes.
    – Eluchel
    Aug 4 '15 at 20:33
  • Sometimes an image says more than words. Maybe that helps to understand what you're trying to achieve... What do you mean with "as if they were two different nodes"?
    – Volker
    Aug 4 '15 at 20:37
2

The only way to do this is going to be to create either a custom entity or at least a custom table with Views integration. Views is designed to list one record per entity (or "row" of data).

Custom code solution:

Create a table that actually has multiple rows per node. It would need at least 2 fields: nid and title, and you'd have to maintain the data in that table by implementing hook_node_update, hook_node_insert, and hook_node_delete. Then you'd need to expose that data to views either by defining a custom entity type based on that table, or by writing the Views integration directly.

Field Collection Approach:

You could store your titles on another entity in the system. A Field Collection is probably the best due to the fact that it won't clutter up any other parts of the interface.

The general idea would be to add a Field Collection field (field_alternate_titles?) to your disease entity. The Field Collection itself really only needs a single field (field_alternate_titles_title?) with cardinality equal to 1. You're not using Field Collection because of its ability to let you group fields together, but rather because it creates separate entities for each of your values that you can use Views to query against. You can set the cardinality for the Field Collection field (field_alternate_titles) to 2 or, if you want to allow an arbitrary number of alternate titles, unlimited.

You could stop now and keep things really simple. Your users would have to enter all of the titles that they'd possibly want for the disease in this field. This would require them to re-enter the node title as an additional entry in the Field Collection field, but it would save you from writing any code.

If you want a slicker user experience then you would probably want to do the following:

  1. Hide the Field Collection field on the disease node edit form by using the available widget type of "hidden" (rather than "embedded"). You don't want your users seeing or touching the field in the edit form as you'll be maintaining the values for them.
  2. Maintain the values in that field by implementing hook_node_presave and creating and relating your node to Field Collection entities according to the values entered for the node title and short title.

Whether you write the code to auto-populate the Field Collection values or not, you'll now need to build your view based on Field Collection as the base table rather than the Content (node) table. You can add a relationship to the host node entity so that you can grab whatever info you need from the node.

Search Index Approach:

Another possible approach would be to use something like Search API which maintains a separate index of your content which you can muck about with when content is sent to the index. So you could create separate index entries for every title you want to be able to search/list by. You may find it helpful to use this module as a starting point as it does denormalization, which is what you're after: https://www.drupal.org/project/search_api_grouping

10
  • I added some pictures. I hope they help clarify what I want to accomplish.
    – Eluchel
    Aug 6 '15 at 17:11
  • Yeah, I saw those. I can't think of a better way to achieve what you're trying to do aside from my answer here.
    – Aaron
    Aug 6 '15 at 17:37
  • Question, is hiding the Field Collection field and setting the #access property to FALSE the same as setting the widget to "Hidden" when making the Field Collection?
    – Eluchel
    Aug 6 '15 at 19:46
  • Oh, huh, I forgot about that option. Yeah, that should work fine.
    – Aaron
    Aug 6 '15 at 19:54
  • I have never programatically created content with a custom module before, do you know of any good articles that talk about how to do that? (How I can populate the field_alternative_titles field with two field_alternative_titles_title fields?)
    – Eluchel
    Aug 6 '15 at 20:28
0

Check out this Acquia tutorial out on creating a view.

1
  • Views aren't the problem. The problem is figuring out how to have the same node come up twice in the view(once as it's title and once as it's short title)
    – Eluchel
    Aug 4 '15 at 4:29
0

You could add another field title, then you would have one long title field and one short one, then in your View options , add the long and short field titles.

2
  • If you know php, you could shorten the title to what ever length you like, add it to your views template
    – Hectorplus
    Aug 4 '15 at 14:40
  • I need both fields to show up as if they were separate nodes, and when I just add them both they appear together. Do you know how to set it up so that they both appear in the list but display as if they were separate nodes?
    – Eluchel
    Aug 4 '15 at 20:31

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.