Skip to content

Commit 6a17fc4

Browse files
authored
Fix null pointer exception in ElementBlogPosts Categories() call (#49)
* Fix null pointer exception in ElementBlogPosts Add null check for blog object before calling Categories() method. This prevents "Call to a member function Categories() on null" error when Blog::get()->byID($val) returns null for non-existent blog IDs. Fixes issue where attempting to edit an ElementBlogPosts element with an invalid or deleted blog ID would cause a fatal error. * Add explicit return statement for null blog case Address Copilot review feedback by adding explicit return [] when blog is null for better code clarity and consistency. This makes the control flow more explicit and easier to understand.
1 parent f4a98d1 commit 6a17fc4

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/Elements/ElementBlogPosts.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ public function getCMSFields()
7676

7777
$dataSource = function ($val) {
7878
if ($val) {
79-
return Blog::get()->byID($val)->Categories()->map('ID', 'Title');
79+
$blog = Blog::get()->byID($val);
80+
if ($blog) {
81+
return $blog->Categories()->map('ID', 'Title');
82+
}
83+
return [];
8084
}
8185
return [];
8286
};

0 commit comments

Comments
 (0)