mirror of
https://github.com/silverstripe/silverstripe-blog
synced 2024-10-22 11:05:58 +02:00
ENH Auto-scaffold blog categories and tags formfields (#769)
This commit is contained in:
parent
cee3b0066d
commit
c736dced66
@ -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}
|
||||
*/
|
||||
|
@ -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}
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user