Seeding page with grid data
Created 7 years ago by william

So i am stuck. Thought i wasnt, but i am. This is what i got so far. How do add this grid "row" to the page?

<?php

use Illuminate\Database\Seeder;
use Anomaly\PagesModule\Page\Contract\PageRepositoryInterface;
use Anomaly\PagesModule\Type\Contract\TypeRepositoryInterface;
use Anomaly\Streams\Platform\Model\Grid\GridTextEntryModel;

class PageGridSeeder extends Seeder
{

    public function __construct(PageRepositoryInterface $pages, TypeRepositoryInterface $types)
    {
        $this->pages = $pages;
        $this->types = $types;
    }

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $type = $this->types->findBySlug('default_content_page');

        $text = GridTextEntryModel::create(
            [
                'en'     => [
                    'locale' => 'en',
                ],
                'content' => 'Test Content'
            ]);

        $page = $this->pages->create(
            [
                'en'           => [
                    'title' => 'dumb dumber and then wordpress',
                ],
                'slug'         => 'when you are dumb you just are',
                'type'         => $type,
                'enabled'      => true,
                'home'         => false,
                'theme_layout' => 'theme::layouts/default.twig',
            ]
        )->allowedRoles()->sync([]);

    }
}

This is my GridTextEntryModel

<?php namespace Anomaly\Streams\Platform\Model\Grid;

use Anomaly\Streams\Platform\Entry\EntryModel;

class GridTextEntryModel extends EntryModel
{

    protected $searchable = false;

    protected $table = 'grid_text';

    protected $titleName = 'id';

    protected $rules = [
'content' => '',
];

    protected $fields = [
'content',
];

    protected $dates = ['created_at', 'updated_at'];

    protected $relationships = [];

    protected $translationForeignKey = 'entry_id';

    protected $translationModel = 'Anomaly\Streams\Platform\Model\Grid\GridTextEntryTranslationsModel';

    protected $stream = [
'id' => '31',
'namespace' => 'grid',
'slug' => 'text',
'prefix' => 'grid_',
'title_column' => 'id',
'order_by' => 'id',
'locked' => '0',
'hidden' => '0',
'sortable' => '1',
'searchable' => '0',
'trashable' => '0',
'translatable' => '1',
'config' => '',
'assignments' => [
[
'id' => '132',
'sort_order' => '131',
'stream_id' => '31',
'field_id' => '121',
'config' => 'a{i:0;s:6:"a:0:{}";}',
'unique' => '0',
'required' => '0',
'translatable' => '0',
'field' => [
'id' => '121',
'namespace' => 'grid',
'slug' => 'content',
'type' => 'anomaly.field_type.wysiwyg',
'config' => 'a:4:{s:7:"buttons";a:8:{i:0;s:6:"format";i:1;s:4:"bold";i:2;s:6:"italic";i:3;s:7:"deleted";i:4;s:5:"lists";i:5;s:4:"link";i:6;s:14:"horizontalrule";i:7;s:9:"underline";}s:7:"plugins";a:8:{i:0;s:9:"alignment";i:1;s"inlinestyle";i:2;s:5:"table";i:3;s:5:"video";i:4;s"filemanager";i:5;s:12:"imagemanager";i:6;s:6:"source";i:7;s:10:"fullscreen";}s:6:"height";s:3:"500";s"line_breaks";b:0;}',
'locked' => '0',
'translations' => [
[
'id' => '121',
'field_id' => '121',
'locale' => 'en',
'name' => 'Content',
'placeholder' => '',
'warning' => '',
'instructions' => '',
],
],
],
'translations' => [
[
'id' => '132',
'assignment_id' => '132',
'locale' => 'en',
'label' => '',
'warning' => '',
'placeholder' => '',
'instructions' => '',
],
],
],
],
'translations' => [
[
'id' => '31',
'stream_id' => '31',
'locale' => 'en',
'name' => 'Text',
'description' => '',
],
],
];

}

And this is my PagesDefaultContentPagePagesEntryModel:

<?php namespace Anomaly\Streams\Platform\Model\Pages;

use Anomaly\Streams\Platform\Entry\EntryModel;

class PagesDefaultContentPagePagesEntryModel extends EntryModel
{

    use \Illuminate\Database\Eloquent\SoftDeletes;

    protected $searchable = false;

    protected $table = 'pages_default_content_page_pages';

    protected $titleName = 'id';

    protected $rules = [
'main_content' => '',
];

    protected $fields = [
'main_content',
];

    protected $dates = ['created_at', 'updated_at', 'deleted_at'];

    protected $relationships = ['main_content'];

    protected $translationForeignKey = 'entry_id';

    protected $translationModel = 'Anomaly\Streams\Platform\Model\Pages\PagesDefaultContentPagePagesEntryTranslationsModel';

    protected $stream = [
'id' => '28',
'namespace' => 'pages',
'slug' => 'default_content_page_pages',
'prefix' => 'pages_',
'title_column' => 'id',
'order_by' => 'id',
'locked' => '0',
'hidden' => '1',
'sortable' => '0',
'searchable' => '0',
'trashable' => '1',
'translatable' => '1',
'config' => 'a:0:{}',
'assignments' => [
[
'id' => '135',
'sort_order' => '134',
'stream_id' => '28',
'field_id' => '122',
'config' => 'a{i:0;s:6:"a:0:{}";}',
'unique' => '0',
'required' => '0',
'translatable' => '0',
'field' => [
'id' => '122',
'namespace' => 'pages',
'slug' => 'main_content',
'type' => 'anomaly.field_type.grid',
'config' => 'a:4:{s:7:"related";N;s:7:"add_row";N;s:3:"min";N;s:3:"max";N;}',
'locked' => '0',
'translations' => [
[
'id' => '122',
'field_id' => '122',
'locale' => 'en',
'name' => 'Main Content',
'placeholder' => '',
'warning' => '',
'instructions' => '',
],
],
],
'translations' => [
[
'id' => '135',
'assignment_id' => '135',
'locale' => 'en',
'label' => '',
'warning' => '',
'placeholder' => '',
'instructions' => '',
],
],
],
],
'translations' => [
[
'id' => '28',
'stream_id' => '28',
'locale' => 'en',
'name' => 'Default Content Page',
'description' => '',
],
],
];

public function mainContent()
{

return $this->getFieldType('main_content')->getRelation();
}

}
piterden  —  7 years ago
<?php namespace Defr\DelozvukaModule\Seeder;

use Anomaly\UsersModule\User\UserActivator;
use Anomaly\Streams\Platform\Database\Seeder\Seeder;
use Anomaly\UsersModule\User\Contract\UserInterface;
use Anomaly\UsersModule\Role\Contract\RoleRepositoryInterface;
use Anomaly\UsersModule\User\Contract\UserRepositoryInterface;

/**
 * Class UserSeeder
 *
 * @link          http://pyrocms.com/
 * @author        PyroCMS, Inc. <support@pyrocms.com>
 * @author        Ryan Thompson <ryan@pyrocms.com>
 * @author        Denis Efremov <efremov.a.denis@gmail.com>
 */
class UserSeeder extends Seeder
{

    /**
     * The user repository.
     *
     * @var UserRepositoryInterface
     */
    protected $users;

    /**
     * The role repository.
     *
     * @var RoleRepositoryInterface
     */
    protected $roles;

    /**
     * The activator utility.
     *
     * @var UserActivator
     */
    protected $activator;

    /**
     * Create a new UserSeeder instance.
     *
     * @param UserRepositoryInterface $users
     * @param RoleRepositoryInterface $roles
     * @param UserActivator           $activator
     */
    public function __construct(
        UserRepositoryInterface $users,
        RoleRepositoryInterface $roles,
        UserActivator $activator
    ) {
        $this->users     = $users;
        $this->roles     = $roles;
        $this->activator = $activator;
    }

    /**
     * Run the seeder.
     */
    public function run()
    {
        $this->users->truncate();

        $admin = $this->roles->findBySlug('admin');
        $user  = $this->roles->findBySlug('user');

        /* @var UserInterface $administrator */
        $administrator = $this->users->create(
            [
                'display_name' => 'Administrator',
                'email'        => env('ADMIN_EMAIL'),
                'username'     => env('ADMIN_USERNAME'),
                'password'     => env('ADMIN_PASSWORD'),
            ]
        );
        $administrator->roles()->sync([$admin->getId()]);
        $this->activator->force($administrator);

        for ($i = 0; $i < 200; $i++) {
            /* @var UserInterface $demo */
            $demo = $this->users->create(
                [
                    'display_name' => 'Demo User '.$i,
                    'email'        => 'demo'.$i.'@pyrocms.com',
                    'password'     => 'password'.$i,
                    'username'     => 'demo'.$i,
                ]
            );
            $demo->roles()->sync([$user->getId()]);
            $this->activator->force($demo);
            echo 'Demo user '.$i.' created';
        }
    }
}
piterden  —  7 years ago

Here are public methods of Anomaly\UsersModule\User\UserModel:

scopeSorted(Builder $builder, $direction = 'asc')
make()
content()
getPath()
getStrId()
getSlug()
getMetaTitle()
getMetaKeywords()
getMetaDescription()
isExact()
isEnabled()
isVisible()
isHome()
getParent()
getParentId()
getChildren()
getAllowedRoles()
getRouteSuffix($prefix = null)
getType()
getHandler()
getThemeLayout()
getEntry()
getEntryId()
getContent()
setContent($content)
getResponse()
setResponse(Response $response)
isCurrent()
setCurrent($current)
isActive()
setActive($active)
children()
toSearchableArray()

U need to use getAllowedRoles()

ryanthompson  —  7 years ago

You are missing the entry pattern for the page.

Pages technically have this relationship structure:

$page->entry->custom_field;

So the page::create should include a new instance of your page type entry model like this: https://github.com/anomalylabs/pages-module/blob/master/src/Page/PageSeeder.php#L78

ryanthompson  —  7 years ago

Aaand your entry would hold the grid field so you would follow this tutorial for seeding the grid: https://pyrocms.com/help/addon-tutorials/grid-field-type/seeding-grid-data