diff --git a/src/Model/BlogCategory.php b/src/Model/BlogCategory.php index 5d9bc509..502d87db 100644 --- a/src/Model/BlogCategory.php +++ b/src/Model/BlogCategory.php @@ -2,7 +2,10 @@ namespace SilverStripe\Blog\Model; +use SilverStripe\CMS\Model\SiteTree; +use SilverStripe\Forms\FormField; use SilverStripe\ORM\DataObject; +use SilverStripe\TagField\TagField; /** * A blog category for generalising blog posts. @@ -55,6 +58,44 @@ class BlogCategory extends DataObject implements CategorisationObject 'BlogPosts' => BlogPost::class, ]; + public function scaffoldFormFieldForHasMany( + string $relationName, + ?string $fieldTitle, + DataObject $ownerRecord, + bool &$includeInOwnTab + ): FormField { + $includeInOwnTab = false; + return $this->scaffoldFormFieldForManyRelation($relationName, $fieldTitle, $ownerRecord); + } + + public function scaffoldFormFieldForManyMany( + string $relationName, + ?string $fieldTitle, + DataObject $ownerRecord, + bool &$includeInOwnTab + ): FormField { + $includeInOwnTab = false; + return $this->scaffoldFormFieldForManyRelation($relationName, $fieldTitle, $ownerRecord); + } + + private function scaffoldFormFieldForManyRelation( + string $relationName, + ?string $fieldTitle, + DataObject $ownerRecord + ): FormField { + $parent = ($ownerRecord instanceof SiteTree) ? $ownerRecord->Parent() : null; + $field = TagField::create( + $relationName, + _t($ownerRecord->ClassName . '.' . $relationName, $fieldTitle), + ($parent instanceof Blog) ? $parent->Categories() : static::get(), + $ownerRecord->$relationName() + )->setShouldLazyLoad(true); + if ($ownerRecord instanceof BlogPost) { + $field->setCanCreate($ownerRecord->canCreateCategories()); + } + return $field; + } + /** * {@inheritdoc} */ diff --git a/src/Model/BlogTag.php b/src/Model/BlogTag.php index a7223a22..b058335c 100644 --- a/src/Model/BlogTag.php +++ b/src/Model/BlogTag.php @@ -2,7 +2,10 @@ namespace SilverStripe\Blog\Model; +use SilverStripe\CMS\Model\SiteTree; +use SilverStripe\Forms\FormField; use SilverStripe\ORM\DataObject; +use SilverStripe\TagField\TagField; /** * A blog tag for keyword descriptions of a blog post. @@ -55,6 +58,44 @@ class BlogTag extends DataObject implements CategorisationObject 'BlogPosts' => BlogPost::class ]; + public function scaffoldFormFieldForHasMany( + string $relationName, + ?string $fieldTitle, + DataObject $ownerRecord, + bool &$includeInOwnTab + ): FormField { + $includeInOwnTab = false; + return $this->scaffoldFormFieldForManyRelation($relationName, $fieldTitle, $ownerRecord); + } + + public function scaffoldFormFieldForManyMany( + string $relationName, + ?string $fieldTitle, + DataObject $ownerRecord, + bool &$includeInOwnTab + ): FormField { + $includeInOwnTab = false; + return $this->scaffoldFormFieldForManyRelation($relationName, $fieldTitle, $ownerRecord); + } + + private function scaffoldFormFieldForManyRelation( + string $relationName, + ?string $fieldTitle, + DataObject $ownerRecord + ): FormField { + $parent = ($ownerRecord instanceof SiteTree) ? $ownerRecord->Parent() : null; + $field = TagField::create( + $relationName, + _t($ownerRecord->ClassName . '.' . $relationName, $fieldTitle), + ($parent instanceof Blog) ? $parent->Tags() : static::get(), + $ownerRecord->$relationName() + )->setShouldLazyLoad(true); + if ($ownerRecord instanceof BlogPost) { + $field->setCanCreate($ownerRecord->canCreateTags()); + } + return $field; + } + /** * {@inheritdoc} */