How to create a taxonomy link block in Drupal 8

crossroad

Drupal's view module allows extracting data right from database and transforming it to present it to user, it allows us to make very nice things just from admin ui but also has a lot of power when it comes to programming.

In this particular case, our page starts growing and article number is increasing but there is no new functionality, so we are going to create a block that will allow us to navigate through different taxonomy terms. Let's start like if there was no content and no terms, so we are going to create a new content type, a new taxonomy term and we are going to assign this term to our new content. Then we are going to create a summary block with all taxonomy terms and how many nodes have this taxonomy term assigned. Let's start step by step:

Adding a new taxonomy term:

In order to add a new taxonomy term go should go to admin > structure > taxonomy (admin/structure/taxonomy) and we click on add vocabulary:

agregar elemento de taxonomía

We fill fields with the values we feel better, since it's an example (our actual block was done by the time this article was written) we will tell simple XML Sitemap to avoid indexing this vocabulary to avoid 404 errors when we delete all items from example:

Nuevo termino de vocabulario

Adding a new content type:

After creating vocabulary, we can create a new content type, as it's only an example we will not add publish, menu and index options but they are enabled on real content, you will usually want them activated by default:

Añadir nuevo tipo de contenido al sitio

This will create a basic content with a single field, it's interesting noting Drupal - opposite to other CMS - will give us a full flexibility on defining how we want to create content, we can add as much fields as we want and we can create new field types adding modules.

By now, we will leave a basic content and we will add a new field, a taxonomy term reference:

Desplegable con los tipos de campo

We add the content type, set a new name and save it, after saving the new field we can configure term with description and options, we will make it mandatory so our block will index all content types, we can choose default reference or use a view to create our own defined references.

We choos the vocabulary elements we created begore and it's all set:

opciones de configuración del campo

Now we can create content that will refer to taxonomy terms or just create terms for the example, the final result is on this article sidebar.

To create a new view we go to Admin > Structure > Views (atajo admin/structure/views)  and add a new view, we fill the details for the new view selecting the terms we want to use for our view:

Pagina de creación de nueva vista

It will allow us to create a new page and a new block, once it's seleted all creation options will be shown, in this case i'd like to create a view for the page and a view for the block, this way we have more flexibility changing both and we don't risk changing the wrong view because we forgt to select "only this view", but in tbhis example we will be creating view and block on same view:

Crear nueva página en la vista

 

Crear nuevo bloque en una vista

Once view is created, on the editing UI we can see a preview on how it will be shown, if we choose block we will see a term list but we won't see a counter, to make it show we need to go to "Advanced" next to block options and a new set of options will be shown, amongst them we have "relationships", we can the "Add" a new relationship selecting "content with term", this way we make a relationship between contents and the terms they appear in,  if we choose "make relationship mandatory" only terms with content will be shown so in this case we don't select it so terms without content are also shown.

Once it's done we activate also aggregation in advanced section:

Opciones avanzadas de la vista

After saving we can add into sindication options field Content:UUID, in the aggregation options we select Count Distinct and in the field we select enitity ID so we can count the number of distinct contents for each term:

Opciones de agregación de la vista

Only presentation is left, to make it nice we should theme the block but right now we will just make the content inline and the number between brackets.

We can just change it on format part of the view, choosing options in  Fields and marking both fields as inline fields. After that they will be shown one after another. We only need to edit field "(nodo) COUNT(DISTINCT Content: UUID)" and "Rewrite field" rewriting output with "({{ uuid }})

With this we have our block created, ww just need to play with the paging and view options to make the block suit our needs. We will come back to this in order to make a page to list all terms for the "more..." link.

 

Tags