Feed Media
Introduction
A model's toFeed() method describes what is stored. Its static feedMedia() method resolves links and media when a feed is retrieved:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedMedia;
class Photo extends Model implements Feedable
{
use InteractsWithFeed;
public static function feedMedia(FeedContext $context): ?FeedMedia
{
return FeedMedia::make()->url(route('photos.show', $context->routeKey()));
}
}<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedMedia;
class Photo extends Model implements Feedable
{
use InteractsWithFeed;
public static function feedMedia(FeedContext $context): ?FeedMedia
{
return FeedMedia::make(
url: route('photos.show', $context->routeKey()),
);
}
}The method receives a FeedContext with the snapshot's label, route key and data, and returns a FeedMedia or null. Resolving URLs here lets a route or thumbnail change without rewriting stored picture bodies. A composed feed calls feedMedia() too, each time it is read.
Using a Closure
With InteractsWithFeed, you may register a closure in booted() instead:
use Storyfeed\FeedContext;
use Storyfeed\FeedMedia;
static::feedMediaUsing(
fn (FeedContext $context, FeedMedia $media): FeedMedia => $media
->url(route('photos.show', $context->routeKey())),
);A model's own feedMedia() method takes precedence over the registered closure. A closure may also return a URL string or null. Without either resolver, the trait returns null.
Linking to the Model
Use url() for the destination of the entity's name in the headline. To say more about the link, pass a FeedLink to link(). Its modal() method hints that the renderer should open the link in place, and attributes() adds link attributes:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedLink;
use Storyfeed\FeedMedia;
class Photo extends Model implements Feedable
{
use InteractsWithFeed;
public static function feedMedia(FeedContext $context): ?FeedMedia
{
return FeedMedia::make()
->link(
FeedLink::to(route('photos.show', $context->routeKey()))
->modal()
->attributes(['aria-label' => 'Open photo'])
);
}
}<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedLink;
use Storyfeed\FeedMedia;
class Photo extends Model implements Feedable
{
use InteractsWithFeed;
public static function feedMedia(FeedContext $context): ?FeedMedia
{
return FeedMedia::make(
link: FeedLink::to(route('photos.show', $context->routeKey()))
->modal()
->attributes(['aria-label' => 'Open photo']),
);
}
}A link does not show a picture; see Showing Pictures. A resolver can also choose a different link for each feed.
Opening Links in a Modal
modal() sets the entity's link.modal to true; the URL stays an ordinary destination. url($href) is the short form of link(FeedLink::to($href)).
Linking Files
A Document model describes its PDF with a FileAttachment body in toFeed(). The body stores the file's name, size and media type. Use files() in feedMedia() to supply the link that opens the document and the file to download:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Body\FileAttachment;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedMedia;
use Storyfeed\FeedResource;
class Document extends Model implements Feedable
{
use InteractsWithFeed;
public function toFeed(): FeedEntity
{
return FeedEntity::make()
->label($this->name)
->body(
FileAttachment::make()
->size($this->bytes)
->mediaType('application/pdf')
->name($this->name)
);
}
public static function feedMedia(FeedContext $context): ?FeedMedia
{
return FeedMedia::make()
->url(route('documents.show', $context->routeKey()))
->files(
FeedResource::make()
->href(route('documents.download', $context->routeKey()))
->name($context->label())
->mediaType('application/pdf')
);
}
}<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Body\FileAttachment;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedMedia;
use Storyfeed\FeedResource;
class Document extends Model implements Feedable
{
use InteractsWithFeed;
public function toFeed(): FeedEntity
{
return FeedEntity::make(
label: $this->name,
body: FileAttachment::make(
size: $this->bytes,
mediaType: 'application/pdf',
name: $this->name,
),
);
}
public static function feedMedia(FeedContext $context): ?FeedMedia
{
return FeedMedia::make(
url: route('documents.show', $context->routeKey()),
files: [
FeedResource::make(
href: route('documents.download', $context->routeKey()),
mediaType: 'application/pdf',
name: $context->label(),
),
],
);
}
}Signed Agreement.pdf
The entity link points to the document. The files() list adds resources to entity.media.files; it does not create a body. Each call appends resources. Activity Streams output carries them in its attachment property.
Showing Pictures
feedMedia() resolves pictures into the entity's media. To show one in a body, name its slot: declare an Image body in toFeed() with the model's feedMediaPreview(), then supply the preview from feedMedia():
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Body\Image;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedImage;
use Storyfeed\FeedMedia;
class Photo extends Model implements Feedable
{
use InteractsWithFeed;
public function toFeed(): FeedEntity
{
return FeedEntity::make()
->label($this->name)
->body(
Image::make($this->feedMediaPreview())
->caption($this->subject)
->alt($this->description)
);
}
public static function feedMedia(FeedContext $context): ?FeedMedia
{
return FeedMedia::make()
->url(route('photos.show', $context->routeKey()))
->preview(FeedImage::make()->src(route('photos.thumbnail', $context->routeKey())));
}
}<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Body\Image;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedImage;
use Storyfeed\FeedMedia;
class Photo extends Model implements Feedable
{
use InteractsWithFeed;
public function toFeed(): FeedEntity
{
return FeedEntity::make(
label: $this->name,
body: Image::make(
image: $this->feedMediaPreview(),
caption: $this->subject,
alt: $this->description,
),
);
}
public static function feedMedia(FeedContext $context): ?FeedMedia
{
return FeedMedia::make(
url: route('photos.show', $context->routeKey()),
preview: FeedImage::make(
src: route('photos.thumbnail', $context->routeKey()),
),
);
}
}
The caption belongs to the body. The body stores the slot's name, and the picture is resolved each time the feed is read, so a changed thumbnail shows on every row. Image::make() with no picture names the preview slot. MediaObject::image() accepts a slot too, or a FeedImage, for a picture alongside its text.
The three slots retain their Activity Streams 2.0 names:
| Slot | Job | Model Method |
|---|---|---|
icon | a small representation that identifies the entity, such as an avatar or logo | feedMediaIcon() |
preview | a preview of the entity, such as a photograph's thumbnail or a page's link-card image | feedMediaPreview() |
image | a larger picture of a non-image entity, such as a menu item | feedMediaImage() |
Each method is shorthand for getFeedMedia(), such as getFeedMedia('icon').
The AS2 serializer emits these as icon, preview, and image. None is the entity's link.
To retain image dimensions or a media type with the entity, store snapshot data in toFeed().
Adding Your Own Slots
For a picture the three slots do not describe, such as a chart of a menu item's orders, name a slot of your own with slot(), and show it in a body with getFeedMedia():
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Body\Image;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedImage;
use Storyfeed\FeedMedia;
class MenuItem extends Model implements Feedable
{
use InteractsWithFeed;
public function toFeed(): FeedEntity
{
return FeedEntity::make()
->label($this->name)
->body(Image::make($this->getFeedMedia('sparkline')));
}
public static function feedMedia(FeedContext $context): ?FeedMedia
{
return FeedMedia::make()
->url(route('menu.show', $context->routeKey()))
->slot('sparkline', FeedImage::make()
->src(route('menu.sparkline', $context->routeKey()))
->width(120)
->height(24)
->alt('Orders this week'));
}
}<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Body\Image;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedImage;
use Storyfeed\FeedMedia;
class MenuItem extends Model implements Feedable
{
use InteractsWithFeed;
public function toFeed(): FeedEntity
{
return FeedEntity::make(
label: $this->name,
body: Image::make(image: $this->getFeedMedia('sparkline')),
);
}
public static function feedMedia(FeedContext $context): ?FeedMedia
{
return FeedMedia::make(
url: route('menu.show', $context->routeKey()),
)->slot('sparkline', FeedImage::make(
src: route('menu.sparkline', $context->routeKey()),
width: 120,
height: 24,
alt: 'Orders this week',
));
}
}A slot name is letters, digits, _ and -, starting with a letter. The built-in names icon, preview and image have their own methods. Custom slots appear under media.slots in the payload and are left out of Activity Streams output. Pass a generated SVG as a data: URL in src.
Giving an Actor an Avatar
Any feedable model can be an actor. Its icon slot supplies the avatar when it appears in that role, even if it is usually the object of an activity:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedImage;
use Storyfeed\FeedMedia;
class Photo extends Model implements Feedable
{
use InteractsWithFeed;
public static function feedMedia(FeedContext $context): ?FeedMedia
{
return FeedMedia::make()
->url(route('photos.show', $context->routeKey()))
->icon(FeedImage::make()->src(route('photos.thumbnail', $context->routeKey())));
}
}<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedImage;
use Storyfeed\FeedMedia;
class Photo extends Model implements Feedable
{
use InteractsWithFeed;
public static function feedMedia(FeedContext $context): ?FeedMedia
{
return FeedMedia::make(
url: route('photos.show', $context->routeKey()),
icon: FeedImage::make(
src: route('photos.thumbnail', $context->routeKey()),
),
);
}
}
An avatar needs no Image body.
Showing Initials Instead of a Picture
A model without a picture, such as a team or a project, may give initials and a disc colour instead. Store both values in toFeed() to read them from $context->data():
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedMedia;
class Team extends Model implements Feedable
{
use InteractsWithFeed;
public function toFeed(): FeedEntity
{
return FeedEntity::make()
->label($this->name)
->data(['initials' => $this->initials, 'color' => $this->color]);
}
public static function feedMedia(FeedContext $context): ?FeedMedia
{
return FeedMedia::make()
->url(route('teams.show', $context->routeKey()))
->initials($context->data('initials'))
->color($context->data('color'));
}
}<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedMedia;
class Team extends Model implements Feedable
{
use InteractsWithFeed;
public function toFeed(): FeedEntity
{
return FeedEntity::make(
label: $this->name,
data: ['initials' => $this->initials, 'color' => $this->color],
);
}
public static function feedMedia(FeedContext $context): ?FeedMedia
{
return FeedMedia::make(
url: route('teams.show', $context->routeKey()),
initials: $context->data('initials'),
color: $context->data('color'),
);
}
}Work out what the Russian message on the tape is saying.
The colour is a hex value, such as #438d98. Without them, Storyfeed derives initials from the label and a colour from the type and key. An entity with an icon gets neither. See Storing Snapshot Data.
See the Feedable API for the complete method list.
For bodies built from current model values, see Resolving Bodies When Retrieved. Link and image resolvers can share the same FeedMedia with those bodies.