How do you seed a page with blocks
Created 5 years ago by william

I have a block field type with the slug content_blocks assigned to the default_pages type. Then i got a block named wysiwyg_block that i want to use/add to a page i create through a seed. I tried the following but i get an error message: In BlockModel.php line 132: Call to a member function setBlock() on null. Could anyone shed some lights on this? Maybe a code example @ryanthompson ?

<?php

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

class AboutSeeder extends Seeder
{
    /**
     * The page repository.
     *
     * @var PageRepositoryInterface
     */
    protected $pages;

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

    /**
     * Create a new PageSeeder instance.
     *
     * @param PageRepositoryInterface $pages
     * @param TypeRepositoryInterface $types
     */
    public function __construct(PageRepositoryInterface $pages, TypeRepositoryInterface $types)
    {
        $this->pages = $pages;
        $this->types = $types;
    }

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

        $wysiwygContent = WysiwygBlockBlocksEntryModel::create([
            'content' => 'Test Content'
        ]);

        $page = $this->pages->create(
            [
                'en' => [
                    'title' => 'We will make the web great again'
                ],
                'slug'         => str_slug('We will make the web great again!'),
                'entry'        => $pageType->getEntryModel()->create(),
                'type'         => $pageType,
                'enabled'      => true,
                'home'         => false,
                'theme_layout' => 'theme::layouts/default.twig',
            ]
        );

        $page->allowedRoles()->sync([]);
        $page->entry->contentBlocks()->create(['entry' => $wysiwygContent]);
    }
}
ryanthompson  —  5 years ago

I just did this the other day! Here is a gist of my class. I was doing posts but the same principle applies:

https://gist.github.com/RyanThompson/0812750ecf8695f83c23ef3b1c04f5af

william  —  5 years ago

Much appreciated

william  —  5 years ago