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

NEW Scaffolded field labels now only have an uppercased first word #8698

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
22 changes: 17 additions & 5 deletions src/Forms/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,24 +295,36 @@ class FormField extends RequestHandler
*
* Examples:
*
* - 'TotalAmount' will return 'Total Amount'
* - 'Organisation.ZipCode' will return 'Organisation Zip Code'
* - 'TotalAmount' will return 'Total amount'
* - 'Organisation.ZipCode' will return 'Organisation zip code'
*
* @param string $fieldName
*
* @return string
*/
public static function name_to_label($fieldName)
{
// Handle dot delimiters
if (strpos($fieldName, '.') !== false) {
$parts = explode('.', $fieldName);

$label = $parts[count($parts) - 2] . ' ' . $parts[count($parts) - 1];
// Ensure that any letter following a dot is uppercased, so that the regex below can break it up
// into words
$label = implode(array_map('ucfirst', $parts));
} else {
$label = $fieldName;
}

return preg_replace('/([a-z]+)([A-Z])/', '$1 $2', $label);
// Replace any capital letter that is followed by a lowercase letter with a space, the lowercased
// version of itself then the remaining lowercase letters.
$labelWithSpaces = preg_replace_callback('/([A-Z])([a-z]+)/', function ($matches) {
return ' ' . strtolower($matches[1]) . $matches[2];
}, $label);

// Add a space before any capital letter block that is at the end of the string
$labelWithSpaces = preg_replace('/([a-z])([A-Z]+)$/', '$1 $2', $labelWithSpaces);

// The first letter should be uppercase
return ucfirst(trim($labelWithSpaces));
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/php/Forms/FieldListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,29 +482,29 @@ public function testTabTitles()
);

$this->assertEquals(
$tabSetWithTitle->Title(),
'My TabSet Title',
'Automatic conversion of tab identifiers through findOrMakeTab() with FormField::name_to_label()'
$tabSetWithTitle->Title(),
'Existing field title should be used'
);

$tabWithoutTitle = $set->findOrMakeTab('Root.TabWithoutTitle');
$this->assertEquals(
'Tab without title',
$tabWithoutTitle->Title(),
'Tab Without Title',
'Automatic conversion of tab identifiers through findOrMakeTab() with FormField::name_to_label()'
);

$tabWithTitle = $set->findOrMakeTab('Root.TabWithTitle', 'My Tab with Title');
$this->assertEquals(
$tabWithTitle->Title(),
'My Tab with Title',
$tabWithTitle->Title(),
'Setting of simple tab titles through findOrMakeTab()'
);

$childTabWithTitle = $set->findOrMakeTab('Root.TabSetWithoutTitle.NewChildTab', 'My Child Tab Title');
$this->assertEquals(
$childTabWithTitle->Title(),
'My Child Tab Title',
$childTabWithTitle->Title(),
'Setting of nested tab titles through findOrMakeTab() works on last child tab'
);
}
Expand Down
27 changes: 27 additions & 0 deletions tests/php/Forms/FormFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,31 @@ public function testLinkWithoutForm()
$field = new FormField('Test');
$field->Link('bar');
}

/**
* @param string $name
* @param string $expected
* @dataProvider nameToLabelProvider
*/
public function testNameToLabel($name, $expected)
{
$this->assertSame($expected, FormField::name_to_label($name));
}

/**
* @return array[]
*/
public function nameToLabelProvider()
{
return [
['TotalAmount', 'Total amount'],
['Organisation.ZipCode', 'Organisation zip code'],
['Organisation.zipCode', 'Organisation zip code'],
['FooBarBaz', 'Foo bar baz'],
['URLSegment', 'URL segment'],
robbieaverill marked this conversation as resolved.
Show resolved Hide resolved
['ONLYCAPS', 'ONLYCAPS'],
['onlylower', 'Onlylower'],
['SpecialURL', 'Special URL'],
];
}
}
4 changes: 2 additions & 2 deletions tests/php/Forms/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public function testValidationExemptActions()
// Firstly, assert that required fields still work when not using an exempt action
$this->assertPartialMatchBySelector(
'#Form_Form_SomeRequiredField_Holder .required',
array('"Some Required Field" is required'),
array('"Some required field" is required'),
'Required fields show a notification on field when left blank'
);

Expand Down Expand Up @@ -487,7 +487,7 @@ public function testSessionValidationMessage()
$this->assertPartialMatchBySelector(
'#Form_Form_SomeRequiredField_Holder span.required',
array(
'"Some Required Field" is required'
'"Some required field" is required'
),
'Required fields show a notification on field when left blank'
);
Expand Down
2 changes: 1 addition & 1 deletion tests/php/i18n/i18nTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function testDataObjectFieldLabels()
$obj->fieldLabel('MyProperty')
);
$this->assertEquals(
'My Untranslated Property',
'My untranslated property',
$obj->fieldLabel('MyUntranslatedProperty')
);
}
Expand Down