Indexing flags in Drupal 8

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:

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.

Indexing flags fig. 1

Indexing flags fig. 2

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.

Indexing flags fig. 3

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

Indexing flags fig. 4

Let's set the flag to Display link as field and Link type will be AJAX link.

Indexing flags fig. 5

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.

Indexing flags fig. 6

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.

Indexing flags fig. 7

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.

Indexing flags fig. 8

Now we can see our newly created index and we can set some fields on it.

Indexing flags fig. 9

Let's check out fields section.

Indexing flags fig. 10

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.

Indexing flags fig. 11

After saving we now have one field for index.

Indexing flags fig. 12

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.

Indexing flags fig. 13

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.

Indexing flags fig. 14

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.

Indexing flags fig. 15

Let's go to View tab and index this manually by clicking Index now.

Indexing flags fig. 16

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.

Indexing flags fig. 17

Indexing flags fig. 18

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

Indexing flags fig. 19

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.

Indexing flags fig. 20

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.

Indexing flags fig. 21

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.

Share this post