Closed johnj24 closed 6 months ago
If I had to guess, probably entry
is null
here at some point, for some reason.
The code snippet I shared above lives on an entry page, so I would be surprised if it were null.
Doesn't the following suggest nothing is null?
3. in /vendor/twig/twig/src/Extension/CoreExtension.php at line 1607– nystudio107\similar\variables\SimilarVariable::find(['element' => craft\elements\Entry, 'context' => ['49913', '22', '207', '75028'], 'criteria' => craft\elements\db\EntryQuery])
mmmm yes it does... here's where it is going wrong (somehow):
// Get an ElementQuery for this Element
$elementClass = is_object($element) ? $element::class : $element;
/** @var EntryQuery $query */
$query = $this->getElementQuery($elementClass, $criteria);
// Stash any orderBy directives from the $query for our anonymous function
$this->preOrder = $query->orderBy;
Somehow $query->orderBy
is coming back as null
Here's what getElementQuery()
does:
protected function getElementQuery(string|ElementInterface $elementType, array $criteria): ElementQueryInterface
{
/** @var string|ElementInterface $elementType */
$query = $elementType::find();
Craft::configure($query, $criteria);
return $query;
}
Try changing this:
// Stash any orderBy directives from the $query for our anonymous function
$this->preOrder = $query->orderBy;
to this:
// Stash any orderBy directives from the $query for our anonymous function
$this->preOrder = $query->orderBy ?? [];
...and see if that doesn't solve the issue for you?
That solved it. Thanks, Andrew.
Addressed in the above commits
Question
At some point in the last few weeks, the code below stopped working and started to show this error:
Cannot assign null to property nystudio107\similar\services\Similar::$preOrder of type array|string
None of the variables set up below return null that I can tell. I recently started 'entrifying' category groups on this site but since I'm not asking for category ids here, I don't think it's an issue -- just adding for context. I haven't 'entryfied' tags yet.
Stack Trace
Additional context
Craft CMS: 4.4.9 Similar: 4.0.0