diff --git a/README.md b/README.md index 1d35a1b..59a1188 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This module extends the `TextField`, `TextareaField` and `HTMLEditorField` class ## Requirements -SilverStripe 3.1+ (3.3 tested) +SilverStripe 4.0+ (3.1+ in previous releases) ## Installation @@ -16,10 +16,6 @@ SilverStripe 3.1+ (3.3 tested) [Packagist listing](https://packagist.org/packages/jonom/silverstripe-text-target-length) and [installation instructions](http://doc.silverstripe.org/framework/en/trunk/installation/composer#adding-modules-to-your-project) -### Manually - -I promise it's worth your time to learn how to use Composer. If painless updating isn't your thing though you can download and extract this project, rename the module folder 'text-target-length', place it in your project root and run a dev/build?flush=1. - ## How to use With the module installed you can call call `setTargetLength()` on `TextField`, `TextareaField` and `HTMLEditorField` form fields. diff --git a/_config.php b/_config.php index 8093b43..a4abe2d 100644 --- a/_config.php +++ b/_config.php @@ -1,6 +1,2 @@ owner; - $idealCharCount = (int)$idealCharCount; - if (!$idealCharCount > 0) return $field; - - // Set defaults - if ($minCharCount === null) $minCharCount = round($idealCharCount * .75); - if ($maxCharCount === null) $maxCharCount = round($idealCharCount * 1.25); - - // Validate - if (!($maxCharCount >= $idealCharCount && $idealCharCount >= $minCharCount)) return $field; - - // Activate - $field->addExtraClass('target-length'); - $field->setAttribute('data-target-ideal-length', $idealCharCount); - $field->setAttribute('data-target-min-length', $minCharCount); - $field->setAttribute('data-target-max-length', $maxCharCount); - - $field->setAttribute('data-hint-length-target', _t('TextTargetLength.LengthTarget', 'Length target: {value}% {remark}')); - $field->setAttribute('data-hint-length-tooshort', _t('TextTargetLength.LengthTooShort', 'Keep going!')); - $field->setAttribute('data-hint-length-toolong', _t('TextTargetLength.LengthTooLong', 'Too long')); - $field->setAttribute('data-hint-length-adequate', _t('TextTargetLength.LengthAdequate', 'Okay')); - $field->setAttribute('data-hint-length-ideal', _t('TextTargetLength.LengthIdeal', 'Great!')); - - Requirements::javascript(FRAMEWORK_DIR.'/thirdparty/jquery/jquery.js'); - Requirements::javascript(FRAMEWORK_DIR.'/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js'); - Requirements::javascript(TEXTTARGETLENGTH_DIR.'/javascript/text-target-length.js'); - Requirements::css(TEXTTARGETLENGTH_DIR.'/css/text-target-length.css'); - - return $field; - } -} diff --git a/composer.json b/composer.json index 5afc7f8..9f362c7 100644 --- a/composer.json +++ b/composer.json @@ -1,17 +1,24 @@ { - "name": "jonom/silverstripe-text-target-length", - "description": "Set character length recommendations on SilverStripe text form fields", - "type": "silverstripe-module", - "keywords": ["silverstripe", "textfield", "textareafield", "target"], - "license": "BSD-3-Clause", - "authors": [{ - "name": "Jonathon Menz", - "homepage": "http://jonathonmenz.com" - }], - "require": { - "silverstripe/framework": "^3.1" - }, - "extra": { - "installer-name": "text-target-length" - } + "name": "jonom/silverstripe-text-target-length", + "description": "Set character length recommendations on SilverStripe text form fields", + "type": "silverstripe-vendormodule", + "keywords": ["silverstripe", "textfield", "textareafield", "target"], + "license": "BSD-3-Clause", + "authors": [{ + "name": "Jonathon Menz", + "homepage": "http://jonathonmenz.com" + }], + "require": { + "silverstripe/framework": "^4.0" + }, + "autoload": { + "psr-4": { + "JonoM\\SilverStripeTextTargetLength\\": "src/" + } + }, + "extra": { + "expose": [ + "client" + ] + } } diff --git a/src/TextTargetLengthExtension.php b/src/TextTargetLengthExtension.php new file mode 100644 index 0000000..24ab778 --- /dev/null +++ b/src/TextTargetLengthExtension.php @@ -0,0 +1,55 @@ +owner; + $idealCharCount = (int)$idealCharCount; + if (!$idealCharCount > 0) return $field; + + // Set defaults + if ($minCharCount === null) $minCharCount = round($idealCharCount * .75); + if ($maxCharCount === null) $maxCharCount = round($idealCharCount * 1.25); + + // Validate + if (!($maxCharCount >= $idealCharCount && $idealCharCount >= $minCharCount)) return $field; + + // Activate + $field->addExtraClass('target-length'); + $field->setAttribute('data-target-ideal-length', $idealCharCount); + $field->setAttribute('data-target-min-length', $minCharCount); + $field->setAttribute('data-target-max-length', $maxCharCount); + + $field->setAttribute('data-hint-length-target', _t('TextTargetLength.LengthTarget', 'Length target: {value}% {remark}')); + $field->setAttribute('data-hint-length-tooshort', _t('TextTargetLength.LengthTooShort', 'Keep going!')); + $field->setAttribute('data-hint-length-toolong', _t('TextTargetLength.LengthTooLong', 'Too long')); + $field->setAttribute('data-hint-length-adequate', _t('TextTargetLength.LengthAdequate', 'Okay')); + $field->setAttribute('data-hint-length-ideal', _t('TextTargetLength.LengthIdeal', 'Great!')); + + Requirements::javascript('silverstripe/admin:thirdparty/jquery/jquery.js'); + Requirements::javascript('silverstripe/admin:thirdparty/jquery-entwine/dist/jquery.entwine-dist.js'); + Requirements::javascript('jonom/silverstripe-text-target-length:client/javascript/text-target-length.js'); + Requirements::css('jonom/silverstripe-text-target-length:client/css/text-target-length.css'); + + return $field; + } +}