Skip to content

Commit

Permalink
API Rename FormField Value to getFormattedValue
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jan 24, 2025
1 parent cedd284 commit 3462520
Show file tree
Hide file tree
Showing 58 changed files with 248 additions and 239 deletions.
4 changes: 2 additions & 2 deletions src/Forms/CheckboxField.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function dataValue()
return ($this->value) ? 1 : null;
}

public function Value()
public function getValue(): mixed
{
return ($this->value) ? 1 : 0;
}
Expand All @@ -39,7 +39,7 @@ public function getAttributes()
return array_merge(
$attributes,
[
'checked' => ($this->Value()) ? 'checked' : null,
'checked' => ($this->getValue()) ? 'checked' : null,
'type' => 'checkbox',
]
);
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/CheckboxField_Readonly.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function performReadonlyTransformation()
return clone $this;
}

public function Value()
public function getFormattedValue(): mixed
{
return $this->value ?
_t('SilverStripe\\Forms\\CheckboxField.YESANSWER', 'Yes') :
Expand Down
10 changes: 5 additions & 5 deletions src/Forms/ConfirmedPasswordField.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function Field($properties = [])
}

// Check if the field should be visible up front
$visible = $this->hiddenField->Value();
$visible = $this->hiddenField->getValue();
$classes = $visible
? 'showOnClickContainer'
: 'showOnClickContainer d-none';
Expand Down Expand Up @@ -418,7 +418,7 @@ public function setName($name)
public function isSaveable()
{
return !$this->showOnClick
|| ($this->showOnClick && $this->hiddenField && $this->hiddenField->Value());
|| ($this->showOnClick && $this->hiddenField && $this->hiddenField->getValue());
}

public function validate(): ValidationResult
Expand All @@ -432,10 +432,10 @@ public function validate(): ValidationResult

$this->getPasswordField()->setValue($this->value);
$this->getConfirmPasswordField()->setValue($this->confirmValue);
$value = $this->getPasswordField()->Value();
$value = $this->getPasswordField()->getValue();

// both password-fields should be the same
if ($value != $this->getConfirmPasswordField()->Value()) {
if ($value != $this->getConfirmPasswordField()->getValue()) {
$result->addFieldError(
$name,
_t('SilverStripe\\Forms\\Form.VALIDATIONPASSWORDSDONTMATCH', "Passwords don't match"),
Expand All @@ -446,7 +446,7 @@ public function validate(): ValidationResult

if (!$this->canBeEmpty) {
// both password-fields shouldn't be empty
if (!$value || !$this->getConfirmPasswordField()->Value()) {
if (!$value || !$this->getConfirmPasswordField()->getValue()) {
$result->addFieldError(
$name,
_t('SilverStripe\\Forms\\Form.VALIDATIONPASSWORDSNOTEMPTY', "Passwords can't be empty"),
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/DateField.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public function setValue($value, $data = null)
return $this;
}

public function Value()
public function getFormattedValue(): mixed
{
return $this->internalToFrontend($this->value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/DateField_Disabled.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function Field($properties = [])
// Render the display value with some complement of info
$displayValue = Convert::raw2xml(sprintf(
$format ?? '',
$this->Value(),
$this->getFormattedValue(),
$infoComplement
));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/DatetimeField.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public function setValue($value, $data = null)
*
* @return string
*/
public function Value()
public function getFormattedValue(): mixed
{
return $this->internalToFrontend($this->value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/DropdownField.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class DropdownField extends SingleSelectField
protected function getFieldOption($value, $title)
{
// Check selection
$selected = $this->isSelectedValue($value, $this->Value());
$selected = $this->isSelectedValue($value, $this->getValue());

// Check disabled
$disabled = false;
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/FileField.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function saveInto(DataObjectInterface $record)
}
}

public function Value()
public function getValue(): mixed
{
return isset($_FILES[$this->getName()]) ? $_FILES[$this->getName()] : null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Forms/FileUploadReceiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ public function getItems()
*/
public function getItemIDs()
{
$value = $this->Value();
$value = $this->getValue();
return empty($value['Files']) ? [] : $value['Files'];
}

public function Value()
public function getValue(): mixed
{
// Re-override FileField Value to use data value
return $this->dataValue();
Expand Down
25 changes: 10 additions & 15 deletions src/Forms/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ class FormField extends RequestHandler implements FieldValidationInterface
'Field' => 'HTMLFragment',
'AttributesHTML' => 'HTMLFragment', // property $AttributesHTML version
'getAttributesHTML' => 'HTMLFragment', // method $getAttributesHTML($arg) version
'Value' => 'Text',
'FormattedValue' => 'Text',
'getFormattedValue' => 'Text',
'extraClass' => 'Text',
'ID' => 'Text',
'isReadOnly' => 'Boolean',
Expand Down Expand Up @@ -457,12 +458,10 @@ public function getValue(): mixed
/**
* Returns the value of the field which may be modified for display purposes
* for instance to add localisation or formatting.
*
* @return mixed
*/
public function Value()
public function getFormattedValue(): mixed
{
return $this->value;
return $this->getValue();
}

/**
Expand Down Expand Up @@ -668,7 +667,7 @@ protected function getDefaultAttributes(): array
$attributes = [
'type' => $this->getInputType(),
'name' => $this->getName(),
'value' => $this->Value(),
'value' => $this->getFormattedValue(),
'class' => $this->extraClass(),
'id' => $this->ID(),
'disabled' => $this->isDisabled(),
Expand Down Expand Up @@ -948,16 +947,12 @@ public function Field($properties = [])
$context = $context->customise($properties);
}

$result = $context->renderWith($context->getTemplates());
$dbHtmlText = $context->renderWith($context->getTemplates());

// Trim whitespace from the result, so that trailing newlines are suppressed. Works for strings and HTMLText values
if (is_string($result)) {
$result = trim($result ?? '');
} elseif ($result instanceof DBField) {
$result->setValue(trim($result->getValue() ?? ''));
}
// Trim whitespace from the result, so that trailing newlines are suppressed.
$dbHtmlText->setValue(trim($dbHtmlText->getValue() ?? ''));

return $result;
return $dbHtmlText;
}

/**
Expand Down Expand Up @@ -1554,7 +1549,7 @@ public function getSchemaStateDefaults()
$state = [
'name' => $this->getName(),
'id' => $this->ID(),
'value' => $this->Value(),
'value' => $this->getFormattedValue(),
'message' => $this->getSchemaMessage(),
'data' => [],
];
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/GridField/GridField.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ private function addStateFromRequest(): void
// Create a dummy state so that we can merge the current state with the request state.
$newState = new GridState($this, $stateStr);
// Put the current state on top of the request state.
$newState->setValue($oldState->Value());
$newState->setValue($oldState->getValue());
$this->state = $newState;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/GridField/GridFieldStateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getStateKey(GridField $gridField): string
public function addStateToURL(GridField $gridField, string $url): string
{
$key = $this->getStateKey($gridField);
$value = $gridField->getState(false)->Value();
$value = $gridField->getState(false)->getValue();

// Using a JSON-encoded empty array as the blank value, to avoid changing Value() semantics in a minor release
if ($value === '{}') {
Expand Down
10 changes: 4 additions & 6 deletions src/Forms/GridField/GridState.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ public function getList()

/**
* Returns a json encoded string representation of this state.
*
* @return string
*/
public function Value()
public function getValue(): string
{
$data = $this->data ? $this->data->getChangesArray() : [];
return json_encode($data, JSON_FORCE_OBJECT);
Expand All @@ -117,7 +115,7 @@ public function Value()
*/
public function dataValue()
{
return $this->Value();
return $this->getValue();
}

/**
Expand All @@ -126,11 +124,11 @@ public function dataValue()
*/
public function attrValue()
{
return Convert::raw2att($this->Value());
return Convert::raw2att($this->getValue());
}

public function __toString()
{
return $this->Value();
return $this->getValue();
}
}
17 changes: 9 additions & 8 deletions src/Forms/HTMLEditor/HTMLEditorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class HTMLEditorField extends TextareaField
{

private static $casting = [
'Value' => 'HTMLText',
'FormattedValue' => 'HTMLText',
'getFormattedValue' => 'HTMLText',
];

protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_HTML;
Expand Down Expand Up @@ -134,7 +135,7 @@ public function saveInto(DataObjectInterface $record)
}

// Sanitise if requested
$htmlValue = HTMLValue::create($this->Value());
$htmlValue = HTMLValue::create($this->getValue());
if (HTMLEditorField::config()->sanitise_server_side) {
$config = $this->getEditorConfig();
$santiser = HTMLEditorSanitiser::create($config);
Expand Down Expand Up @@ -184,25 +185,25 @@ public function getSchemaStateDefaults()
}

/**
* Return value with all values encoded in html entities
* Return formatted value with all values encoded in html entities
*
* @return string Raw HTML
*/
public function ValueEntities()
public function getFormattedValueEntities()
{
$entities = get_html_translation_table(HTML_ENTITIES);

foreach ($entities as $key => $value) {
$entities[$key] = "/" . $value . "/";
}

$value = preg_replace_callback($entities, function ($matches) {
$formattedValue = preg_replace_callback($entities, function ($matches) {
// Don't apply double encoding to ampersand
$doubleEncoding = $matches[0] != '&';
return htmlentities($matches[0], ENT_COMPAT, 'UTF-8', $doubleEncoding);
}, $this->Value() ?? '');
}, $this->getFormattedValue() ?? '');

return $value;
return $formattedValue;
}

/**
Expand Down Expand Up @@ -231,7 +232,7 @@ private function usesXmlFriendlyField(DataObjectInterface $record): bool
}

$castingService = CastingService::singleton();
$castValue = $castingService->cast($this->Value(), $record, $this->getName());
$castValue = $castingService->cast($this->getFormattedValue(), $record, $this->getName());
return $castValue instanceof DBField && $castValue::config()->get('escape_type') === 'xml';
}
}
3 changes: 2 additions & 1 deletion src/Forms/HTMLEditor/HTMLEditorField_Readonly.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
class HTMLEditorField_Readonly extends HTMLReadonlyField
{
private static $casting = [
'Value' => 'HTMLText',
'FormattedValue' => 'HTMLText',
'getFormattedValue' => 'HTMLText',
];

public function Type()
Expand Down
12 changes: 7 additions & 5 deletions src/Forms/HTMLReadonlyField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
class HTMLReadonlyField extends ReadonlyField
{
private static $casting = [
'Value' => 'HTMLFragment',
'ValueEntities' => 'HTMLFragment',
'FormattedValue' => 'HTMLFragment',
'getFormattedValue' => 'HTMLFragment',
'FormattedValueEntities' => 'HTMLFragment',
'getFormattedValueEntities' => 'HTMLFragment',
];

protected $schemaDataType = HTMLReadonlyField::SCHEMA_DATA_TYPE_STRUCTURAL;
Expand All @@ -27,12 +29,12 @@ public function Field($properties = [])
}

/**
* Return value with all values encoded in html entities
* Return formatted value with all values encoded in html entities
*
* @return string Raw HTML
*/
public function ValueEntities()
public function getFormattedValueEntities()
{
return htmlentities($this->Value() ?? '', ENT_COMPAT, 'UTF-8');
return htmlentities($this->getFormattedValue() ?? '', ENT_COMPAT, 'UTF-8');
}
}
7 changes: 3 additions & 4 deletions src/Forms/ListboxField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use SilverStripe\Model\List\ArrayList;
use SilverStripe\Model\ArrayData;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBHTMLText;

/**
* Multi-line listbox field, created from a select tag.
Expand Down Expand Up @@ -68,9 +70,6 @@ public function __construct($name, $title = '', $source = [], $value = null, $si

/**
* Returns a select tag containing all the appropriate option tags
*
* @param array $properties
* @return string
*/
public function Field($properties = [])
{
Expand Down Expand Up @@ -252,7 +251,7 @@ protected function getOptionsArray($term)

public function getValueArray()
{
$value = $this->Value();
$value = $this->getFormattedValue();
$validValues = $this->getValidValues();
if (empty($validValues)) {
return [];
Expand Down
3 changes: 2 additions & 1 deletion src/Forms/LiteralField.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class LiteralField extends DatalessField
{

private static $casting = [
'Value' => 'HTMLFragment',
'FormattedValue' => 'HTMLFragment',
'getFormattedValue' => 'HTMLFragment',
];

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/MoneyField.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function dataValue()
return $this->getDBMoney()->getValue();
}

public function Value()
public function getFormattedValue(): mixed
{
// Localised money
return $this->getDBMoney()->Nice();
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/MultiSelectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class MultiSelectField extends SelectField
*/
public function getValueArray()
{
return $this->getListValues($this->Value());
return $this->getListValues($this->getFormattedValue());
}

/**
Expand Down
Loading

0 comments on commit 3462520

Please sign in to comment.