Skip to content

Commit

Permalink
SilverStripe 4 compatibility (#87)
Browse files Browse the repository at this point in the history
* Update composer constraint and branch alias to support SS4 testing

* Add namespaces, update DataList quirk with getSourceList

* Add PSR-4 autoloader definition

* Move template to correct namespace location, update requirement paths

* FIX Visibility on allowed actions

* FIX Update chosen class names to match updates in framework

* Update Travis configuration for 4.x builds. Update docs for namespaced classes.

* Use "4" for the release instead of master.

* FIX Selected tag height. Move Readonly to own class.
  • Loading branch information
robbieaverill authored and dhensby committed Jan 13, 2017
1 parent 4ce0560 commit 518189e
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 158 deletions.
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Expand Up @@ -6,4 +6,4 @@ checks:
duplication: true

filter:
paths: [code/*, tests/*]
paths: [src/*, tests/*]
22 changes: 7 additions & 15 deletions .travis.yml
Expand Up @@ -4,26 +4,18 @@ sudo: false

language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0

env:
- DB=MYSQL CORE_RELEASE=3.2

matrix:
include:
- php: 5.5
env: DB=MYSQL CORE_RELEASE=4
- php: 5.6
env: DB=MYSQL CORE_RELEASE=3
env: DB=MYSQL CORE_RELEASE=4
- php: 5.6
env: DB=MYSQL CORE_RELEASE=3.1
- php: 5.6
env: DB=PGSQL CORE_RELEASE=3.2
allow_failures:
env: DB=PGSQL CORE_RELEASE=4
- php: 7.0
env: DB=MYSQL CORE_RELEASE=4
- php: 7.0
env: DB=PGSQL CORE_RELEASE=4

before_script:
- composer self-update || true
Expand Down
59 changes: 32 additions & 27 deletions composer.json
@@ -1,29 +1,34 @@
{
"name": "silverstripe/tagfield",
"description": "Tag field for Silverstripe",
"license": "BSD-3-Clause",
"type": "silverstripe-module",
"keywords": [
"silverstripe",
"tag",
"field"
],
"authors": [
{
"name": "Christopher Pitt",
"email": "chris@silverstripe.com",
"homepage": "http://github.com/assertchris"
}
],
"support": {
"issues": "http://github.com/silverstripe-labs/silverstripe-tagfield/issues"
},
"require": {
"silverstripe/framework": "~3.1"
},
"extra": {
"branch-alias": {
"dev-master": "2.x-dev"
}
}
"name": "silverstripe/tagfield",
"description": "Tag field for Silverstripe",
"license": "BSD-3-Clause",
"type": "silverstripe-module",
"keywords": [
"silverstripe",
"tag",
"field"
],
"authors": [
{
"name": "Christopher Pitt",
"email": "chris@silverstripe.com",
"homepage": "http://github.com/assertchris"
}
],
"support": {
"issues": "http://github.com/silverstripe-labs/silverstripe-tagfield/issues"
},
"require": {
"silverstripe/framework": "^4.0"
},
"autoload": {
"psr-4": {
"SilverStripe\\TagField\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "2.x-dev"
}
}
}
4 changes: 2 additions & 2 deletions css/TagField.css
Expand Up @@ -50,7 +50,7 @@
}

.select2-selection__choice {
height: 13px;
height: 22px;
line-height: 13px;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
Expand Down Expand Up @@ -100,4 +100,4 @@
background-image: -ms-linear-gradient(top, #3875d7 20%, #2a62bc 90%) !important;
background-image: linear-gradient(top, #3875d7 20%, #2a62bc 90%) !important;
color: #fff !important;
}
}
17 changes: 11 additions & 6 deletions docs/en/using.md
Expand Up @@ -3,19 +3,21 @@
The primary use, for this module, is as a custom input field interface. For instance, imagine you had the following data objects:

```php
class BlogPost extends DataObject {
class BlogPost extends DataObject
{
private static $many_many = array(
'BlogTags' => 'BlogTag'
'BlogTags' => 'SilverStripe\\Blog\\Model\\BlogTag'
);
}

class BlogTag extends DataObject {
class BlogTag extends DataObject
{
private static $db = array(
'Title' => 'Varchar(200)',
'Title' => 'Varchar(200)'
);

private static $belongs_many_many = array(
'BlogPosts' => 'BlogPost'
'BlogPosts' => 'SilverStripe\\Blog\\Model\\BlogPost'
);
}
```
Expand All @@ -33,6 +35,8 @@ $field = TagField::create(
->setCanCreate(true); // new tag DataObjects can be created
```

**Note:** This assumes you have imported the namespaces class, e.g. `use SilverStripe\TagField\TagField;`.

This will present a tag field, in which you can select existing blog tags or create new ones. They will be created/linked after the blog posts are saved.

You can also store string-based tags, for blog posts, with the following field type:
Expand All @@ -51,7 +55,8 @@ $field->setShouldLazyLoad(true); // tags should be lazy loaded
This assumes you are storing tags in the following data object structure:

```php
class BlogPost extends DataObject {
class BlogPost extends DataObject
{
private static $db = array(
'Tags' => 'Text'
);
Expand Down
8 changes: 4 additions & 4 deletions js/TagField.js
Expand Up @@ -10,11 +10,11 @@
*/
$.fn.chosenDestroy = function () {
var $this = $(this);
if ($this.siblings('.chzn-container').length) {
if ($this.siblings('.chosen-container').length) {
$this
.show() // The field needs to be visible so Select2 evaluates the width correctly.
.removeClass('chzn-done')
.removeClass('has-chzn')
.removeClass('chosen-done')
.removeClass('has-chosen')
.next()
.remove();
}
Expand All @@ -23,7 +23,7 @@

$.entwine('ss', function ($) {

$('.ss-tag-field.has-chzn + .chzn-container, .ss-tag-field:not(.has-chzn)').entwine({
$('.ss-tag-field.has-chosen + .chosen-container, .ss-tag-field:not(.has-chosen)').entwine({
applySelect2: function () {
var self = this,
$select = $(this);
Expand Down
4 changes: 4 additions & 0 deletions src/.upgrade.yml
@@ -0,0 +1,4 @@
mappings:
StringTagField: SilverStripe\TagField\StringTagField
TagField: SilverStripe\TagField\TagField
TagField_Readonly: SilverStripe\TagField\TagField\Readonly
48 changes: 25 additions & 23 deletions code/StringTagField.php → src/StringTagField.php
@@ -1,22 +1,36 @@
<?php

namespace SilverStripe\TagField;

use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Convert;
use SilverStripe\Forms\DropdownField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\ORM\SS_List;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;

/**
* Provides a tagging interface, storing comma-delimited tags in a DataObject string field.
*
* This is intended bridge the gap between 1.x and 2.x, and when possible TagField should be used
* instead.
*
* @package forms
* @package tagfield
* @subpackage fields
*/
class StringTagField extends DropdownField
{
/**
* @var array
*/
public static $allowed_actions = array(
'suggest',
);
public static $allowed_actions = [
'suggest'
];

/**
* @var bool
Expand All @@ -43,17 +57,6 @@ class StringTagField extends DropdownField
*/
protected $isMultiple = true;

/**
* @param string $name
* @param string $title
* @param array|SS_List $source
* @param array|SS_List $value
*/
public function __construct($name, $title = '', $source = array(), $value = array())
{
parent::__construct($name, $title, $source, $value);
}

/**
* @return bool
*/
Expand Down Expand Up @@ -150,8 +153,8 @@ public function Field($properties = array())
Requirements::css(TAG_FIELD_DIR . '/css/select2.min.css');
Requirements::css(TAG_FIELD_DIR . '/css/TagField.css');

Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
Requirements::javascript(ADMIN_THIRDPARTY_DIR . '/jquery/jquery.js');
Requirements::javascript(ADMIN_THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
Requirements::javascript(TAG_FIELD_DIR . '/js/select2.js');
Requirements::javascript(TAG_FIELD_DIR . '/js/TagField.js');

Expand All @@ -173,7 +176,7 @@ public function Field($properties = array())

return $this
->customise($properties)
->renderWith(array("templates/TagField"));
->renderWith(TagField::class);
}

/**
Expand Down Expand Up @@ -264,17 +267,16 @@ public function saveInto(DataObjectInterface $record)
/**
* Returns a JSON string of tags, for lazy loading.
*
* @param SS_HTTPRequest $request
*
* @return SS_HTTPResponse
* @param HTTPRequest $request
* @return HTTPResponse
*/
public function suggest(SS_HTTPRequest $request)
public function suggest(HTTPRequest $request)
{
$responseBody = Convert::raw2json(
array('items' => array())
);

$response = new SS_HTTPResponse();
$response = new HTTPResponse;
$response->addHeader('Content-Type', 'application/json');

if ($record = $this->getRecord()) {
Expand Down

0 comments on commit 518189e

Please sign in to comment.