Hello all,
I want to create a one-page website. On this one-page website, I went about writing a custom dataobject to generate a "mastery/specialty page", every time a client wanted to talk about the different specialties they have. Unfortunately, even though the page displays correctly, I can't seem to get the navigation to work.
I was thinking about generating two cases:
1.) If the dataobject exists on the current page, it returns them a local URL (so that the user can smooth scroll to the lower part of the page).
2.) Else, it returns them a full URL with the local link referenced (and automatically get them there).
As it stands, I've been able to get case one to sorta work - my controller dynamically generates "$Title.URL" within a <% Masteries %> loop, and this generates links to each mastery. Except, I can't get this URL to be generated even on other page types - when my MainNav.ss calls the <% Masteries %> loop on another page type, it seems to return null.
So, what's the best way of fixing this bug? Here's some code:
HomeLoopHolder.php (some)
public function getCMSFields()
{
// This controls the three subpages, which are dataobjects belonging to Mastery
$fields->addFieldToTab('Root.Masteries', GridField::create(
'Masteries',
'Here are a list of different specialities and masteries',
$this->Masteries(),
GridFieldConfig_RecordEditor::create()
));
}
class HomeLoopHolder_Controller extends Page_Controller
{
private static $allowed_actions = array (
'show',
'showAbout'
);
public function show(SS_HTTPRequest $request)
{
/*
This creates a variable Mastery, by fetching the page ID.
$mastery = Mastery::get()->byID($request->param('ID'));
*/
$mastery = Mastery::get()->byTitle($request->param('Title'));
if(!$mastery)
{
return $this->httpError(404, 'That mastery could not be found.');
}
return array(
'Mastery' => $mastery,
'Title' => $mastery->Title
);
}
}
Mastery.php
class Mastery extends DataObject
{
private static $db = array (
'Title' => 'Varchar',
'Description' => 'HTMLText'
);
private static $has_one = array
(
'Photo' => 'Image',
'HomeLoopHolder' => 'HomeLoopHolder'
);
public function getCMSFields()
{
$fields = FieldList::create
(
TextField::create('Title'),
HTMLEditorField::create('Description'),
$uploader = UploadField::create('Photo')
);
$uploader->setFolderName('mastery-photos');
$uploader->getValidator()->setAllowedExtensions(array('png','jpeg','jpg','gif','svg'));
return $fields;
}
public function Link()
{
// Returns to HomeLoopHolder a link with the title of the mastery.
return $this->Title;
}
public function LinkingMode()
{
// Returns the currently actively controller by getting the page Title. If the Title matches the Mastery Title, return current. Otherwise return link.
return Controller::curr()->getRequest()->param('Title') == $this->Title ? 'current' : 'link';
}
}
MainNav.ss
<!-- Navigation -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<a href="#top"></a>
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header page-scroll">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-loops="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="$AbsoluteBaseURL">$SiteConfig.Title</a>
</div>
<!-- /.navbar-header page-scroll -->
<div id="navbar" class="navbar-collapse collapse">
<!-- Collect the nav links, forms, and other content for toggling -->
<ul class="nav navbar-nav navbar-right">
<% loop Menu(1) %>
<% if Children %>
<li class="dropdown">
<a href="$Link" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">$MenuTitle<span class="caret"></span></a>
<ul class="dropdown-menu">
<li class="$LinkingMode"><a class="firstlevel" href="$Link" title="$Title"><strong>$MenuTitle Overview</strong></a>
<% loop Children %>
<li class="$LinkingMode"><a class="secondlevel" href="$Link" title="$Title">$MenuTitle</a>
</li>
<% end_loop %>
</ul>
</li>
<% else %>
<li class="$LinkingMode"><a class="firstlevel" href="$Link" title="$Title">$MenuTitle</a>
<% end_if %>
</li>
<% end_loop %>
<% loop $Masteries %>
<li class="$LinkingMode">
<a class="$LinkingMode" href="#$Link">$Title</a>
</li>
<% end_loop %>
</ul>
<!-- /.nav navbar-nav navbar-right -->
</div>
<!-- /.navbar-collapse collapse -->
</div>
<!-- /.container-fluid -->
</nav>