Seeder by CartThrob Version 1.2.0
Creating a Channel Entry FieldType
To use your FieldTypes with the CartThrob\Seeders\Channel
library, an ExpressionEngine FieldType MUST:
- Be installed AND assigned to a Channel within ExpressionEngine
- Have an object that inherits from
CartThrob\Seeder\Channels\AbstractField
- Update your
addon.setup.php
file to let Seeder now where your object is
The FieldType Object
A Basic FieldType Object
Depending on how data for your FieldType is stored within ExpressionEngine, building your custom FieldType into the library can be as simple as:
Full Example
<?php
namespace CartThrob\Seeder\Channels\Fields;
use CartThrob\Seeder\Channels\AbstractField;
class FieldTypeName extends AbstractField
{
}
At the most basic, the system just uses the data stored in the channel data set. Note that this is the raw data with zero processing done on it so if you need to manipulate the data you'll have to flesh this out more.
A Slightly Advanced FieldType
Some FieldTypes, like Checkbox
, store data in the channel data set BUT also needs to process it beforhand. Pipe delimited, in this case. And this is where the read()
and prepValueForStorage
methods come into play.
read
Has one parameter (the data from channel data) that you can manipulate and return for use programmatically.
prepValueForStorage
Should convert the processed data back into same format as storage.
Full Example
<?php
namespace CartThrob\Seeder\Channels\Fields;
use CartThrob\Seeder\Channels\AbstractField;
class Checkboxes extends AbstractField
{
/**
* @param mixed $value
* @return string[]
*/
public function read($value)
{
return $this->explodePipe($value);
}
/**
* @param $value
* @return bool|string
*/
public function prepValueForStorage($value)
{
return implode('|', $value);
}
}
An Advanced FieldType
This is where it gets fun. For those FieldTpes that store data seperately from the Entry, you have a couple more hoops to jump through but you also have a couple new toys to help out:
read
Same as the above, but the $value
will likely be empty.
save
Called after the Channel Entry has been created or updated.
delete
Called after the Channel entry has been deleted.
Full Example
<?php
namespace CartThrob\Seeder\Channels\Fields;
use CartThrob\Seeder\Channels\AbstractField;
class Relationship extends AbstractField
{
/**
* This Field isn't stored in channel_data table
* @var bool
*/
protected $cd_storage = false;
/**
* @param $value
* @return bool|void
*/
public function save($value)
{
}
/**
* Retuns an array of related entry_id
* @param mixed $value
* @return array
*/
public function read($value)
{
}
/**
* Removes all the Relationships assigned by this Entry
* @return bool|void
*/
public function delete()
{
}
}
Working With Seeder
Now, if all you want to do is use the Channel Entry library, the above has you sorted. Be sure to check out how the FieldTypes that ship with the Channel Entry library are structured, but that should do it. But, if you want to use your FieldType within CartThrob Seeder, you have two more things to do:
- Ensure your FieldType implements the
CartThrob\Seeder\Core\SeedInterface
interface. - Add a
fakieData
data method.
The fakieData
method accepts 2 parameters; an instance of \Faker\Generator
library and the parent CartThrob\Seeder\Core\AbstractSeed
object.
Full Example
<?php
namespace CartThrob\Seeder\Channels\Fields;
use CartThrob\Seeder\Channels\AbstractField;
use CartThrob\Seeder\Core\AbstractSeed;
use CartThrob\Seeder\Core\SeedInterface;
class Text extends AbstractField implements SeedInterface
{
/**
* @param \Faker\Generator $faker
* @param AbstractSeed $seed
* @return mixed|string
*/
public function fakieData(\Faker\Generator $faker, AbstractSeed $seed)
{
return ucwords(implode(' ', $faker->words(rand(3, 6))));
}
}
Example addon.setup.php
//gotta ensure Seed object is added to autoload ;)
if (defined('PATH_THIRD')) {
require_once PATH_THIRD . 'custom_addon/vendor/autoload.php';
}
return [
'author' => 'Custom Addon',
'author_url' => 'https://yoururl.com',
'docs_url' => 'https://www.yoururl.com/docs',
'name' => CUSTOM_ADDON_NAME,
'description' => 'Custom bucket container for mess around stuff',
'version' => CUSTOM_ADDON_VERSION,
'namespace' => 'Your\Namespace',
'settings_exist' => false,
'seeder' => [
'fields' => [
'your_field_name' => Your\Namespace\Fields\YourField::class,
'your_other_seed_name' => Your\Namespace\Fields\YourOtherField::class,
]
],
];