Using the Flippy module with taxonomies.

Recently, I've been working on a project at Ymbra, where we needed a pager between nodes that allows us to have on each page a navigation with the “Previous” and “Next” links. We chose the Flippy module that adds this functionality easily with a minimum of configuration.

Our project needed to filter nodes by taxonomy, but Flippy was only prepared to filter by content type. So we needed an additional filter to the one provided by default.

So we contributed this improvement to the module, for those who need to implement the same functionality. In fact, there is an issue in the module page asking for that.

Basically, we’ve added two new fields in the Flippy configuration for each type of content: a “checkbox” to indicate the filter by taxonomy, and a “select”, dependent on the previous one, to choose the taxonomy by which we filter the contents.

To achieve this, two files of the module have been modified: flippy.module and FlippyPager.php. In flippy.module the two necessary fields were added, taking into account that when creating the list of taxonomies for the select options, we only have to include those that are in the content type:

 
// List taxonomies used in the content type.

$taxonomy_options = [];

$content_type_fields = \Drupal::service('entity_field.manager')

->getFieldDefinitions('node', $form['type']['#default_value']);

foreach ($content_type_fields as $taxo_field) {

  if (get_class($taxo_field) == 'Drupal\field\Entity\FieldConfig') {

    // It is an additional field.

    $itemDefinition = $taxo_field->getFieldStorageDefinition();

    $schema_info = $itemDefinition->getSchema();

    if (isset($schema_info['columns']['target_id'])) {

      $entity = $itemDefinition->getPropertyDefinitions()['entity'];

      $constrains = $entity->getConstraints();

      if ($constrains['EntityType'] == 'taxonomy_term') {

        $taxonomy_options[$taxo_field->getName()] = $taxo_field->getLabel();

      }

    }

  }

}

In addition, in FlippyPager.php the query that performs the content search was modified to build the pagination, introducing the new condition:

// Filter by taxonomy select in config.

$taxonomy_filter = $this->flippySettings

->get('flippy_taxonomy_' . $node->getType());

if ($taxonomy_filter) {

  $taxonomy = $this->flippySettings

    ->get('flippy_taxonomy_list_' . $node->getType());

  // Check if node has a value in the taxonomy.

  if (isset($node->get($taxonomy)->target_id)) {

    $taxonomy_id = $node->get($taxonomy)->target_id;

    $query->condition($taxonomy, $taxonomy_id);

 }

}

To avoid problems in a scenario where a node does not have a value assigned to the taxonomy whereby we filter, it is convenient to check it before adding the condition.

With this small improvement, we can now make the Flippy module filter the contents by taxonomy.

The patch can be downloaded here: https://www.drupal.org/project/flippy/issues/1497070#comment-12945412

We hope you find this contribution useful!