1

I have installed the Calendar plugin and I'd like to search across event entries from this plugin and from channel/section entries across the base Craft 3 site.

However, I am getting the usual Limit-Array error when trying to paginate the results.

Is there a method for paginating multiple search results in Craft 3? I haven't come across a way to do this in any current questions or Craft 3 Docs.

2

I doubt this is possible in Twig by default because you have two different Element types, thus you'll need to different instances of the ElementQuery to populate all models correctly. I think you'll need to create your own pagination. That being said Element::find()->all() will throw an error

Cannot instantiate abstract class craft\base\Element

Thus you'll have to Query for your elements in your own

// get all possible element ids
$sectionId = X;
$query = (new Query())
    ->select('elements.id')
    ->from('{{%elements}}')
    ->leftJoin('{{%elements_sites}}', 'elements_sites.elementId = elements.id')
    ->leftJoin('{{%entries}}', 'entries.id = elements.id')
    ->where(new OrCondition([
        ['=', 'entries.sectionId', $sectionId], <-- your section Id
        ['is', 'entries.sectionId', null],
    ]))
    ->andWhere(new OrCondition([
        ['=', 'elements.type', \craft\elements\Entry::class],
        ['=', 'elements.type', YourCalenderClassComesHere],
    ]))
    ->andWhere(['elements_sites.siteId' => 1])
    ->limit(10)
    ->offset(0)
    ->column();

// populate entry by Id
foreach ($query as $elementId){
    $element = Craft::$app->getElements()->getElementById($elementId);
}
| improve this answer | |
  • Wow, thanks Robin. What a complete answer. I've yet to test myself with Craft Plugin work so I'll give writing one a go with this! Assuming you'd recommend this to be implemented in plugin form? – Jay May 27 '18 at 19:23
  • You need to implement it via plugin or module, these are the only ways to access custom php code. There are a few other checks you might want to include. The easiest is to run Entry::find()->getRawSql() to see all conditions Craft includes by default – Robin Schambach May 27 '18 at 19:24
  • This is great. Thanks for your help. I'll be sure to try and write my first plugin with this then! Cheers again. 👍🏼 – Jay May 27 '18 at 19:35

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.