Skip to content

Instantly share code, notes, and snippets.

@kinglozzer
Last active March 8, 2016 12:31
Show Gist options
  • Save kinglozzer/7487105 to your computer and use it in GitHub Desktop.
Save kinglozzer/7487105 to your computer and use it in GitHub Desktop.
SilverStripe page/file/external URL field setup
/**
* File: LinkSwitcher.js
*/
(function($) {
$.entwine(function($) {
/**
* Class: .cms-edit-form .field.switchable-controller
*/
$('.cms-edit-form .field.switchable-controller').entwine({
onmatch: function() {
var id = this.find('input:checked').val();
this.showField(id);
},
// Show the field with the given ID. Looks at siblings, so multiple
// groups of switchable fields will need to be in different tabs,
// or wrapped in CompositeFields
showField: function(id) {
var form = this.closest('form'),
formId = form.attr('id'),
switchableFields = this.siblings().filter('.field.switchable');
switchableFields.hide();
switchableFields.filter(function() {
// Rough match - ID may be #FormName_FieldName or #FormName_FieldName_Holder
return this.id.match(formId + '_' + id);
}).show();
}
});
/**
* Input: .cms-edit-form .field.switchable-controller input
*/
$('.cms-edit-form .field.switchable-controller input').entwine({
onclick: function() {
var id = this.val(),
controller = this.parents('.field.switchable-controller');
controller.showField(id);
this._super();
}
});
});
})(jQuery);
<% loop $List('MyObjectWithLink') %>
<a href="{$Link}" target="{$LinkTarget}">{$Title}</a>
<% end_loop %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment