Handling anchor target attribute

Permalink Browser Info Environment
Hi,

my problem was the lack of handling target attribute in the nav item.
I made a small change in the code, so external links are displayed in new browser window.

The changings in block's controller.php:

function view() {
$nh = Loader::helper('navigation');
$linkRows = $this->getLinks();
$linkObjs = array();
foreach ($linkRows as $row) {
$page = Page::getByID($row['linkToCID']);
$link = new stdClass;
$link->url = $nh->getLinkToCollection($page);
$link->text = $row['linkText'];
$link->cID = $row['linkToCID'];
$link->cPath = $page->getCollectionPath();
// UPDATE //
$link->external_link = ($row['cPointerExternalLink'] != null);
$linkObjs[] = $link;
}
$this->set('links', $linkObjs);
}

function getLinks() {
$db = Loader::db();
// Pages table JOINED to the query //
$sql = 'SELECT bmnl.*, p.cPointerExternalLink FROM btManualNavLinks bmnl LEFT JOIN Pages p ON p.cID = bmnl.linkToCID WHERE bID=' . intval($this->bID) . ' ORDER BY position';
return $db->getAll($sql);
}

Changings in the block's view.php:

<li class="<?php echo $cssClasses; ?>">
<a href="<?php echo $link->url; ?>" class="<?php echo $cssClasses; ?>" <?php if ($link->external_link): ?>target="_new"<?php endif; ?>>
<?php echo htmlentities($link->text, ENT_QUOTES, APP_CHARSET); ?>
</a>
</li>

Type: Discussion
Status: New
View Replies:
megant replied on at Permalink Reply
Update:

controller.php

function view() {
$nh = Loader::helper('navigation');
$linkRows = $this->getLinks();
$linkObjs = array();
foreach ($linkRows as $row) {
$page = Page::getByID($row['linkToCID']);
$link = new stdClass;
$link->url = $nh->getLinkToCollection($page);
$link->text = $row['linkText'];
$link->cID = $row['linkToCID'];
$link->cPath = $page->getCollectionPath();
// Checks NEW WINDOW
$link->new_window = $row['cPointerExternalLinkNewWindow'];
// Checks external link
$link->external_link = $row['cPointerExternalLink'];
$linkObjs[] = $link;
}
$this->set('links', $linkObjs);
}

function getLinks() {
$db = Loader::db();
$sql = 'SELECT bmnl.*, p.cPointerExternalLink, p.cPointerExternalLinkNewWindow FROM btManualNavLinks bmnl LEFT JOIN Pages p ON p.cID = bmnl.linkToCID WHERE bID=' . intval($this->bID) . ' ORDER BY position';
return $db->getAll($sql);
}

view.php:

<li class="<?php echo $cssClasses; ?>">
<a href="<?php if ($link->external_link): echo $link->external_link; else: echo $link->url; endif; ?>" class="<?php echo $cssClasses; ?>" <?php if ($link->new_window): ?>target="_new"<?php endif; ?>>
<?php echo htmlentities($link->text, ENT_QUOTES, APP_CHARSET); ?>
</a>
</li>

It works with pretty URLs also.

concrete5 Environment Information

Browser User-Agent String

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You have not specified a license for this support ticket. You must have a valid license assigned to a support ticket to request a refund.