Skip to content
Snippets Groups Projects
Commit d95d3cba authored by Ferdinand Aneke's avatar Ferdinand Aneke Committed by Jo Fitzgerald
Browse files

Issue #3381194 by chike, jofitz: More modifiers for Drupal 9/10

parent 0c630d8c
No related branches found
No related tags found
1 merge request!3Resolve #3381194 "Extra modifiers d9 d10" - Title case and Upper case added
<?php
namespace Drupal\token_modifier\Plugin\token_modifier;
use Drupal\token_modifier\Plugin\TokenModifierPluginBase;
/**
* Makes the first characters of each word uppercase.
*
* @TokenModifier(
* id = "title-case",
* name = @Translation("Title case"),
* description = @Translation("Uppercases the first letter of each word.")
* )
*/
class TitleCase extends TokenModifierPluginBase {
/**
* {@inheritdoc}
*/
public function transform(string $text, array $data = [], array $options = []) {
return ucwords($this->token->replace($text, $data, $options));
}
}
<?php
namespace Drupal\token_modifier\Plugin\token_modifier;
use Drupal\token_modifier\Plugin\TokenModifierPluginBase;
/**
* Makes all characters of the string uppercase.
*
* @TokenModifier(
* id = "uppercase",
* name = @Translation("Upper case"),
* description = @Translation("Uppercases all characters.")
* )
*/
class UpperCase extends TokenModifierPluginBase {
/**
* {@inheritdoc}
*/
public function transform(string $text, array $data = [], array $options = []) {
return strtoupper($this->token->replace($text, $data, $options));
}
}
<?php
namespace Drupal\Tests\token_modifier\Unit;
use Drupal\token_modifier\Plugin\token_modifier\TitleCase;
class TitleCaseTest extends TokenModifierTestCase {
/**
* Tests the Title Case token modifier.
*
* @param string $value
* The source value for the modifier.
* @param string $expected
* The expected result.
*
* @covers ::transform
*
* @dataProvider providerTestTransform
*/
public function testTransform(string $value, string $expected) {
$plugin = new TitleCase([], 'title-case', [], $this->token);
$actual = $plugin->transform($value);
$this->assertSame($expected, $actual);
}
/**
* Provides data for testTransform.
*/
public function providerTestTransform(): array {
$data['basic'] = ['hello world!', 'Hello World!'];
$data['already capitalised'] = ['It\'s Working?', 'It\'s Working?'];
$data['everything else capitalised'] = ['cAPITALISE eVERYTHING', 'CAPITALISE EVERYTHING'];
return $data;
}
}
<?php
namespace Drupal\Tests\token_modifier\Unit;
use Drupal\token_modifier\Plugin\token_modifier\UpperCase;
class UpperCaseTest extends TokenModifierTestCase {
/**
* Tests the Upper Case token modifier.
*
* @param string $value
* The source value for the modifier.
* @param string $expected
* The expected result.
*
* @covers ::transform
*
* @dataProvider providerTestTransform
*/
public function testTransform(string $value, string $expected) {
$plugin = new UpperCase([], 'uppercase', [], $this->token);
$actual = $plugin->transform($value);
$this->assertSame($expected, $actual);
}
/**
* Provides data for testTransform.
*/
public function providerTestTransform(): array {
$data['basic'] = ['hello world!', 'HELLO WORLD!'];
$data['already capitalised'] = ['WORKING?', 'WORKING?'];
$data['camel case'] = ['cApItAlIsE', 'CAPITALISE'];
return $data;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment