From 8a3e66097345e13b71c5541e2a4da0d9b0efffa6 Mon Sep 17 00:00:00 2001 From: Len van Essen Date: Fri, 23 Mar 2018 14:59:01 +0100 Subject: [PATCH] Added example for pages --- .../m180221_092709_adigital_matrix_fields.php | 22 ++++----- migrations/m180323_132831_adigital_pages.php | 47 +++++++++++++++++++ 2 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 migrations/m180323_132831_adigital_pages.php diff --git a/migrations/m180221_092709_adigital_matrix_fields.php b/migrations/m180221_092709_adigital_matrix_fields.php index 9ac9172..6b4e37c 100644 --- a/migrations/m180221_092709_adigital_matrix_fields.php +++ b/migrations/m180221_092709_adigital_matrix_fields.php @@ -21,7 +21,7 @@ public function safeUp() /** * Group: Matrix */ - + $matrixGroup = null; $groups = Craft::$app->getFields()->getAllGroups(); foreach($groups as $group) { @@ -29,27 +29,27 @@ public function safeUp() $matrixGroup = $group; } } - + if (is_null($matrixGroup)) { $matrixGroup = new \craft\models\FieldGroup(); $matrixGroup->name = "Matrix"; Craft::$app->getFields()->saveGroup($matrixGroup); } - + $mainUploadsVolume = Craft::$app->volumes->getVolumeByHandle("mainUploads"); $mainUploadsFolderTree = Craft::$app->assets->getFolderTreeByVolumeIds([$mainUploadsVolume->id]); $mainUploadsFolder = $mainUploadsFolderTree[0]->id; $galleryVolume = Craft::$app->volumes->getVolumeByHandle("gallery"); $galleryFolderTree = Craft::$app->assets->getFolderTreeByVolumeIds([$galleryVolume->id]); $galleryFolder = $galleryFolderTree[0]->id; - + ///////////////////////////////////////////// ////////////// MATRIX FIELDS ////////////// ///////////////////////////////////////////// /** * Fields: Slider, Gallery Block, Content Block * - * Blocks: + * Blocks: * Slider: Slides * Gallery Block: Images, Description * Content Block: Content, Text, Heading, Image, Video, Gallery, Quote @@ -84,7 +84,7 @@ public function safeUp() * print_r($matrixField); * exit; */ - + if (is_null(Craft::$app->getFields()->getFieldByHandle("slider"))) { $homeBlock = new \craft\fields\Matrix([ "groupId" => $matrixGroup->id, @@ -142,7 +142,7 @@ public function safeUp() ]); Craft::$app->getFields()->saveField($homeBlock); } - + if (is_null(Craft::$app->getFields()->getFieldByHandle("galleryBlock"))) { $galleryBlock = new \craft\fields\Matrix([ "groupId" => $matrixGroup->id, @@ -218,7 +218,7 @@ public function safeUp() ]); Craft::$app->getFields()->saveField($galleryBlock); } - + if (is_null(Craft::$app->getFields()->getFieldByHandle("contentBlock"))) { $contentBlock = new \craft\fields\Matrix([ "groupId" => $matrixGroup->id, @@ -455,7 +455,7 @@ public function safeUp() ]); Craft::$app->getFields()->saveField($contentBlock); } - + return true; } @@ -476,14 +476,14 @@ public function safeDown() if (!is_null($contentBlock)) { Craft::$app->getFields()->deleteFieldById($contentBlock->id); } - + $groups = Craft::$app->getFields()->getAllGroups(); foreach($groups as $group) { if (count(Craft::$app->getFields()->getFieldsByGroupId($group->id)) == 0 && $group->name == "Matrix") { Craft::$app->getFields()->deleteGroupById($group->id); } } - + return true; } } diff --git a/migrations/m180323_132831_adigital_pages.php b/migrations/m180323_132831_adigital_pages.php new file mode 100644 index 0000000..aad4e5b --- /dev/null +++ b/migrations/m180323_132831_adigital_pages.php @@ -0,0 +1,47 @@ +name = 'Pages'; + $section->type = 'structure'; + $section->handle = 'pages'; + $section->enableVersioning = true; + $allSiteSettings = []; + foreach (Craft::$app->getSites()->getAllSites() as $site) { + $siteSettings = new Section_SiteSettings(); + $siteSettings->siteId = $site->id; + $siteSettings->hasUrls = true; + $siteSettings->uriFormat = '{slug}'; + $siteSettings->template = 'pages/_entry'; + $allSiteSettings[$site->id] = $siteSettings; + } + $section->setSiteSettings($allSiteSettings); + // Setting the saveSection runvalidation to false helps throw debugging errors + $status = Craft::$app->getSections()->saveSection($section); + return $status; + } + /** + * @inheritdoc + */ + public function safeDown() + { + // Get the section + $section = Craft::$app->sections->getSectionByHandle('pages'); + // Delete it + $success = Craft::$app->sections->deleteSectionById($section->id); + return $success; + } +} \ No newline at end of file