diff --git a/README.md b/README.md index 38671ed..b952df4 100644 --- a/README.md +++ b/README.md @@ -22,11 +22,24 @@ composer require tractorcow/silverstripe-colorpicker ^4@dev Here's how you define a DB field to be a color: ```php -private static $db = array( - 'BgColor' => 'TractorCow\Colorpicker\Color' -); +private static $db = [ + 'BgColor' => 'Color' +]; ``` - + +Alternatively, you can also use the fully qualified classname. +The best way to do this is to import the class at the top of your PHP file, like so: + + use TractorCow\Colorpicker\Color; + +In your class, you can then use: + +```php +private static $db = [ + 'BgColor' => Color::class +]; +``` + That's all... scaffolding will take care of creating the appropriate form-field. If you use `getCMSFields` to create your fields yourself, you might want to do something like this: diff --git a/_config.php b/_config.php index c868623..b3d9bbc 100644 --- a/_config.php +++ b/_config.php @@ -1,9 +1 @@ 'Float', - 'AlteredColorHSV' => 'TractorCow\Colorpicker\Color' + 'AlteredColorHSV' => Color::class ]; /** @@ -168,7 +168,7 @@ public function __construct($name = null, $options = []) public function scaffoldFormField($title = null, $params = null) { - $field = new ColorField($this->name, $title); + $field = ColorField::create($this->name, $title); return $field; } diff --git a/src/Forms/ColorField.php b/src/Forms/ColorField.php index 450385e..87a7e18 100644 --- a/src/Forms/ColorField.php +++ b/src/Forms/ColorField.php @@ -27,10 +27,11 @@ public function __construct($name, $title = null, $value = '', $form = null) public function Field($properties = []) { + Requirements::javascript('tractorcow/silverstripe-colorpicker: client/javascript/colorfield.js'); + Requirements::javascript('tractorcow/silverstripe-colorpicker: client/javascript/colorpicker.js'); + Requirements::css('tractorcow/silverstripe-colorpicker: client/css/colorpicker.css'); + $this->addExtraClass('colorfield'); - Requirements::javascript(COLORPICKER_DIR . '/client/javascript/colorpicker.js'); - Requirements::javascript(COLORPICKER_DIR . '/client/javascript/colorfield.js'); - Requirements::css(COLORPICKER_DIR . '/client/css/colorpicker.css'); $this->setAttribute('style', sprintf( 'background-image: none; background-color: %s; %s',