Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added example for pages #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions migrations/m180221_092709_adigital_matrix_fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,35 @@ public function safeUp()
/**
* Group: Matrix
*/

$matrixGroup = null;
$groups = Craft::$app->getFields()->getAllGroups();
foreach($groups as $group) {
if ($group->name == "Matrix") {
$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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -455,7 +455,7 @@ public function safeUp()
]);
Craft::$app->getFields()->saveField($contentBlock);
}

return true;
}

Expand All @@ -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;
}
}
47 changes: 47 additions & 0 deletions migrations/m180323_132831_adigital_pages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace craft\contentmigrations;
use Craft;
use craft\models\Section;
use craft\models\Section_SiteSettings;
use craft\db\Migration;
/**
* m180108_154605_news migration.
*/
class m180323_132831_adigital_pages extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{
$section = new Section();
$section->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;
}
}