1

Is it possible with the modifyEntrySources hook to query a field value against another fields value? Example:

Two fields are present on the entry section this hook targets

  • fieldParentId
  • fieldId

I want to be able to query in the entry source definition if the fieldParentId is the same as the fieldId to return only those records in the entry list within Craft that match this. I'm not sure this is possible, or if there is a special syntax required within the criteria array to do it?

3
  • It is possible to do this with Craft 3 since you work with queries directly, you'll need an ugly work around with Craft 2 because you can't create such a condition via array by default, so you'll need to overwrite and extend more things Aug 30, 2018 at 13:47
  • I'm on Craft 2 for now. I ended up having to perform a ElementCriterialModel query and return all entries in the section then in a foreach do a comparison with the two fields and return the ids that all matched to use in the entry source criteria. Not ideal but does the job. Aug 30, 2018 at 18:04
  • @JamesWhite Could you add the solution you ended up on as an official answer? Might end up helping someone in the future.
    – Brad Bell
    Aug 31, 2018 at 20:31

1 Answer 1

1

A workaround solution was to essentially do an criteria on the section, return all the records, use a conditional to find out which records have the same "parent ID" and ID", stores those in an array and then use the id criteria for the entry source.

Something like:

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'theSection';
$criteria->limit = null;
$articles = $criteria->find();

// For storing matching entries on the conditional below
$ids = [];

foreach ($entries as $entry)
{
     if($entry->fieldParentId == $entry->fieldId)
     {
         array_push($ids, $entry->id);
     }
}

This will then provide a list of ids that match the required criteria, which can then be used for a specific entry source.

This has performance issues though, because you have to get the entire list of entries first each time.

2
  • So why don't you query for entries with the same columns from the beginning and return only the ids? You can transform the criteria model into a query and include normal where conditions. Otherwise you might run into performance issues sooner or later Sep 2, 2018 at 8:56
  • @RobinSchambach I'd agree that's a better way. We ended up changing the structure of the records, so we didn't have to do this in the end, but a query would be more performance friendly. Sep 12, 2018 at 7:41

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.