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>
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 |
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.