Created
February 21, 2018 18:37
-
-
Save squatto/7171748afc562464f7f1efdf15b6cf47 to your computer and use it in GitHub Desktop.
PageTypeSeeder and an implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Freemotion\FreemotionTheme\Database\Seeder; | |
class AboutPageTypeSeeder extends PageTypeSeeder | |
{ | |
protected $slug = 'about'; | |
protected $name = 'About Freemotion Page'; | |
protected $description = 'About Freemotion page type'; | |
protected $layout = '{% include "theme::about/index" %}'; | |
protected $fields = [ | |
'header_image' => [ | |
'type' => 'anomaly.field_type.file', | |
], | |
'header_tagline' => [ | |
'type' => 'anomaly.field_type.text', | |
], | |
'header_text' => [ | |
'type' => 'anomaly.field_type.textarea', | |
], | |
'freemotion_text' => [ | |
'type' => 'anomaly.field_type.wysiwyg', | |
], | |
'icon_logo' => [ | |
'type' => 'anomaly.field_type.file', | |
], | |
'icon_image' => [ | |
'type' => 'anomaly.field_type.file', | |
], | |
'icon_text' => [ | |
'type' => 'anomaly.field_type.wysiwyg', | |
], | |
'icon_brand_logos' => [ | |
'type' => 'anomaly.field_type.files', | |
'config' => [ | |
'folders' => ['icon_brand_logos'], | |
], | |
], | |
]; | |
protected $assignments = [ | |
'header_image' => [ | |
'required' => true, | |
], | |
'header_tagline' => [ | |
'required' => true, | |
'translatable' => true, | |
], | |
'header_text' => [ | |
'required' => true, | |
'translatable' => true, | |
], | |
'freemotion_text' => [ | |
'required' => true, | |
'translatable' => true, | |
], | |
'icon_logo' => [ | |
'required' => true, | |
], | |
'icon_image' => [ | |
'required' => true, | |
], | |
'icon_text' => [ | |
'required' => true, | |
'translatable' => true, | |
], | |
'icon_brand_logos' => [ | |
'required' => true, | |
], | |
]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Freemotion\FreemotionTheme\Database\Seeder; | |
use Anomaly\Streams\Platform\Database\Seeder\Seeder; | |
use Anomaly\PagesModule\Type\Contract\TypeInterface; | |
use Anomaly\PagesModule\Type\Contract\TypeRepositoryInterface; | |
use Anomaly\Streams\Platform\Assignment\Contract\AssignmentRepositoryInterface; | |
use Anomaly\Streams\Platform\Field\Contract\FieldRepositoryInterface; | |
use Anomaly\Streams\Platform\Stream\Contract\StreamRepositoryInterface; | |
class PageTypeSeeder extends Seeder | |
{ | |
/** | |
* The type repository. | |
* | |
* @var TypeRepositoryInterface | |
*/ | |
protected $typeRepository; | |
/** | |
* The field repository. | |
* | |
* @var FieldRepositoryInterface | |
*/ | |
protected $fieldRepository; | |
/** | |
* The streams repository. | |
* | |
* @var StreamRepositoryInterface | |
*/ | |
protected $streamRepository; | |
/** | |
* The assignment repository. | |
* | |
* @var AssignmentRepositoryInterface | |
*/ | |
protected $assignmentRepository; | |
// configuration | |
protected $namespace = 'pages'; | |
protected $slug = ''; | |
protected $name = ''; | |
protected $description = ''; | |
protected $fields = []; | |
protected $assignments = []; | |
protected $layout = ''; | |
/** | |
* Create a new PageTypeSeeder instance. | |
* | |
* @param TypeRepositoryInterface $typeRepository | |
* @param FieldRepositoryInterface $fieldRepository | |
* @param StreamRepositoryInterface $streamRepository | |
* @param AssignmentRepositoryInterface $assignmentRepository | |
*/ | |
public function __construct( | |
TypeRepositoryInterface $typeRepository, | |
FieldRepositoryInterface $fieldRepository, | |
StreamRepositoryInterface $streamRepository, | |
AssignmentRepositoryInterface $assignmentRepository | |
) | |
{ | |
$this->typeRepository = $typeRepository; | |
$this->fieldRepository = $fieldRepository; | |
$this->streamRepository = $streamRepository; | |
$this->assignmentRepository = $assignmentRepository; | |
} | |
/** | |
* Run the seeder. | |
*/ | |
public function run() | |
{ | |
$this->checkRequired(); | |
if ($type = $this->typeRepository->findBySlug($this->slug)) { | |
// you MUST call forceDelete() or you will get integrity constraint violations | |
$this->typeRepository->forceDelete($type); | |
} | |
/* @var TypeInterface $type */ | |
$type = $this->typeRepository->create([ | |
'en' => [ | |
'name' => $this->name, | |
'description' => $this->description, | |
], | |
'slug' => $this->slug, | |
'handler' => 'anomaly.extension.default_page_handler', | |
'theme_layout' => 'theme::layouts/default.twig', | |
'layout' => $this->layout ?: '{% include "theme::' . $this->slug . '/index" %}', | |
]); | |
$stream = $type->getEntryStream(); | |
// create fields | |
foreach ($this->fields as $fieldSlug => $setup) { | |
$field = $this->fieldRepository->findBySlugAndNamespace($fieldSlug, $this->namespace); | |
if ($field) { | |
// you MUST call forceDelete() or you will get integrity constraint violations | |
$field->forceDelete(); | |
} | |
// create the field | |
$setup['slug'] = $fieldSlug; | |
$setup['namespace'] = $this->namespace; | |
$setup['locked'] = false; | |
$setup['name'] = array_get($setup, 'name', "freemotion.theme.freemotion::field.$fieldSlug.name"); | |
$setup['instructions'] = array_get($setup, 'instructions', "freemotion.theme.freemotion::field.$fieldSlug.instructions"); | |
$setup['placeholder'] = array_get($setup, 'placeholder', "freemotion.theme.freemotion::field.$fieldSlug.placeholder"); | |
$field = $this->fieldRepository->create($setup); | |
// create the field assignment | |
$assignment = array_get($this->assignments, $fieldSlug, []); | |
$assignment['stream'] = $stream; | |
$assignment['field'] = $field; | |
$this->assignmentRepository->create($assignment); | |
} | |
// assign the content field | |
$this->assignmentRepository->create([ | |
'translatable' => true, | |
'stream' => $stream, | |
'field' => $this->fieldRepository->findBySlugAndNamespace('content', 'pages'), | |
]); | |
} | |
private function checkRequired() | |
{ | |
if (! $this->namespace || ! $this->slug || ! $this->name || ! $this->description) { | |
throw new \Exception('Required page type seeder values were not set'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment