bnomei/kirby3-feed

Generate a RSS/JSON-Feed from a Pages-Collection

Installs: 279

Dependents: 0

Suggesters: 2

Stars: 16

Watchers: 0

Forks: 2

Open Issues: 0

Type:kirby-plugin

1.3.5 2019-11-06 11:02 UTC

README

Release Stars Downloads Issues Build Status Coverage Status Maintainability Demo Gitter Twitter

Generate a RSS/JSON-Feed from a Pages-Collection.

Commercial Usage

This plugin is free but if you use it in a commercial project please consider to

Installation

  • unzip master.zip as folder site/plugins/kirby3-feed or
  • git submodule add https://github.com/bnomei/kirby3-feed.git site/plugins/kirby3-feed or
  • composer require bnomei/kirby3-feed

Usage

You can use this in a template for a dedicated feed page, in a template controller or a route.

<?php
$options = [
    'title'       => 'Latest articles',
    'description' => 'Read the latest news about our company',
    'link'        => 'blog'
];
echo page('blog')->children()->visible()->flip()->limit(10)->feed($options);

options array defaults

If you use these defaults you need to provide the fields date (type: date) and text (type: text).

[
    'url' => site()->url(),
    'feedurl' => site()->url() . '/feed/',
    'title' => 'Feed',
    'description' => '',
    'link' => site()->url(),
    'urlfield' => 'url',
    'titlefield' => 'title',
    'datefield' => 'date',
    'textfield' => 'text',
    'modified' => time(),
    'snippet' => 'feed/rss', // 'feed/json'
    'mime' => null,
    'sort' => true,
]

virtual page in site/config.php

return [
    'routes' => [
        [
            'pattern' => 'feed',
            'method' => 'GET',
            'action'  => function () {
                $options = [
                    'title'       => 'Latest articles',
                    'description' => 'Read the latest news about our company',
                    'link'        => 'blog'
                ];
                $feed = page('blog')->children()->visible()->flip()->limit(10)->feed($options);
                return $feed;
            }
        ]
    ]
];

HTML head element

rss xml

<link rel="alternate" type="application/rss+xml" title="Latest articles" href="<?= site()->url() ?>/feed"/>

and/or rss json

<link rel="alternate" type="application/json" title="Latest articles" href="<?= site()->url() ?>/feed"/>

TIP: Having multiple feed links is still valid html. So you can have both rss and json if you want and setup the routes properly.

Sorting

The Plugin applies a default sorting for the pages by date/modified in descending order (newest first).

  • If you do not want this you have to set the datefield setting to another Field name or PageMethod name.
  • If you want to disable sorting by the plugin and add your own you can set the option sort to false.

Pitfalls when presorting by date and limit

Using sortBy('date', 'desc') will not yield expected results! In K3 sorting by date needs a callback.

$feed = page('blog')->children()->visible()->sortBy(function ($page) {
 return $page->date()->toDate();
}, 'desc')->limit(10)->feed($options);

Settings

bnomei.feed. Default Description
mime null to autodetect json or rss-xml otherwise enforce output with a certain mime type
expires 60*24*7 in minutes

The plugin will automatically devalidate the cache if any of the Page-Objects were modified.

Cache

If the global debug option is set to true the plugin will automatically flush its own cache and not write to the cache.

Disclaimer

This plugin is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please create a new issue.

License

MIT

It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech.

Credits

based on K2 versions of