Create a multiple file field and assign it to a page type, using seeder / migration.

araminho - 1 week ago

I created a new field of "Files" type and assigned it to "Default" page type. Now I want to do the same with a seeder or a migration. What should I do?

ryanthompson - 1 week ago

This would be with a migration - pretty straight forward like any other field type. Is there anything specific you need help with? Docs and existing addons are littered with examples and available configuration.

araminho - 1 week ago

Thank you, I found the solution. In case if anyone needs it, here it is <?php

use Anomaly\PagesModule\Type\Contract\TypeInterface; use Anomaly\PagesModule\Type\Contract\TypeRepositoryInterface; use Anomaly\Streams\Platform\Assignment\Contract\AssignmentRepositoryInterface; use Anomaly\Streams\Platform\Database\Seeder\Seeder; use Anomaly\Streams\Platform\Field\Contract\FieldRepositoryInterface; use Anomaly\Streams\Platform\Stream\Contract\StreamRepositoryInterface;

/** * Class TypeSeeder * * @link http://pyrocms.com/ * @author PyroCMS, Inc. [email protected] * @author Ryan Thompson [email protected] */ class FieldSeeder extends Seeder {

/**
 * The type repository.
 *
 * @var TypeRepositoryInterface
 */
protected $types;

/**
 * The field repository.
 *
 * @var FieldRepositoryInterface
 */
protected $fields;

/**
 * The streams repository.
 *
 * @var StreamRepositoryInterface
 */
protected $streams;

/**
 * The assignment repository.
 *
 * @var AssignmentRepositoryInterface
 */
protected $assignments;

/**
 * Create a new TypeSeeder instance.
 *
 * @param TypeRepositoryInterface       $types
 * @param FieldRepositoryInterface      $fields
 * @param StreamRepositoryInterface     $streams
 * @param AssignmentRepositoryInterface $assignments
 */
public function __construct(
    TypeRepositoryInterface $types,
    FieldRepositoryInterface $fields,
    StreamRepositoryInterface $streams,
    AssignmentRepositoryInterface $assignments
) {
    $this->types       = $types;
    $this->fields      = $fields;
    $this->streams     = $streams;
    $this->assignments = $assignments;
}

/**
 * Run the seeder.
 */
public function run()
{
    $type = $this->types->findBySlug('default');

    /* @var TypeInterface $type */

    $stream = $type->getEntryStream();

    $field = $this->fields->findBySlugAndNamespace('images_1', 'pages');

    if (!$field) {
        $field = $this->fields->create([
            'slug' => 'images_1',
            'namespace' => 'pages',
            'type' => 'anomaly.field_type.files',
            'locked' => false,
            'en' => [
                'name' => 'Images for first section'
            ]
        ]);
    }

    $this->assignments->create(
        [
            'translatable' => true,
            'stream'       => $stream,
            'field'        => $field
        ]
    );
}

}