How to create automatically a view of all nodes sharing the same field value? 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
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=&amp;status=13&amp;priorities=All&amp;categories=All&amp;version=All&amp;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. 



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.



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.



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. 



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.)



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.



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=&amp;other_field=&amp;another_field=&amp;field_tags=[TOKEN_VALUE]

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