ENH Auto-scaffold Member and Group with appropriate form fields (#11285)

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

View File

@ -8,6 +8,7 @@ use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
@ -27,6 +28,8 @@ use SilverStripe\Forms\Tab;
use SilverStripe\Forms\TabSet;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\Forms\TreeMultiselectField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataQuery;
@ -297,6 +300,35 @@ class Group extends DataObject
return $labels;
}
public function scaffoldFormFieldForHasOne(
string $fieldName,
?string $fieldTitle,
string $relationName,
DataObject $ownerRecord
): FormField {
return TreeDropdownField::create($fieldName, $fieldTitle, static::class);
}
public function scaffoldFormFieldForHasMany(
string $relationName,
?string $fieldTitle,
DataObject $ownerRecord,
bool &$includeInOwnTab
): FormField {
$includeInOwnTab = false;
return TreeMultiselectField::create($relationName, $fieldTitle, static::class);
}
public function scaffoldFormFieldForManyMany(
string $relationName,
?string $fieldTitle,
DataObject $ownerRecord,
bool &$includeInOwnTab
): FormField {
$includeInOwnTab = false;
return TreeMultiselectField::create($relationName, $fieldTitle, static::class);
}
/**
* Get many-many relation to {@link Member},
* including all members which are "inherited" from children groups of this record.

View File

@ -40,6 +40,10 @@ use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Exception\RfcComplianceException;
use Closure;
use RuntimeException;
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\SearchableDropdownField;
use SilverStripe\Forms\SearchableMultiDropdownField;
use SilverStripe\ORM\FieldType\DBForeignKey;
/**
* The member class which represents the users of the system
@ -1442,6 +1446,50 @@ class Member extends DataObject
return $labels;
}
public function scaffoldFormFieldForHasOne(
string $fieldName,
?string $fieldTitle,
string $relationName,
DataObject $ownerRecord
): FormField {
$field = parent::scaffoldFormFieldForHasOne($fieldName, $fieldTitle, $relationName, $ownerRecord);
if ($field instanceof SearchableDropdownField) {
$field->setUseSearchContext(true);
}
return $field;
}
public function scaffoldFormFieldForHasMany(
string $relationName,
?string $fieldTitle,
DataObject $ownerRecord,
bool &$includeInOwnTab
): FormField {
$includeInOwnTab = false;
return $this->scaffoldFormFieldForManyRelation($relationName, $fieldTitle);
}
public function scaffoldFormFieldForManyMany(
string $relationName,
?string $fieldTitle,
DataObject $ownerRecord,
bool &$includeInOwnTab
): FormField {
$includeInOwnTab = false;
return $this->scaffoldFormFieldForManyRelation($relationName, $fieldTitle);
}
private function scaffoldFormFieldForManyRelation(string $relationName, ?string $fieldTitle): FormField
{
$list = static::get();
$field = SearchableMultiDropdownField::create($relationName, $fieldTitle, $list);
// Use the same lazyload threshold has_one relations use
$threshold = DBForeignKey::config()->get('dropdown_field_threshold');
$overThreshold = $list->count() > $threshold;
$field->setIsLazyLoaded($overThreshold)->setLazyLoadLimit($threshold);
return $field;
}
/**
* Users can view their own record.
* Otherwise they'll need ADMIN or CMS_ACCESS_SecurityAdmin permissions.