Problem/Motivation

This query doesn't work:

// Gimme all top-level terms from 'topic' vocab.
$tids = \Drupal::entityTypeManager::getStorage('taxonomy_term')
  ->getQuery()
  ->accessCheck(FALSE)
  ->condition('vid', 'topic')
  ->condition('parent', '0')
  ->execute();

Especially after #3332666: Latest core taxonomy changes are breaking rdf_taxonomy, fixing this became trickier as we define a top-level term in two ways:

  • When the term's vocabulary doesn't provide a parent mapping -> it's a top-level term
  • When the term has multiple parent triples (aka multiple parents) and one of them is the vocab RDF type' -> it's (also) a top-level term

Steps to reproduce

Write a kernel regression test

Proposed resolution

Some hints:

  • Implement hook_query_sparql_reference_alter() and check for a 'parent' condition (=, !=, <>, IN, NOT IN) with '0' value.
  • If found, move the condition in an OR conjunction and add also a 'parent (NOT) EXISTS' condition under OR.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

claudiu.cristea created an issue. See original summary.

claudiu.cristea’s picture

Title: Top-level parent entity query » Top-level taxonomy term entity query
dimilias’s picture

Another approach to this would be to alter the queries for the RDF terms to include the vocabulary ID as a root term.
This is because the concepts have a concept scheme they are related to and usually this gets the part of the URI that is common to all.
For example, a vocabulary (concept scheme) with URI http://example.com/myVoc# would have terms like http://example.com/myVoc#term1 etc. Though even if the above rule is not written in stone, still, the concept scheme is the correct way to structure a vocabulary.

That way we could avoid trying to build OR conditions and replace the '0' with the concept scheme.
For the record, I am not claiming that this is necessarily an easier task, though I assume it is.

Edit: An idea would be to simply not add the condition if the requested field is not mapped. If it is very hard to not add it generically, we can alter the query and remove the condition if it is there.

claudiu.cristea’s picture

Issue summary: View changes
claudiu.cristea’s picture

What if the vocab condition is missing? Let's throw an exception that this is a limitation when querying for parent '0'