Skip to content

Commit

Permalink
Merge pull request #13 from chtombleson/ss4
Browse files Browse the repository at this point in the history
Silverstripe 4 support
  • Loading branch information
jonom authored Jan 30, 2018
2 parents b337d28 + 882d139 commit 26c8e18
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 81 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ 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

### Composer (best practice)

[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.
Expand Down
4 changes: 0 additions & 4 deletions _config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
<?php

if (!defined('TEXTTARGETLENGTH_DIR')) {
//Assumes this module is installed in web root
define('TEXTTARGETLENGTH_DIR', basename(dirname(__FILE__)));
}
12 changes: 6 additions & 6 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
Name: text-target-length
---
TextField:
SilverStripe\Forms\TextareaField:
extensions:
- TextTargetLengthExtension
TextareaField:
- JonoM\SilverStripeTextTargetLength\TextTargetLengthExtension
SilverStripe\Forms\TextareaField:
extensions:
- TextTargetLengthExtension
HTMLEditorField:
- JonoM\SilverStripeTextTargetLength\TextTargetLengthExtension
SilverStripe\Forms\HTMLEditor\HTMLEditorField:
extensions:
- TextTargetLengthExtension
- JonoM\SilverStripeTextTargetLength\TextTargetLengthExtension
File renamed without changes.
File renamed without changes.
51 changes: 0 additions & 51 deletions code/TextTargetLengthExtension.php

This file was deleted.

37 changes: 22 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
55 changes: 55 additions & 0 deletions src/TextTargetLengthExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace JonoM\SilverStripeTextTargetLength;

use SilverStripe\Core\Extension;
use SilverStripe\View\Requirements;

/**
* Apply this extension to text editing form field such as TextareaField
* to allow setting of ideal, min and max lengths. These are
* soft limits only, to give CMS users an idea of target length.
*/
class TextTargetLengthExtension extends Extension
{
/**
* Set a target character length for text in a field.
*
* @param Int $idealCharCount the ideal number of characters for this text
* @param Int $minCharCount (default: null) the minimum number of characters for this text
* @param Int $maxCharCount (default: null) the maximum number of characters for this text
* @return FormField
*/
public function setTargetLength($idealCharCount, $minCharCount = null, $maxCharCount = null)
{
$field = $this->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: <b>{value}%</b> <i>{remark}</i>'));
$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;
}
}

0 comments on commit 26c8e18

Please sign in to comment.