Indexing flags in Drupal 8 with Flag Search API module
Using flags on large datasets allows for user customisation. A user can flag nodes, comments, users, and any other type of entity. Some possibilities include bookmarking, flagging friends, flagging important, visited/seen or offensive content.
In this blog post I'll show you how you can index flags using Search API and how you can use them in indexed view. This is possible with the Flag Search API module which I created last week for our project (sponsored by Websolutions Agency). Flag Search API module has dependencies to Flag and Search API module.
Now let's see how this works. For testing we'll use the following modules:
- Search API,
- Search API Solr Search (since we'll index this on Apache Solr),
- Flag Search API,
- and I'll also use Devel module for generating content.
Our use case here will be to display all Articles that are bookmarked by a logged in user.
First we need to download all modules on our Drupal installation and set them up. We can install them with composer (preferable).
composer require drupal/<module name>
Alternatively, download them with drush or download them manually.
After installation with composer we'll have to install (enable) them using drush:
drush en <modulename>
Or manually go to Admin / Modules link in your Drupal installation.
After modules are enabled let's create a new flag called Bookmark, so we need to go to Administration / Structure / Flags and click Add flag.
Selecting Flag Type Content and clicking continue Link where we'll set up some basic things about our flag. Label will be "Bookmark", Scope is Personal and we'll not set up Flag access for the purpose of this blog. Flag link text will be “Bookmark this item” and Unflag link text will be “Unbookmark this item”.
Let's set the flag to Display link as field and Link type will be AJAX link.
Now let's save this flag and move on. For my testing environment I'll need to have some content created so I'll use Devel generate and generate about 50 article nodes for this.
Now that we've created some content I can go to any page and bookmark it. I've placed bookmark link just below the title for testing purposes.
After bookmarking content we’ll have to add it to create index and add it to the index so that we can create a view for bookmarked articles page. For index I'll use my existing Apache Solr server. You can add connection to your server on Administration / Configuration / Search and metadata / Search API from backend. After that I’ll add index called "Content Articles" and in Bundles select Article to be indexed.
Now we can see our newly created index and we can set some fields on it.
Let's check out fields section.
Right now there are no fields because we didn't select any to be indexed. Let's add a field and our flag. I'll click on Add fields and under General select Rendered HTML output to our fields.
After saving we now have one field for index.
Notice there still isn't any flags here so let's save the changes and move to Processors tab to see what we have there.
As you can see here there is a Flag indexing processor. Once we enable it we'll see that it moves to preprocess index section and now we have options to enable which flags we want to index.
Here we'll check our created Bookmark flag and click on Save. Now you can see that there are two fields for index. One is Rendered HTML output and the other one is tflag.
Let's go to View tab and index this manually by clicking Index now.
Now when all is indexed we can make a view that will show only bookmarked articles. Let's create a view called "Bookmarked Articles" with a page called the same like shown below.
Next let's add a contextual filter. As you can see here, there is an option in the contextual filter to filter by Bookmark (which is our Bookmark flag).
Select Bookmark and click on Add and configure contextual filters.
Now there is an option to Provide default value for bookmark and this is actually User ID from logged in user. Since user flags are flagged by user we only need to select logged in user as a default value and we’ll get all content bookmarked by the logged in user.
And here we go. Since I am using admin user role and admin is a logged in user, it is showing only articles flagged (bookmarked) by admin.
This was an example of how you can create flags, index them through Search API into Apache Solr using Flag Search API module and create custom pages based on your flagged content.