Skip to content

Commit

Permalink
Merge pull request #149 from Taitava/new-window
Browse files Browse the repository at this point in the history
BetterButtonLink: Create a newWindow() method which prevents the CMS …
  • Loading branch information
Aaron Carlino committed Jan 12, 2017
2 parents 04ff8bf + c8577f5 commit 5aab654
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
23 changes: 19 additions & 4 deletions code/actions/BetterButtonAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,29 @@ public function shouldDisplay() {
*/
public function getButtonHTML() {
return sprintf(
'<a class="%s %s" href="%s" %s>%s</a>',
$this->isGrouped() ? '' : 'ss-ui-button cms-panel-link',
$this->extraClass(),
'<a class="%s" href="%s" %s>%s</a>',
$this->getButtonClasses(),
$this->getButtonLink(),
$this->getAttributesHTML(),
$this->getAttributesHTML('class'), //Prevent outputting the 'class' attribute twice by excluding it from other attributes
$this->getButtonText()
);
}

/**
* Combines classes from $this->extraClass() with a couple of additional classes if they are applicable for this button.
* @return string
*/
private function getButtonClasses() {
$classes = $this->extraClass();
if ($this->isGrouped()) return $classes; //Do not return the below additional classes
$classes .= ' ss-ui-button';
if ($this->getAttribute('target') != '_blank') {
//Only add this class if this link is targeted inside the CMS. Any links targeted to a new browser window/tab should not have this as the CMS JavaScript would hook to the 'onclick' event and load the content via AJAX to the CMS, which could cause problems.
$classes .= ' cms-panel-link';
}

return $classes;
}


/**
Expand Down
11 changes: 11 additions & 0 deletions code/actions/BetterButtonLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,15 @@ public function __construct($text, $link) {
public function getButtonLink() {
return $this->link;
}

/**
* Makes the link to open to a new tab. If not used, the CMS will try to load the link via an AJAX request, which
* can cause problems if the link target is not a page inside the CMS.
*
* @param bool $enable True if omitted
*/
public function newWindow($enable = true)
{
$this->setAttribute('target', $enable ? '_blank' : '');
}
}

0 comments on commit 5aab654

Please sign in to comment.