Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Tutorial 2: New Fields in Page Types

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

Page: 1
Go to End
Author Topic: 1425 Views
  • SmartRoss
    Avatar
    Community Member
    13 Posts

    Tutorial 2: New Fields in Page Types Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Tutorial 2: New Fields in Page Types Link to this post

    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;
    }

  • SmartRoss
    Avatar
    Community Member
    13 Posts

    Re: Tutorial 2: New Fields in Page Types Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Tutorial 2: New Fields in Page Types Link to this post

    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

  • SmartRoss
    Avatar
    Community Member
    13 Posts

    Re: Tutorial 2: New Fields in Page Types Link to this post

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

    1425 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.