PHP code example of anubarak / craft-seeder

1. Go to this page and download the library: Download anubarak/craft-seeder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

anubarak / craft-seeder example snippets




use anubarak\seeder\models\EntryConfig;
use anubarak\seeder\models\FieldCallback;
use anubarak\seeder\models\Settings;
use craft\base\ElementInterface;
use craft\base\FieldInterface;

$config = (new Settings())
    ->fieldsConfig([
        new EntryConfig(
            'news',
            [
                (new FieldCallback('date'))
                    ->setCallable(
                        static function(
                            \Faker\Generator $faker,
                            FieldInterface   $field,
                            ElementInterface $element
                        ) {
                            return new DateTime();
                        }
                    ),
                (new FieldCallback('date2'))
                    ->setCallable(
                        static function(
                            \Faker\Generator $faker,
                            FieldInterface   $field,
                            ElementInterface $element
                        ) {
                            $date = (clone $element->getFieldValue('date'));
                            $date->modify('+1 day');

                            return $date;
                        }
                    ),
                (new FieldCallback('headline'))
                    ->setFakerMethod('text'),
            ]
        )
    ]);

// 

\yii\base\Event::on(
    \anubarak\seeder\services\SeederService::class,
    \anubarak\seeder\services\SeederService::REGISTER_FIELD_TYPES,
    static function(\anubarak\seeder\events\RegisterFieldTypeEvent $event){
        $event->types['my\field\Class'] = MyCustomField::class;
    }
);

use craft\base\ElementInterface;
use craft\base\FieldInterface;
use anubarak\seeder\services\fields\BaseField

class PlainText extends BaseField
{
    /**
     * @inheritDoc
     */
    public function generate(\craft\fields\PlainText|FieldInterface $field, ElementInterface $element = null)
    {
        if(!$field->multiline){
            return $this->factory->text($field->charLimit ?: 200);
        }

        return $this->factory->realText($field->charLimit ?: 200);
    }
}

\yii\base\Event::on(
    \anubarak\seeder\services\UniqueFields::class,
    \anubarak\seeder\services\UniqueFields::EVENT_REGISTER_UNIQUE_FIELDS,
    static function(\anubarak\seeder\events\RegisterUniqueFieldEvent $event){
        $event->fields[] = MyCustomUniqueField::class;
    }
);

class DropdownUniqueField implements UniqueFieldInterface
{
    /**
     * @param \craft\fields\BaseOptionsField $field
     *
     * @inheritDoc
     */
    public function getDescription(Field $field): string
    {
        $options = [];
        foreach ($field->options as $option) {
            $options[] = '<code>' . $option['value'] . '</code>';
        }

        return 'Options: ' . join(' | ', $options);
    }

    /**
     * @param BaseOptionsField $field
     *
     * @inheritDoc
     */
    public function getValues(Field $field): array
    {
        $options = [];
        foreach ($field->options as $option) {
            $options[] = $option['value'];
        }

        return $options;
    }

    /**
     * @inheritDoc
     */
    public function getFieldClass(): string
    {
        return BaseOptionsField::class;
    }
}
Shell
php craft element-seeder/generate/entries --section=news --count=15
Shell
php craft element-seeder/clean-up