Score:2

How can I sort results by contextual filter value?

do flag

I have a set of pages that use path information to - amongst other things - create view of relevent news articles.

eg:

mysite.com/city-service
mysite.com/country-service
etc

I have an existing view that uses a contextual filter to extract the relevant portion of the URL and then look up relevant news articles that are tagged with an organisation tag that maps to each service.

This works great, but I have been asked to change the view to the following behavior:

  1. Show ALL recent news articles
  2. Sort the news articles so the relevant (and recent) organisation tagged news is at the top

I am not allowed to radically redesign the page or workflow for staff - eg manually tag all news articles - so the only place I can get the taxonomy term name for the sort is the url.

How can I sort view results based on a taxonomy term name that takes a parameter from the page url?

leymannx avatar
ne flag
With a little bit of redesign though (e.g. a second pager) it could be as easy as adding another block or views attachment in the bottom of the first view. Nice question!! Looking forward to nice answers.
leymannx avatar
ne flag
Maybe check out the comment under this answer: https://drupal.stackexchange.com/a/212340/15055.
Score:4
gb flag

To accomplish this, you need to alter the views query with hook_views_query_alter in your custom module. Read out the requested URL and get the related taxonomy term to it. Then you can adjust the query according to your needs. A similar answer can be found here: How to sort on different columns in the same view?

Here is an example:

use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;

function MYMODULE_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  // Determine view by id and current display.
  if ($view->id() == 'content_recent' && $view->current_display == 'default') {
    // Get the requested path.
    $request = \Drupal::request();
    $path = $request->getPathInfo();

    // Use some logic to get the term for the current path.
    // @see https://drupal.stackexchange.com/a/266084/15055
    $tid = FALSE;
    if ($path == 'example') {
      $tid = 9;
    }

    // If the URL belongs to a term, we change the order.
    if ($tid) {
      // You may need to add a relationship to the taxonomy terms,
      // if your view is not already doing this or add it in the Views UI.

      $sql = "CASE
        WHEN taxonomy_term_field_data_node_field_data.tid = $tid THEN 0
        ELSE 1
      END";
      // Add the sql formula for sorting.
      $query->addField(NULL, $sql, 'related_taxonomy_term');
      // We first sort by the related term and than by the views settings,
      // which should for example be the changed date descending.
      $query->orderby = array_merge([[
        'field' => 'related_taxonomy_term',
        'direction' => 'ASC',
      ]], $query->orderby);
    }
  }
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.