Skip to content

Commit

Permalink
BUGFIX Call to a member function Categories() on null
Browse files Browse the repository at this point in the history
Resolves an issue where no blog selected causes a chained `Categories()` relation call to error when attempting to edit the block later in the cms.
  • Loading branch information
muskie9 committed Aug 26, 2022
1 parent c6f4adf commit c1ad497
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/Elements/ElementBlogPosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,25 @@ class ElementBlogPosts extends BaseElement
/**
* @var array
*/
private static $db = array(
private static $db = [
'Limit' => 'Int',
'Content' => 'HTMLText',
);
];

/**
* @var array
*/
private static $has_one = array(
private static $has_one = [
'Blog' => Blog::class,
'Category' => BlogCategory::class,
);
];

/**
* @var array
*/
private static $defaults = array(
private static $defaults = [
'Limit' => 3,
);
];

/**
* @return FieldList
Expand All @@ -85,7 +85,10 @@ public function getCMSFields()

$dataSource = function ($val) {
if ($val) {
return Blog::get()->byID($val)->Categories()->map('ID', 'Title');
if ($blog = Blog::get()->byID($val)) {
return $blog->Categories()->map('ID', 'Title');
}

}
return [];
};
Expand Down Expand Up @@ -137,7 +140,7 @@ public function getSummary()
$label = _t(
BlogPost::class . '.PLURALS',
'A Blog Post|{count} Blog Posts',
[ 'count' => $count ]
['count' => $count]
);
return DBField::create_field('HTMLText', $label)->Summary(20);
}
Expand Down

0 comments on commit c1ad497

Please sign in to comment.