ENH Auto-scaffold blog categories and tags formfields (#769)

This commit is contained in:
Guy Sartorelli 2024-06-27 10:44:38 +12:00 committed by GitHub
parent cee3b0066d
commit c736dced66
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 82 additions and 0 deletions

View File

@ -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}
*/

View File

@ -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}
*/