Null / undefined / unable to show dates

Hi, I’m working off the starterkit and renamed “Date” to “Published” but it’s been really frustrating and keeps throwing errors when I try to get it to display a basic date in ‘01.12.2021’ format…

Post yml (Previously ‘note’)

title: Post

num: date

icon: 📖

status:
  draft:
    label: Draft
    text: The note is still in draft mode. It can only be seen by editors with panel access.
  unlisted:
    label: In Review
    text: The note is online and can be visited with the direct URL. The team must still give the final go to publish it.
  listed:
    label: Published
    text: The note is online and listed in the blog

columns:
  - width: 2/3
    fields:
      standfirst:
        type: textarea
        buttons: false
      text:
        type: blocks

  - width: 1/3
    sections:
      meta:
        type: fields
        fields:
          cover: fields/cover
          published:
            type: date
            display: DD.MM.YYYY
            time: true
            default: now
          author:
            type: users
          tags: true
      files:
        type: files
        template: blocks/image

Template

<?php if ($page->published() !== ''): ?>
  <time class="note-date" datetime="<?= $page->published('c') ?>">Published on <?= $page->published->toDate()  ?></time>
<?php endif ?>

Just trying to get the date to show up? I don’t really understand cause it shows up on project pages and ostensibly it all looks the same…

Not sure if this is related but I also noticed a random “meta.yml” file recently? Don’t believe I made this or installed any plugins so no idea where it came from! or where to put it… it’s just living in “fields” blueprint for now

type:           group
fields:

    seotitle:
      label:    SEO Title
      type:     text
      icon:     font
      width:    1/2
      required: true
    seotags:
      label:    Keywords
      type:     tags
      width:    1/2
    seometa:
      label:    Meta Description
      type:     textarea
      size:     small
      required: true

You have two errors:

  • Parentheses missing after the second published
  • You need to pass the format to the toDate() field method in the first instance, not to published()
  <time class="note-date" datetime="<?= $page->published()->toDate('c') ?>">Published on <?= $page->published()->toDate()  ?></time>

Note that without a format the second instance will only return a timestamp

Thank you!

I changed the code to what you suggested but still get the error: Call to a member function toDate() on null even though there’s a date in the published field (Published: 2019-06-25 00:00:00).

And the error is really thrown on that line?

Yeah! It’s weird…

Yml for the post in question:

Title: A night in the forest

----

Text: [{"content":{"text":"<p>Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar.<\/p><p>The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn\u2019t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of the Italic Mountains, she had a last view back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek, then she continued her way. On her way she met a copy.<\/p><p>The copy warned the Little Blind Text, that where it came from it would have been rewritten a thousand times and everything that was left from its origin would be the word \"and\" and the Little Blind Text should turn around and return to its own, safe country. But nothing the copy said could convince her and so it didn\u2019t take long until a few insidious Copy Writers ambushed her, made her drunk with Longe and Parole and dragged her into their agency, where they abused her for their projects again and again. And if she hasn\u2019t been rewritten, then they are still using her. Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean.<\/p><p>A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn\u2019t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of the Italic Mountains, she had a last view back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek, then she continued her way. On her way she met a copy. The copy warned the Little Blind Text, that where it came from it would have been rewritten a thousand times and everything that was left from its origin would be the word \"and\" and the Little Blind Text should turn around and return to its own, safe country. But nothing the copy said could convince her and so it didn\u2019t take long until a few insidious Copy Writers ambushed her, made her drunk<\/p>"},"id":"_nEOMzN6tX","isHidden":false,"type":"text"}]

----

Published: 2019-06-25 00:00:00

----

Author:

----

Tags: forest, trees

----

Subheading:

----

Cover:

[Edit: Just noticed this code works on the Projects page (ostensibly the same code) but not the writing/notes page!]

Are you also using the original NotePage model and have renamed it to PostPage maybe? Because that model has a published method that already calls the toDate() method internally…

In that case, you would have to pass the format to published() rather than calling toDate().

Ahhh yes… I am :sweat_smile: That makes sense, thank you! I will try that or just rename the field to “Date published” instead then…

But I also can’t get my “check if date exists” check to work… It always prints the “Published on” even if there isn’t a date in there? <?php if ($page->datepublished()->toDate() !== ''): ?>

$page->something() will always return a field object no matter if the field exists or is empty, so checking if it returns am empty string doesn’t make sense. Use the isEmpty()/isNotEmpty() field methods instead.

Ah amazing thank you very much! All functioning now : )

1 Like