From e3c42727e447782d5bb0e59ce3bebcdd04b724a6 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Fri, 22 Jul 2016 08:52:17 +1200 Subject: [PATCH] Apply canCreate permissions to Select2 for tags * Resolves #80 --- code/StringTagField.php | 2 ++ code/TagField.php | 12 +++++++----- js/TagField.js | 4 ++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/code/StringTagField.php b/code/StringTagField.php index 551b5b2..33f5ff0 100644 --- a/code/StringTagField.php +++ b/code/StringTagField.php @@ -164,6 +164,8 @@ class StringTagField extends DropdownField )); } + $this->setAttribute('data-can-create', (int) $this->getCanCreate()); + return $this ->customise($properties) ->renderWith(array("templates/TagField")); diff --git a/code/TagField.php b/code/TagField.php index 93db395..120ac71 100644 --- a/code/TagField.php +++ b/code/TagField.php @@ -178,6 +178,8 @@ class TagField extends DropdownField )); } + $this->setAttribute('data-can-create', (int) $this->getCanCreate()); + return $this ->customise($properties) ->renderWith(array("templates/TagField")); @@ -406,7 +408,7 @@ class TagField extends DropdownField { return true; } - + /** * Converts the field to a readonly variant. * @@ -429,7 +431,7 @@ class TagField extends DropdownField class TagField_Readonly extends TagField { protected $readonly = true; - + /** * Render the readonly field as HTML. * @@ -439,13 +441,13 @@ class TagField_Readonly extends TagField public function Field($properties = array()) { $options = array(); - + foreach ($this->getOptions()->filter('Selected', true) as $option) { $options[] = $option->Title; } - + $field = ReadonlyField::create($this->name.'_Readonly', $this->title); - + $field->setForm($this->form); $field->setValue(implode(', ', $options)); return $field->Field(); diff --git a/js/TagField.js b/js/TagField.js index 49b9181..49e9611 100644 --- a/js/TagField.js +++ b/js/TagField.js @@ -46,6 +46,10 @@ 'tokenSeparators': [','] }; + if ($select.attr('data-can-create') == 0) { + options.tags = false; + } + if ($select.attr('data-ss-tag-field-suggest-url')) { options.ajax = { 'url': $select.attr('data-ss-tag-field-suggest-url'),