Skip to content

Commit

Permalink
ENH Auto-scaffold Member and Group with appropriate form fields (#11285)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Jun 26, 2024
1 parent cbc984e commit a684c8c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Security/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand All @@ -27,6 +28,8 @@
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;
Expand Down Expand Up @@ -297,6 +300,35 @@ public function fieldLabels($includerelations = true)
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.
Expand Down
48 changes: 48 additions & 0 deletions src/Security/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
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
Expand Down Expand Up @@ -1442,6 +1446,50 @@ public function fieldLabels($includerelations = true)
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.
Expand Down

0 comments on commit a684c8c

Please sign in to comment.