Skip to content

Commit

Permalink
Merge pull request #150 from studio1902/feature/add-page
Browse files Browse the repository at this point in the history
Add ability to add a page to mount collection on
  • Loading branch information
robdekort committed Oct 24, 2021
2 parents c06b58c + c8e1667 commit e39b117
Showing 1 changed file with 50 additions and 9 deletions.
59 changes: 50 additions & 9 deletions dev/app/Console/Commands/AddCollection.php
Expand Up @@ -4,11 +4,14 @@

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Statamic\Console\RunsInPlease;
use Statamic\Facades\Config;
use Statamic\Facades\Entry;
use Statamic\Support\Arr;
use Symfony\Component\Yaml\Yaml;
use Stringy\StaticStringy as Stringy;

class AddCollection extends Command
{
Expand Down Expand Up @@ -56,6 +59,20 @@ class AddCollection extends Command
*/
protected $route = '';

/**
* Add page.
*
* @var bool
*/
protected $add_page = true;

/**
* The page title.
*
* @var string
*/
protected $page_title = '';

/**
* The layout
*
Expand Down Expand Up @@ -138,10 +155,17 @@ public function handle()
$this->public = ($this->confirm('Should this be a public collection with a route?', true)) ? true : false;
if ($this->public) {
$this->route = $this->ask('What should be the route for this collection?', '/{mount}/{slug}');
$choice = $this->choice('On which page do you want to mount this collection?', $this->getPages());
preg_match('/\[(.*?)\]/', $choice, $id);
$this->mount = $id[1];
$this->setIndexTemplate($id[1]);
$this->add_page = ($this->confirm('Do you want to create a new page to mount this collection on?', true)) ? true : false;
if ($this->add_page) {
$this->page_title = $this->ask('What should be the page title for this mount?');
$this->mount = $this->addPage();
$this->setIndexTemplate($this->mount);
} else {
$choice = $this->choice('On which page existing page do you want to mount this collection?', $this->getPages());
preg_match('/\[(.*?)\]/', $choice, $id);
$this->mount = $id[1];
$this->setIndexTemplate($id[1]);
}
}
$this->layout = $this->ask('What should be the layout file for this collection?', 'layout');
$this->revisions = ($this->confirm('Should revisions be enabled?', false)) ? true : false;
Expand All @@ -167,7 +191,7 @@ public function handle()
return $this->error($e->getMessage());
}

// $this->createPartial();
Artisan::call('cache:clear');

$this->info("Collection '{$this->collection_name}' created.");
}
Expand All @@ -191,12 +215,12 @@ protected function checkExistence($type, $path)
*/
protected function getPages()
{
$query = Entry::query()
return Entry::query()
->where('collection', 'pages')
->where('status', 'published')
->orderBy('title', 'asc');

return $query->get()->map(fn($entry) =>
->orderBy('title', 'asc')
->get()
->map(fn($entry) =>
"{$entry->get('title')} [{$entry->id()}]"
)
->toArray();
Expand Down Expand Up @@ -294,6 +318,23 @@ protected function createShowTemplate()
File::put(base_path("resources/views/{$this->filename}/show.antlers.html"), $contents);
}

/**
* Add a page.
*
* @return string
*/
protected function addPage()
{
$entry = Entry::make()
->collection('pages')
->published(true)
->slug(Stringy::slugify($this->page_title, '-', Config::getShortLocale()))
->data(['title' => $this->page_title]);
$entry->save();

return $entry->id();
}

/**
* Set index template.
*
Expand Down

0 comments on commit e39b117

Please sign in to comment.