mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
55 lines
1012 B
PHP
55 lines
1012 B
PHP
|
<?php
|
||
|
|
||
|
use SilverStripe\Forms\SegmentFieldModifier\AbstractSegmentFieldModifier;
|
||
|
|
||
|
class DisambiguationSegmentFieldModifier extends AbstractSegmentFieldModifier {
|
||
|
/**
|
||
|
* @inheritdoc
|
||
|
*
|
||
|
* @param string $value
|
||
|
*
|
||
|
* @return string
|
||
|
*/
|
||
|
public function getPreview($value) {
|
||
|
if($this->form instanceof Form && $record = $this->form->getRecord()) {
|
||
|
$parent = $record->Parent();
|
||
|
|
||
|
$try = $value;
|
||
|
|
||
|
$sibling = EditableformField::get()
|
||
|
->filter('ParentID', $parent->ID)
|
||
|
->filter('Name', $try)
|
||
|
->where('"ID" != ' . $record->ID)
|
||
|
->first();
|
||
|
|
||
|
$counter = 1;
|
||
|
|
||
|
while($sibling !== null) {
|
||
|
$try = $value . '_' . $counter++;
|
||
|
|
||
|
$sibling = EditableformField::get()
|
||
|
->filter('ParentID', $parent->ID)
|
||
|
->filter('Name', $try)
|
||
|
->first();
|
||
|
}
|
||
|
|
||
|
if ($try !== $value) {
|
||
|
return $try;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return $value;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @inheritdoc
|
||
|
*
|
||
|
* @param string $value
|
||
|
*
|
||
|
* @return string
|
||
|
*/
|
||
|
public function getSuggestion($value) {
|
||
|
return $this->getPreview($value);
|
||
|
}
|
||
|
}
|