From 9716223f45442b5d48ff6fd2e34a52df8c7f6c15 Mon Sep 17 00:00:00 2001 From: Roman Schmid Date: Thu, 18 Jan 2018 15:11:14 +0100 Subject: [PATCH] Turn the module into a vendor-module. Some code-style changes in docs and classes. Register `'Color'` as alias for easier upgrade-path. --- README.md | 21 +++++++++++--- _config.php | 8 ----- _config/model.yml | 7 +++++ composer.json | 63 ++++++++++++++++++++++------------------ src/Color.php | 4 +-- src/Forms/ColorField.php | 7 +++-- 6 files changed, 65 insertions(+), 45 deletions(-) create mode 100644 _config/model.yml 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',