Add image from Files to description in RSS (using kirby3-feed)

I’ve added Bruno Meilick’s kirby3-feed plugin to my webcomic site using the Virtual Page configuration option, and it works great. I’m pulling the feed items directly from a collection that contains a filtered set of content from my site, and the feed is showing the item titles which, when clicked, redirect correctly to the relevant page on the site.

Here’s what the configuration file looks like:

<?php

return [
    'debug' => true,
    'routes' => [
        [
            'pattern' => 'feed',
            'method' => 'GET',
            'action'  => function () {
                $options = [
                    'title'       => 'Latest Comics',
                    'description' => 'Read the latest Skeleton Crew comics',
                    'link'        => 'comic',
                    'description'   => '', // comic image should go here
                ];
            
                $feed = collection("all-comics")->feed($options);
                return $feed;
            }
        ]
    ]
];

Each item in the feed is a comic strip, so I’d like to use the comic image as the description for each RSS entry. This is how I access the images currently:

$page()->files()->findBy('template', 'main-image');

Does anyone know how I might achieve that in the format that the virtual page configuration can parse?

hi @Mike_Armstrong. the rss item description is generated based on the textfield option.

for virtual pages you can extend your model to provide the text() method or create a new one like rssDescWithImg() and set 'textfield'=>'rssDescWithImg' in the config.

maybe the method must return a field-object so the rss snippet can invoke ->kirbytext() on it. but i never tested this.

Thanks for the advice. I’ve rolled my own solution for now, but will try and get this working at a later date.

@bnomei This is the best post related to what I’m looking to do (alter text field output in the feed), so I’m adding this here:

With three main content areas in my site (with children), how do I have my feed contain all of my content without restructuring my site?

controllers/site.php:

return function($site, $page) {
    $contentChild1 = page('child1')->children()->listed();
    $contentChild2 = page('child2')->children()->listed();
    $contentChild3 = page('child3')->children()->listed();

    $allcontent = new Pages($contentChild1, $contentChild2, $contentChild3) 

    return [
    'contentChild1' => $contentChild1,
    'contentChild2' => $contentChild1,
    'contentChild3' => $contentChild1,
    'allcontent' => $allcontent
    ];
  1. When altering the feed options in the routes for the Feed plugin, I’d like to combine multiple page fields into one for the feed (ie.: I use the first image that is not in text field, author, categories, etc.). I gather you can do this using a snippet, but I need a more noob explanation on how this is possible.

  2. If I try to set the feed to get the array coming from my allcontent controller I’m not sure I can apply the options to it to get all those field outputs. I hope I’m making sense.

Could you help a noob out?

I think a first step should be to create a custom collection for $allcontent. That way, you can easily pass this collection to the feed options.

Please rename your variables. Dashes are not allowed in PHP variable names. In the Kirby context, we use camelCase for readability, but you might as well use underscores to separate words/parts.

As regards combining multiple fields into one, you can use a custom page method or a page model that returns what you need.


Oh, thank you! Was just browsing other feed configuration solutions and noticed someone else was referencing Collections, which I haven’t made yet, so that might go a long way.

(Ah, those were dummy variables, I’ll fix them before someone else assumes they’re good. Thank you.)

Methods and models are beyond my understanding so far, but I will put those on my list to learn.
Thank you.

See the new screencast regarding collections:

Methods and models are no “Hexenwerk” either, nothing to be afraid of, even if @thewebprojects wanted to hide from them as well: Creating page that you never intend to navigate to - #13 by thewebprojects

2 Likes

I’m trying to do this very same thing but am hitting a wall. I’m unsure how to combine the default text with the $page->file() in a format that the feed plugin wants (a field object, I guess?).

My PHP is super rusty. Am I missing something really basic?

class EntryPage extends Page {
  public function text() {
    $formattedImage = kirbytext('(image: ' . $this->file()->filename() . ' width: 400 srcset: [200, 400, 600])');
    $unalteredText = parent::text();

    // Combine $formattedImage and $unalteredText somehow.
    // $textWithImage = ??

    return $textWithImage;
  }
}

What I’d do:

public function rssDesc()
{
    $image = Html::img($this->file()->url());
    $text    = $this->text()->kt();

    return $image . $text;
}

This can of course be refined by passing an array of attributes to the Html::img() method or you can wrap it in inside a figure tag or whatever.

Very nice, I like how clean that is! Thank you!

Unfortunately, it’s giving me the error:

Call to a member function kirbytext() on string

Is there a way to wrap that output in a “field object” or whatever the plugin is expecting?

Yes, you can return a field object

public function rssDesc(): Field
{
    $image = Html::img($this->file()->url());
    $text = $this->text()->toBlocks();

    return new Field($this, 'rssDesc', $image . $text);
}
1 Like

Thank you! I just figured that out right as you posted that. :sweat_smile:

This forum is such an awesome resource. I really appreciate your patience and explanations!

Note that you should check if an image exist to prevent errors.

$image = $this->file() ? Html::img($image->url()) : '';
1 Like