Skip to content

Commit

Permalink
ENH Auto-scaffold blog categories and tags formfields (#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Jun 26, 2024
1 parent cee3b00 commit c736dce
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Model/BlogCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}
*/
Expand Down
41 changes: 41 additions & 0 deletions src/Model/BlogTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}
*/
Expand Down

0 comments on commit c736dce

Please sign in to comment.