lessons/v4/adding-custom-fields-to-a-page having issues with a varchar value not displaying.

Silverstripe Version:
SS4.3
Question:
Struggling to get $Author to display only displaying blank value even though the database is holding a value. Could anybody possibly tell me why I am not able to call this variable from the database?
Details of query:
I am new to SilverStripe and have been working through the lessons. I am on lesson 6 “Adding custom fields to a page”. I’ve updated the “ArticlePage” class to generate the fields in the database and use the “FieldList” API to add some new form inputs.
No issues displaying $Date and $Teaser but $Author isn’t working. I’ve checked the GitHub repository to compare my code but unfortunately even though the “Tutor” tells you to use $Author to replace static values he/she hasn’t gone as far as to do the same.

ArticlePage.php

<?php
namespace SilverStripe\Lessons;

use Page;
use SilverStripe\Forms\DateField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\TextField;

class ArticlePage extends Page
{
  private static $can_be_root = false;

  private static $db = [
    'Date' => 'Date',
    'Teaser' => 'Text',
    'Author' => 'Text',
  ];
  public function getCMSFields()
  {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Main', DateField::create('Date','Date of article'),'Content');
    $fields->addFieldToTab('Root.Main', TextareaField::create('Teaser')
      ->setDescription('This is the summary that appears on the article list page.')
      ,'Content');
    $fields->addFieldToTab('Root.Main', TextField::create('Author','Author of article'),'Content');

    return $fields;
  }
}

ArticlePage.ss

...//
			<!-- BEGIN MAIN CONTENT -->
			<div class="main col-sm-8">

				<h1 class="blog-title">$Title</h1>

				<div class="blog-main-image">
					<img src="http://placehold.it/765x362" alt="" />
					<div class="tag"><i class="fa fa-file-text"></i></div>
				</div>

				<div class="blog-bottom-info">
					<ul>
						<li><i class="fa fa-calendar"></i> $Date.Long </li>
						<li><i class="fa fa-comments-o"></i> 3 Comments</li>
						<li><i class="fa fa-tags"></i> Properties, Prices, best deals</li>
					</ul>

					<div id="post-author"><i class="fa fa-pencil"></i> By $Author</div>
				</div>

				<div class="post-content">
					<div class="highlight-bold">$Teaser</div>

					<div class="divider"></div>
//...

Had the same problem… Author value was persisting correctly in the CMS and DB, just resulting in a blank string when trying to use the template var.

Only way I was able to get it working was to not use $Author or $Publisher (note Yoda-Soda was using ‘Publiser’), so presuming they’re reserved in some way, even after implementing @James 's suggestions.

I ended up with:

private static $table_name = 'ArticlePage';

and, for my DB fields:

private static $db = [
   'Date' => 'Date',
    'ArticleAuthor' => 'Varchar(100)',
    'Teaser' => 'Text',
];

The code looks fine, although it might make more sense to use Varchar for Author rather than Text. Is it possible you just need to publish the page?

Thank you. Sorry the datatype Text was meant as a test and got snucked in when I copied it to this post.
I’ve definitely published the page.

In addition to @JonoM’s answer, just in case, add ?flush to the end of the URL when you view the page. Especially if your site is in dev mode, template caches sometimes get in the way.

Yeah I’ve been using ?flush and /dev/build on regular intervals.

I’ve tested my code by including another field to ArticlePage.php to “replace” Author with Publisher. This seemed to work. So I am drawing a conclusion that the field name Author is not allowed to be used. For my sanity sake if anybody has an idea of why this is happening I would be thankful.

private static $db = [
‘Date’ => ‘Date’,
‘Teaser’ => ‘Text’,
‘Author’ => ‘Varchar’,
‘Publiser’ => ‘Varchar’,
];
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab(‘Root.Main’, DateField::create(‘Date’,‘Date of article’), ‘Content’);
$fields->addFieldToTab(‘Root.Main’, TextareaField::create(‘Teaser’)
->setDescription(‘This is the summary that appears on the article list page.’),
‘Content’
);
$fields->addFieldToTab(‘Root.Main’, TextField::create(‘Author’,‘Author of article’),‘Content’);
$fields->addFieldToTab(‘Root.Main’, TextField::create(‘Publiser’,‘Publiser of article’),‘Content’);

return $fields;

}

I had the exact same issue and finally just gave up. Definitely something up with using ‘Author’ but I couldn’t get to the bottom of it.
Kind of difficult to “hop on the SilverStripe train” when the lessons don’t work as expected and there are thousands of pages of outdated documentation and articles out there. I’m questioning if SilverStripe is the way to go. I want to give it the benefit of the doubt, but struggling daily.

1 Like

SS 4.7
Same problem, changing Author to Published didn’t help
And secont problem - https://www.silverstripe.org/learn/lessons/v4/adding-custom-fields-to-a-page-1

String as DataType in not allowed

I had problems last year, when I tried to follow the tutorials, I found watching the v3 videos helped. See my blog for details: Setup a new SilverStripe 4 Project [ Pen y Fan ]

You can see a branch for each lesson on Github: GitHub - Pen-y-Fan/silverstripe-lessons: SilverStripeCMS Lessons

I hope this helps, once you get your head around SilerverStripe it is great.

Here’s two possibilities I can think of.

  1. To ensure it’s not a clashing namespacing issue. Try dev/building with:

private static $table_name = 'ArticlePage';

  1. I recall having issues with varchar DB types when not specifying the size. I’m not sure if that’s a PHP7 thing, SilverStripe or maybe just my memory playing up.

‘Author’ => ‘Varchar(100)’,

I can confirm this.
Happened to me too in the lessons.
Author was the culprit, although I still do not know why

And same again $Author not working in the templates for this lesson.

Seems like this is a bug…

I don’t believe it’s a bug as such… If I had to guess, I’d say it’s probably because the Versioned class (which will be applied to any subclass of Page) already has Author() as a method. So trying to get the value of Author is probably invoking the method in that class, rather than getting your page class property via the magic getter.

You could probably test this by adding the following to your page class:

public function getAuthor() 
{
  return $this->Author;
}

In theory, that would return the property from the class, overriding the method from Versioned… if not, possibly using Author() as the method name… I haven’t tested it!

In any case, you’re probably safer just steering away from using Author as a property to avoid the possible collision.

1 Like

The method

    public function Author() {
      return $this->Author;
    }

is the one which works. Using $Publisher causes a similar problem since Versioned also contains a Publisher() method

Thanks DorsetDigital that makes sense: I was looking at one of the CMS templates which is looping through $Versions and uses an $Author variable.

Looking in the API docs, it does list both Author() and Publisher() for the class.
https://api.silverstripe.org/4/SilverStripe/Versioned/Versioned.html