Processors and Modifiers
Processors and Modifiers allow you to control how Feedamic handles your Entry's data, and how it outputs it to the Atom and RSS feed templates.
A Processor allows you to modify how a specific field in your Entry gets processed.
A Modifier allows you to modify the output of one of FeedamicEntry's core methods - title
, summary
, content
, image
.
This means that you could have your "Page Builder" field processed in one way that will help populate the content and summary, but then be modified for the summary
to only show the first 100 characters.
Similar, but different
Adding a Processor or Modifier is very similar.
A Processor requires a fieldHandle
and a processor
Closure.
A Modifier requires a feedamicEntryProperty
and a modifier
Closure.
The Closure for each expects a Feedamic Entry, of type MityDigital\Feedamic\Abstracts\AbstractFeedamicEntry
. This means that even if you use different Entry models, your one Modifier can work for any of them.
Processors and Modifiers both optionally support when
and feeds
properties - these behave the same way in each.
Adding a Modifier or Processor
In a service provider, such as App\Providers\AppServiceProvider
, simply tell Feedamic to modify a specific part of the Feedamic Entry, or process a field handle.
You could also make your own FeedamicServiceProvider
if you wanted.
1use MityDigital\Feedamic\Abstracts\AbstractFeedamicEntry; 2use MityDigital\Feedamic\Facades\Feedamic; 3use Statamic\Fields\Value; 4 5Feedamic::modify( 6 feedamicEntryProperty: 'summary', 7 modifier: function (AbstractFeedamicEntry $feedamicEntry, null|string|Value $value) { 8 // do something to the $value variable 9 }10);
When adding a modifier, you need:
a
feedamicEntryProperty
, one oftitle
,summary
,content
orimage
, anda
modifier
Closure, which accepts a Feedamic Entry and the field's value
Within your modifier Closure, you're free to do whatever you would like - a simple transformation, rendering a custom view, whatever you want to include in your feed.
For a Processor:
1use MityDigital\Feedamic\Abstracts\AbstractFeedamicEntry; 2use MityDigital\Feedamic\Facades\Feedamic; 3use Statamic\Fields\Value; 4 5Feedamic::processor( 6 fieldHandle: 'page_builder', 7 processor: function (AbstractFeedamicEntry $feedamicEntry, null|string|Value $value) { 8 // do something to the $value variable 9 }10);
When adding a Processor, you need:
a
fieldHandle
, a handle from one of your Blueprint fieldsa
modifier
Closure, which accepts a Feedamic Entry and the field's value
Optional parameters
when
The when
param is optional, and is another Closure that allows you to programmatically add additional logic to whether the Modifier should be applied.
Like the modifier
param, you will have access to a Feedamic Entry and the value.
feeds
The feeds
param is optional, and is either null or an array of Feed handles.
This is a quick conditional test to apply the Modifier only to the Entries within the given Feed handles.
Removing a Modifier or Processor
Pass a Feedamic Entry Property to the facade to remove a Modifier from the list.
This example will remove all Modifiers for the property content
.
1use MityDigital\Feedamic\Facades\Feedamic;2 3Feedamic::removeModifier('content');
Removing a Modifier will remove all Modifiers for that property.
A Processor can be removed with the removeProcessor
method.
This example will remove all Processors for the field handle page_builder
.
1use MityDigital\Feedamic\Facades\Feedamic;2 3Feedamic::removeProcessor('page_builder');
Removing a Processor will remove all Processors for that field handle.
Examples
Get the first 100 characters
This is a applying a simple limit to the "summary" of the FeedamicEntry summary
:
1use Illuminate\Support\Str; 2use MityDigital\Feedamic\Abstracts\AbstractFeedamicEntry; 3use MityDigital\Feedamic\Facades\Feedamic; 4use Statamic\Fields\Value; 5 6Feedamic::modify( 7 feedamicEntryProperty: 'summary', 8 modifier: function (AbstractFeedamicEntry $feedamicEntry, null|string|Value $value) { 9 return Str::limit($value, 100, preserveWords: true);10 }11);
Use the content
in the summary
modifier
If you have a Page Builder that gets used as part of your content generation, and you want to use that content as your summary, you can call the Feedamic Entry's content method from within the summary modifier, to then limit by 100 characters.
1use Illuminate\Support\Str; 2use MityDigital\Feedamic\Abstracts\AbstractFeedamicEntry; 3use MityDigital\Feedamic\Facades\Feedamic; 4use Statamic\Fields\Value; 5 6Feedamic::modify( 7 feedamicEntryProperty: 'summary', 8 modifier: function (AbstractFeedamicEntry $feedamicEntry, null|string|Value $value) { 9 return Str::limit(strip_tags($feedamicEntry->content()), 100, preserveWords: true);10 }11);
With this example, your content
only needs to be rendered once, which can be used for the content
method of the Feedamic Entry, and re-used in the modifier for the summary
.
Conditional
When you pass a when
closure you need to return a true or false response. This allows you to do further tests based on your setup to determine whether the Modifier or Processor should be applied.
In this example, we only run the Processor if the $feedamicEntry
is a custom Model we created:
1use MityDigital\Feedamic\Abstracts\AbstractFeedamicEntry; 2use MityDigital\Feedamic\Facades\Feedamic; 3use Statamic\Fields\Value; 4 5Feedamic::processor( 6 fieldHandle: 'page_builder', 7 processor: function (AbstractFeedamicEntry $feedamicEntry, null|string|Value $value) { 8 // do something to the $value variable 9 },10 when: function (AbstractFeedamicEntry $feedamicEntry, mixed $value) {11 return $feedamicEntry instanceOf \App\Models\MyCustomFeedamicEntry::class;12 }13);
Feed-specific modifier
When you pass an array of feeds
(and it could only be an array of one), your modifier will only apply if the Entry appears in one of those feeds.
This Modifier will only apply to an Entry in the "blog" Feed.
1use MityDigital\Feedamic\Abstracts\AbstractFeedamicEntry; 2use MityDigital\Feedamic\Facades\Feedamic; 3use Statamic\Fields\Value; 4 5Feedamic::modify( 6 feedamicEntryProperty: 'content', 7 modifier: function (AbstractFeedamicEntry $feedamicEntry, null|string|Value $value) { 8 // do something to the $value variable 9 },10 feeds: ['blog']11);
Bard with Sets
Bard fields are automatically converted to HTML, and you can choose to include or exclude Bard sets.
See Bard for full details.
However, when you have Bard sets included, you need to process the Bard field yourself. A Modifier allows you to render that markup however you see fit for your specific site setup.
For this example, if our Bard content includes sets, we will render the feedamic.content
view (note, this is something you need to create within your site) that processes the Bard array and renders your Sets as you need. Review Statamic's documentation if you need.
1use MityDigital\Feedamic\Abstracts\AbstractFeedamicEntry; 2use MityDigital\Feedamic\Facades\Feedamic; 3use Statamic\Fields\Value; 4 5Feedamic::modify( 6 fieldHandle: 'content', 7 modifier: function (AbstractFeedamicEntry $feedamicEntry, mixed $value) { 8 return view('feedamic.content', [ 9 'content' => $value,10 ]);11 },12 when: function (AbstractFeedamicEntry $feedamicEntry, mixed $value): bool {13 if ($value instanceof Value && $value->fieldtype() instanceof Bard) {14 $hasSets = collect($value->raw())->first(fn (array $block) => Arr::get($block, 'type', 'paragraph') === 'set');15 if ($hasSets) {16 return true;17 }18 }19 20 return false;21 },22);
You may want to use the same Sets as your standard Bard rendering, or you could create a feed-specific layout that perhaps has simpler style requirements for greater feed reader support. That is totally up to you.
Page Builder
Many use a Replicator fieldtype to create a Page Builder, with a number of blocks that can include all sorts of different content including Bard.
This is a great use case to a Processor to handle the Page Builder structure to HTML for our Entry's page_builder
field, and then the summary
Modifier to truncate this to 100 words only.
1use MityDigital\Feedamic\Abstracts\AbstractFeedamicEntry; 2use MityDigital\Feedamic\Facades\Feedamic; 3use Statamic\Fields\Value; 4 5Feedamic::processor( 6 fieldHandle: 'page_builder', 7 processor: function (AbstractFeedamicEntry $feedamicEntry, null|string|Value $value) { 8 // create your own view, and handle the blocks within your page builder 9 return view('feedamic.page_builder', [10 'page_builder' => $value,11 ]);12 },13);14 15Feedamic::modify(16 feedamicEntryProperty: 'summary',17 modifier: function (AbstractFeedamicEntry $feedamicEntry, null|string|Value $value) {18 return Str::limit($value, 100, preserveWords: true);19 }20);