0

Working with Drupal 7.54 and Views 3.15

For each content type field, I need to display a link (after the value) which returns a view of all nodes sharing the same field value.

I manage to get my link (field - rewrite results and add a link with tokens) but I need to create a view (with specific contextual filter related to the field) for each field.

Is there a way to build this so I do not need to create x views for x fields? Any token for the field? Any PHP contextual filter using field_name and field_value?

Thanks in advance.

Example :

Node 1

  • Field A : value x (link)

  • Field B : value y (link)

etc.

The link returns a view :

All nodes with value x for Field A

  • Node 2

  • Node 7

  • Node 15

...

Related questions :

Create a view of all nodes that have the same value for a specified field as the current node

  • 1
    Hi Toki. Could you use taxonomy for your fields? The links you are describing are default behavior for taxonomy terms in Drupal. – Kristin Mar 14 '17 at 16:19
  • Thank you Kristin for your answer. Unfortunately, I will not be able to use drupal taxonomy for each field. Some of them will be integer or boolean, etc. And I will end with a large number of vocabularies. But you are absolutely right, using taxonomy terms would work. My only concern is the dynamic contextual filter I would need if I do not want to build 50 views for 50 fields? I hope I am clear enough here. – Toki Mar 14 '17 at 17:02
  • Hi Toki, yes, I see why taxonomy terms wouldn't work for you based on the types of fields you will be using. I would take a look at using the Search API with views, I've added it as an answer below. – Kristin Mar 14 '17 at 17:44
  • 1
    Do note that taxonomies are indexed by id, which are often faster than strings in SQL. Having to scan through a lot of strings might impact the performance in the long run. – Neograph734 Mar 14 '17 at 17:53
1

Suggested method

I suggest looking into using the Search API (with Views) for this purpose.

From the Search API module page:

This module integrates the Search API with the Views module, allowing searches on any index to be created and viewed via Views. All of an entity's properties, as well as those of related entities (e.g. a node's author's name), are available as fields, filters and arguments for all indexed fields are available and sorts (as well as click sorts) can be created on any indexed single-valued field. Also some additional features, like linking the results to the entity, are available.

Drupal.org uses this method to create Issue pages with links like: https://www.drupal.org/project/issues/search_api?text=&status=13&priorities=All&categories=All&version=All&component=All. You should be able to craft your links to the view integrated with the Search API using tokens.

Detailed Instructions

I was able to set this up in my testing with the following steps, which are detailed below.

  • Install the Search API and related modules
  • Set up a search server
  • Set up a search index
  • Set up a view of the search index
  • Create the links to the view with tokens

Install the Search API and related modules

Install and enable the Search API module. Also enable the Search views extension module that comes with the Search API.

Set up a search server

For my testing, I used the Search API Database Search module. This is a separate module in Drupal 7 but it is in the main Search API project for Drupal 8. From the module page:

This module provides a backend for the Search API that uses a normal database to index data. It is therefore a cheap and simple alternative to backends like Solr, but can also be a great option for larger sites if you know what you're doing.

This search server set up may work for you, or you may need to configure a different service class for your search server. I think specific instructions on other options like Solr are out of the scope of this answer, but I recommend taking a look at the Service classes and features documentation page which lists various options and their features.

To use the Search API Database Search, install and enable the module. Then visit Configuration > Search and Metadata > Search API (admin/config/search/search_api) and click +Add server. Name your server, leave the Enabled checkbox checked, and choose the Database service as the Service class. Click Create server.

Add a server

Set up an index

Visit Configuration > Search and Metadata > Search API (admin/config/search/search_api) and click +Add index. Name your index, choose the Item type, and restrict by Bundle (content type) if desired. I restricted to Nodes of type Article in my testing. You can leave the Bundles checkboxes blank if you want to include all content types. Choose the server you set up in the previous step as the Server. Click Create index.

Create an index

Now, you will select the fields you want to add to the index. In my testing I added an Integer field to the Article content type, so I added both the integer field and the tags field to the index. Check the fields you want to add and click Save changes.

Add fields to the index

The next part of the configuration is the Filters settings. You can choose to add "Alterations" or "Processors" to the data in the index. Your choices here will depend on what kind of data you're indexing, each option has a description that will help you understand what it does. In my testing I chose to Exclude unpublished nodes under Alterations and I chose Ignore case under Processors. Click Save configuration.

At this point, you should click the View tab to see the Index status page and add items to your index. The Index status page is located at Configuration > Search and metadata > Search API > Edit (Your Index) (admin/config/search/search_api/index/INDEX_MACHINE_NAME). For testing purposes I suggest indexing in a small batch at first and clicking Index now.

Index items button

Set up a view of the search index

Now, visit Structure > Views (admin/structure/views) and click Add a new view. Name your view and in the Show dropdown field choose Your Index. Click continue and edit.

In the view under Filter Criteria, add a filter for your each of your indexed fields (I know that will be a lot of fields in your specific case). I added one for the Integer field and one for the Tags taxonomy field. Make sure you click the "Expose this filter to visitors" checkbox for each filter. (You can also look into the Facet API for the facets option, however I think you would end up with a block for every field you wanted to put a facet on as opposed to one disabled block with a ton of filters you won't use. I think the Facet API method would also be worse for performance but I may be incorrect.)

Example adding the Integer field filter

Now you can visit the view page and adjust the exposed filters to get the URL structures you need to use with your tokens. Or, the pattern is: https://example.com/[VIEW_NAME]?[FIELD_MACHINE_NAME]=[FIELD_VALUE]

Change the Exposed form to a block

For your use case, it sounds like you just want the results from your links and not really a search page with filters or facets. So, once you've got your tokens and URLs working, in your view settings expand the Advanced section and change the Exposed form in block to Yes. This will put all the filters you've added to a block that must be manually enabled. The filtering in the URL will still work, users just won't see the exposed form with all the possible filters.

Exposed form to a block setting

Set up your fields to be links with tokens in your content type

It sounds like you've already got the tokens in links part working on your nodes or your view of nodes. In my testing, I found you can exclude all link arguments except the one you're filtering with, and the view will display everything that matches that field regardless of the other filters.

So, instead of having to make a link this: https://example.com/test-view?field_integer_field=&other_field=&another_field=&field_tags=[TOKEN_VALUE]

Your link can just include the one relevant filter: https://example.com/test-view?field_tags=[FIELD_VALUE_TOKEN]

  • It looks quite promising, thanks. I will try this and get back to you. – Toki Mar 14 '17 at 17:53
  • OK, I am a little but confused now since this is my first time with Search API and search server creation, etc. Now, I know what to do tomorrow! Read and learn (hopefully). I will let you know if I make any progress. Thanks again. – Toki Mar 14 '17 at 18:07
  • I look forward to hearing how it works out for you! – Kristin Mar 14 '17 at 18:09
  • @Toki - I was able to test this out more thoroughly and add some detailed instructions to my answer. I hope this helps you in your research. Please let me know how it goes. – Kristin Mar 15 '17 at 4:28
  • Wow, thank you so much for this detailed answer. I followed each step and I am really close to a solution, I just need some adjustments now for specific fields (Entity reference, aggregations, etc.) but globally your solution works perfectly. And thank you also for the tip on Search API Database Search, a really useful module to avoid server configuration, outside Drupal. Many thanks again, I wish all answers would be like yours!! – Toki Mar 15 '17 at 9:37

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.