picocms / Pico

Pico is a stupidly simple, blazing fast, flat file CMS.
http://picocms.org/
MIT License
3.83k stars 617 forks source link

meta for modification date? #430

Closed in8sworld closed 6 years ago

in8sworld commented 6 years ago

This is a type: question but not sure how that gets assigned.

I have been using picocms for some time now and love it (thanks to all!) but I've come across a need (want) and can't figure out how to accomplish it. I'm thinking it would need to be a plugin with a function:

I write up my posts and date them using the date meta tag. The posts are ordered by date (I'm using a blog type functionality in a sub-folder) but I occassionally update a post at a later date. What I would like is to keep that posted date unchanged, and have a separate field to display the modified date (if different) of the file. I would prefer that it is the modified date of the file from the underlying OS through some function and not a manually updated date in the Yaml header (which would be simple enough to do). This way I wouldn't have to remember to update the modified date every time and I'm sometimes updating the file a bunch of times subsequent to publishing it.

Thanks again for all the work on this great project!

PhrozenByte commented 6 years ago

Try something like this (untested; written for Pico 2.0):

class PicoLastModificationPlugin extends AbstractPicoPlugin
{
    const API_VERSION = 2;

    public function onMetaParsed(array &$meta)
    {
        if (!isset($meta['modificationTime']) && file_exists($this->getRequestFile())) {
            $meta['modificationTime'] = filemtime($this->getRequestFile());
        }
    }

    public function onSinglePageLoaded(array &$pageData)
    {
        if ($pageData['id']) {
            $fileName = $this->getConfig('content_dir') . $pageData['id'] . $this->getConfig('content_ext');
            if (!isset($pageData['modificationTime']) && file_exists($fileName)) {
                $pageData['modificationTime'] = filemtime($fileName);
            }
        }
    }
}
in8sworld commented 6 years ago

Thanks so much! I was able to get it working in Pico v1 (update 200203: still working in Pico v2) with just the first part of your function (note the added date function to make the date non-Unix time) saving it as the filename PicoLastModificationPlugin.php in the plugins directory and by including {{ meta.modificationTime }} to my blog page twig file. I wasn't sure how I would use the second portion of your function.

class PicoLastModificationPlugin extends AbstractPicoPlugin
{
    const API_VERSION = 2;
    public function onMetaParsed(array &$meta)
    {
        if (!isset($meta['modificationTime']) && file_exists($this->getRequestFile())) {
            $meta['modificationTime'] = date ("F d, Y", filemtime($this->getRequestFile()));
        }
    }
}

I then added an if statement to that twig template so that the modification date line only shows up if I manually set a moddate field in the YAML header. This way I can choose if I want folks to know that I'm editing the post after the fact or not (helpful when I've got a lot of older posts to put up but don't want them all to appear like I edited them recently when all I did was move the files around)

{% if meta.moddate %}
Modified on {{ meta.modificationTime }}
{% else %}
{% endif %}
stale[bot] commented 6 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in two days if no further activity occurs. Thank you for your contributions! :+1: