Metafields
Usage
Any metafield can be accessed via the metafield
method on your metafieldable
model by passing in
the key of the metafield that you want and getting the value.
1$page->metafield('author')->getValue();
In your Blade file you can simply output it as a string and you will get the hydrated value back.
1{!! $page->metafield('featuredMedia') !!}
Making your model have metafields
All you have to do in order to give your model the ability to define metafields is to implement the
Metafieldable
contract on your model and use the the
HasMetafields
trait. That will make it show up in the "Metafields"
settings.
1use Helix\Lego\Models\Contracts\Metafieldable;2use Helix\Lego\Models\Traits\HasMetafields;3 4class Collection extends Model implements Metafieldable5{6 use HasMetafields;7 // ...8}
If you're building an app then make sure that your model has also been registered with Strata. See Registering models
Adding your own metafield type
In order to create your own metafield type, we need to add to files
- A PHP class extending the
Helix\Lego\Metafields\Types\MetafieldType
class - A Blade file for interacting with the metafield.
1<?php 2 3namespace Astrogoat\Docs\Metafields\Types; // Replace with your own namespace. 4 5use Helix\Lego\Metafields\Types\MetafieldType; 6 7class Testimonial extends MetafieldType 8{ 9 public function render()10 {11 return view('metafields.types.testimonial');12 }13}
The extended MetafieldType
class is also an extension of Livewire\Component
, so you have the full
power Livewire at your desposal.
The only method we are required to implement is the render
method, which defines which view we
should use to display the metafield definition to the user.
In the Blade file, we are free to define any input fields we need.
1<!--// EXAMPLE -->2<div wire:key="{{ $definition->id }}">3 <x-fab::forms.input4 :label="$definition->name"5 :help="$definition->description"6 wire:model="definition.value"7 id="{{ 'metafield_' . $definition->id }}"8 />9</div>
The metafield definition model is passed to the view as $definition
.
The only thing we need to do, is to make sure that our inputs are passed back to the
definition.value
property, so they will get properly saved to the model when the user saves.
One way this can be done, is in the example above by setting the wire:model="definition.value"
.
Next will need to register the Livewire component in your Service Provider.
1use Astrogoat\Docs\Metafields\Types\Testimonial; // Replace with your own namespace.2 3 // vvvvvv Replace with your path vvvvvvv 4Livewire::component('astrogoat.docs.metafields.types.media', Testimonial::class);
Seeding metafields
In cases where you want to make sure that a metafield definition will exists across environments, you may
opt to seed your metafields via a seeder. Each metafield type can be easily seeded with the define
method.
Here's an example of seeding a "Text" metafield type definition. It require you to pass in the name
,
key
, description
, and the model
for which the metafield is intended for.
1Text::define(2 name: 'Blurb',3 key: 'blurb',4 description: 'Product blurbs are small descriptions of the product.',5 model: Product::class,6);