Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading