Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Tutorial 2: New Fields in Page Types


Go to End


5 Posts   2375 Views

Avatar
SmartRoss

Community Member, 13 Posts

26 September 2010 at 3:25am

I couldn't find these two questions asked already, so here it goes:

First question: I'm following the second tutorial on SilverStripe, and have reached the section on adding database fields for specific Page Types - ArticlePage in this case. The examples in the tutorial use the fields $Date and $Author. Following the same pattern, I added my own field - $Level. While this $Level field appears in the CMS Content->Main tab for the relevant page, I cannot make the contents of the field print to the screen in my website - I just get a blank space. This is my ArticlePage.php (the comments relate to my second question):

<?php
class ArticlePage extends Page {
   static $db = array(
   		'Date' => 'Date',
   		'Author' => 'Text',
		'Level' => 'Text'
   );
   static $has_one = array(
   );
    /*$datefield = new DateField('Date');
	$datefield->setConfig('showcalendar', true);
	$datefield->setConfig('showdropdown', true);
	$datefield->setConfig('dateformat', 'dd/MM/YYYY');*/
   
   function getCMSFields() {
      $fields = parent::getCMSFields();
 
      //$fields->addFieldToTab('Root.Content.Main', $datefield, 'Content');
	  $fields->addFieldToTab('Root.Content.Main', new DateField('Date'), 'Content');
      $fields->addFieldToTab('Root.Content.Main', new TextField('Author'), 'Content');
	  $fields->addFieldToTab('Root.Content.Main', new TextField('Level'), 'Content');
 
      return $fields;
   }
}
 
class ArticlePage_Controller extends Page_Controller {
 
}
 
?>

and here is my Layout/ArticlePage.ss:

<% if Menu(2) %>
  <ul id="Menu2">
    <% control Menu(2) %>
      <li class="$LinkingMode"><a href="$Link" title="Go to the $Title page">$MenuTitle</a></li>
    <% end_control %>
  </ul>
<% end_if %>
 
<div id="Content" class="typography">
  <% if Level(2) %>
    <div class="breadcrumbs">
      $Breadcrumbs
    </div>
  <% end_if %>
 
  <h1>$Title</h1>
  $Content
  <div class="newsDetails">
    $Date.Nice by $Author for $Level
  </div>
</div>

My second question: the commented out sections in ArticlePage.php are what a comment (by 'Frank') in Tutorial 2 said to put in to allow the JavaScript Date Picker to be used to select a date. However, when I run this code, having removed the original date code, I get this error:

Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in /home/irevise/public_html/mysite/code/ArticlePage.php on line 13

Line 13 is $datefield = new DateField('Date');.

However I shift code about, I always get this error.

Can anybody shed any light on either of these two issues?

Avatar
Willr

Forum Moderator, 5523 Posts

26 September 2010 at 10:23am

You might want to avoid using Level since that could be conflicting with the built in $Level indicator which determines what level you are viewing.

The issue with the date field code is that it is not contained in the cms fields function. It should look like this

function getCMSFields() { 
$fields = parent::getCMSFields();

$fields->addFieldToTab('Root.Content.Main', $date = new DateField('Date'), 'Content'); 
$date->setConfig('showcalendar', true); 
$date->setConfig('dateformat', 'dd/MM/YYYY');

....
return $fields; 
} 

Avatar
SmartRoss

Community Member, 13 Posts

27 September 2010 at 7:28pm

Thanks! I've just changed that, but now I'm trying to troubleshoot SilverStripe's 'The website server was unable to respond to your request' error, so I don't know if it worked yet...

Avatar
Willr

Forum Moderator, 5523 Posts

27 September 2010 at 7:32pm

Thanks! I've just changed that, but now I'm trying to troubleshoot SilverStripe's 'The website server was unable to respond to your request' error, so I don't know if it worked yet...

That is the default error message for production sites. Put your site into devmode and you will get errors outputted to the browser.

http://doc.silverstripe.org/debugging#dev_mode

Avatar
SmartRoss

Community Member, 13 Posts

28 September 2010 at 4:22am

Edited: 28/09/2010 4:23am

It works! Thanks for the help. The $Level field must have caused some confusion. I missed the last reply, but flushing everything seemed to fix it...