5

I have an article content type linked to an event content type via entity reference.

In the Articles view, I would like to offer a filter allowing to filter articles by linked event title.

With Drupal 7, we used to manage it with better_exposed_filters module but I can't find a way to do it in Drupal 8.

5

Here is the my solution inspired from:

Thanks to Danny Sipos

I already had a dedicated custom module for this project where I added the following:

mymodule.views.inc

<?php
/**
 * Implements hook_views_data_alter().
 */
function mymodule_views_data_alter(array &$data)
{
    $data['node_field_data']['related_content_titles'] = array(
        'title' => t('Related content titles'),
        'filter' => array(
            'title' => t('Related content titles'),
            'help' => t('Specify a list of titles an content node can have.'),
            'field' => 'nid',
            'id' => 'mymodule_related_content_titles'
        ),
    );
}

src/Plugin/views/filter/RelatedContentTitles.php

use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\filter\ManyToOne;
use Drupal\views\ViewExecutable;

/**
 * Filters by given list of event title options.
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("mymodule_related_content_titles")
 */
class RelatedContentTitles extends ManyToOne
{
    /**
     * {@inheritdoc}
     */
    public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL)
    {
        parent::init($view, $display, $options);
        $this->valueTitle = t('Allowed event titles');
        $this->definition['options callback'] = array($this, 'generateOptions');
    }

    /**
     * Helper function that generates the options.
     * @return array
     */
    public function generateOptions()
    {
        $storage = \Drupal::entityManager()->getStorage('node');

        $relatedContentQuery = \Drupal::entityQuery('node')
            ->condition('type', array('event', 'story'), 'IN')
            ->condition('status', 1);

        $relatedContentIds = $relatedContentQuery->execute();

        $res = array();
        foreach ($relatedContentIds as $contentId) {
            $res[$contentId] = $storage->load($contentId)->getTitle();
        }

        return $res;
    }
}

When ceating the view listing my articles where I want to expose the related event filter I had to :

  • add a relationship field_article_associated_event: Content
  • add a filter criteria of my type Related content titles, select the perviously cerated relationship for the field Relationshi, check Expose this filter to visitors, to allow them to change it
0

You can use the Entity Reference Exposed Filters module.

A module for those who need their Entity Reference Exposed Filters to have titles in Select Lists.

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.