Last active
March 8, 2016 12:31
-
-
Save kinglozzer/7487105 to your computer and use it in GitHub Desktop.
SilverStripe page/file/external URL field setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyObjectWithLink extends DataObject { | |
private static $db = array( | |
'Title' => 'Varchar', | |
'URL' => 'Varchar', | |
'LinkTarget' => 'Varchar' | |
); | |
private static $has_one = array( | |
'PageLink' => 'SiteTree', | |
'FileLink' => 'File' | |
); | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getCMSFields() { | |
Requirements::javascript('mysite/javascript/LinkSwitcher.js'); | |
$fields = parent::getCMSFields(); | |
// Hide undesired fields | |
$fields->removeByName('PageLinkID'); | |
$fields->removeByName('FileLinkID'); | |
$fields->removeByName('URL'); | |
$fields->removeByName('LinkTarget'); | |
$linkTypeField = OptionsetField::create( | |
'LinkType', | |
'', | |
array( | |
'PageLinkID' => 'Link to a page on this site', | |
'FileLink' => 'Link to a file on this site', | |
'URL' => 'Link to another website' | |
), | |
($this->URL ? 'URL' : ($this->FileLinkID ? 'FileLink' : 'PageLinkID')) | |
)->addExtraClass('switchable-controller'); | |
$internalLinkField = TreeDropdownField::create('PageLinkID', 'Linked page', 'SiteTree') | |
->addExtraClass('switchable'); | |
$fileLinkField = UploadField::create('FileLink', 'Linked file') | |
->addExtraClass('switchable'); | |
$externalLinkField = TextField::create('URL', 'Linked page') | |
->setDescription('Please include the "http://" prefix') | |
->addExtraClass('switchable'); | |
$targetField = DropdownField::create( | |
'LinkTarget', | |
'Link target', | |
array( | |
'' => 'Open in the same window', | |
'_blank' => 'Open in a new window' | |
) | |
); | |
$fields->addFieldsToTab( | |
'Root.Main', | |
array( | |
$linkTypeField, | |
$internalLinkField, | |
$fileLinkField, | |
$externalLinkField, | |
$targetField | |
) | |
); | |
return $fields; | |
} | |
/** | |
* @return string | |
*/ | |
public function getLink() { | |
$page = $this->PageLink(); | |
if($page->exists()) { | |
return $page->Link(); | |
} | |
$file = $this->FileLink(); | |
if ($file->exists()) { | |
return $file->Link(); | |
} | |
return $this->URL; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% 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