Skip to content

Commit

Permalink
carft 5: initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
matfish3 committed Apr 13, 2024
1 parent ecf2573 commit d98f871
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 83 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Release Notes for Blogify

## 3.0.0 - 2024-04-13
- Craft 5: Initial Release

## 2.0.3 - 2022-07-10
### Added
- Add dedicated log file
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"docs": "https://github.com/matfish2/craft-blogify/blob/master/README.md"
},
"require": {
"craftcms/cms": "^4.0.4",
"craftcms/redactor": "^3.0.0"
"craftcms/cms": "^5.0.4",
"craftcms/redactor": "^4.0.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 0 additions & 2 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function safeUp()
BlogListingMigrator::add();
BlogCategoriesMigrator::add();
BlogTagsMigrator::add();
BlogFieldGroupMigrator::add();
BlogAssetsVolumeMigrator::add();
BlogThumbnailTransform::add();
TagPageMigrator::add();
Expand All @@ -51,7 +50,6 @@ public function safeDown()
BlogListingMigrator::remove();
BlogCategoriesMigrator::remove();
BlogTagsMigrator::remove();
BlogFieldGroupMigrator::remove();
BlogAssetsVolumeMigrator::remove();
BlogThumbnailTransform::remove();
TagPageMigrator::remove();
Expand Down
10 changes: 9 additions & 1 deletion src/migrations/Migrators/AuthorPageMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


use Craft;
use craft\models\EntryType;
use craft\models\Section;
use craft\models\Section_SiteSettings;
use matfish\Blogify\Handles;
Expand All @@ -16,11 +17,18 @@ class AuthorPageMigrator extends Migrator

public static function add(): bool
{
$entryType = new EntryType([
'name' => 'Author Page',
'handle' => 'authorPage',
'hasTitleField' => true,
]);

return (new SectionService())->add('Blog Author Page',
Handles::AUTHOR_PAGE,
Section::TYPE_SINGLE,
'/author',
'filters/author/_entry'
'filters/author/_entry',
$entryType
);
}

Expand Down
9 changes: 8 additions & 1 deletion src/migrations/Migrators/BlogChannelMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


use Craft;
use craft\models\EntryType;
use craft\models\Section;
use matfish\Blogify\Handles;
use matfish\Blogify\services\SectionService;
Expand All @@ -15,7 +16,13 @@ public static function add(): bool
{
\Craft::$app->cache->delete(Handles::CHANNEL);

return (new SectionService())->add('Blog', Handles::CHANNEL, Section::TYPE_CHANNEL, '/{slug}', 'post/_entry');
$entryType = new EntryType([
'name' => 'Post',
'handle' => 'post',
'hasTitleField' => true,

]);
return (new SectionService())->add('Blog', Handles::CHANNEL, Section::TYPE_CHANNEL, '/{slug}', 'post/_entry', $entryType);
}

public static function remove(): bool
Expand Down
42 changes: 0 additions & 42 deletions src/migrations/Migrators/BlogFieldGroupMigrator.php

This file was deleted.

10 changes: 9 additions & 1 deletion src/migrations/Migrators/BlogListingMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace matfish\Blogify\migrations\Migrators;

use Craft;
use craft\models\EntryType;
use craft\models\Section;
use craft\models\Section_SiteSettings;
use matfish\Blogify\Handles;
Expand All @@ -13,11 +14,18 @@ class BlogListingMigrator extends Migrator
{
public static function add(): bool
{
$entryType = new EntryType([
'name' => 'Blog Index',
'handle' => 'blogIndex',
'hasTitleField' => true,
]);

return (new SectionService())->add('Blog Listing',
Handles::LISTING,
Section::TYPE_SINGLE,
'/index',
'listing/_entry'
'listing/_entry',
$entryType
);
}

Expand Down
10 changes: 9 additions & 1 deletion src/migrations/Migrators/TagPageMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace matfish\Blogify\migrations\Migrators;

use craft\models\EntryType;
use craft\models\Section;
use matfish\Blogify\Handles;
use matfish\Blogify\services\SectionService;
Expand All @@ -12,11 +13,18 @@ class TagPageMigrator extends Migrator

public static function add(): bool
{
$entryType = new EntryType([
'name' => 'Tag Page',
'handle' => 'tagPage',
'hasTitleField' => true,
]);

return (new SectionService())->add('Blog Tag Page',
Handles::TAG_PAGE,
Section::TYPE_SINGLE,
'/tag',
'filters/tag/_entry'
'filters/tag/_entry',
$entryType
);
}

Expand Down
5 changes: 3 additions & 2 deletions src/services/EntryTypeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public function addFields($fields)
}, $fields);


$section = Craft::$app->sections->getSectionByHandle(Handles::CHANNEL);
$sectionService = Craft::$app->getEntries();
$section = $sectionService->getSectionByHandle(Handles::CHANNEL);
$entryType = $section->getEntryTypes()[0];
$layout = $entryType->getFieldLayout();

Expand All @@ -47,6 +48,6 @@ public function addFields($fields)

$entryType->setFieldLayout($layout);

return Craft::$app->sections->saveEntryType($entryType);
return $sectionService->saveEntryType($entryType);
}
}
19 changes: 0 additions & 19 deletions src/services/FieldsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ public function add($name, $handle, $type, $attributes = null, $settings = null)

blogify_log("Creating field {$name}");

$groupId = $this->getFieldGroupId();
$fieldsService = Craft::$app->getFields();

$params = [
'type' => $type,
'groupId' => $groupId,
'name' => $name,
'handle' => $handle
];
Expand Down Expand Up @@ -60,21 +58,4 @@ public function remove($handle): bool

return false;
}

private function getFieldGroupId()
{
$group = FieldGroup::findOne([
'name' => Handles::BLOG_FIELDS_GROUP_NAME
]);

if ($group) {
return $group->id;
}

$group = FieldGroup::findOne('1=1');

blogify_log("Failed to find blog fields group. Using {$group->name} group instead");

return $group->id;
}
}
2 changes: 1 addition & 1 deletion src/services/IdsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function tagsGroupId()
protected function getSectionIdByHandle($handle)
{
return blogify_cache($handle, function () use ($handle) {
return (int)Craft::$app->sections->getSectionByHandle($handle)->id;
return (int)Craft::$app->getEntries()->getSectionByHandle($handle)->id;
});
}
}
20 changes: 15 additions & 5 deletions src/services/SectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
namespace matfish\Blogify\services;

use Craft;
use craft\models\EntryType;
use craft\models\Section;
use craft\models\Section_SiteSettings;

class SectionService
{

public function add($name, $handle, $type, $url, $template): bool
public function add($name, $handle, $type, $url, $template, EntryType $entryType): bool
{
blogify_log("Creating section {$name}");

$section = Craft::$app->sections->getSectionByHandle($handle);
$sectionService = Craft::$app->getEntries();
$section = $sectionService->getSectionByHandle($handle);

if ($section) {
blogify_log("Section {$name} exists. Skipping");
Expand All @@ -39,7 +41,14 @@ public function add($name, $handle, $type, $url, $template): bool
'siteSettings' => $allSitesSettings
]);

if (!Craft::$app->sections->saveSection($section)) {
$sectionService->saveEntryType($entryType);
$et = $sectionService->getEntryTypeByHandle('posts');

$section->setEntryTypes([
$et
]);

if (!$sectionService->saveSection($section)) {
blogify_log("Failed to create section {$name}");
blogify_log(json_encode($section->getErrors()));
throw new \Exception("Failed to create section {$name}");
Expand All @@ -51,11 +60,12 @@ public function add($name, $handle, $type, $url, $template): bool
public function remove($handle): bool
{
blogify_log("Removing section {$handle}");
$sectionService = Craft::$app->getEntries();

$section = Craft::$app->sections->getSectionByHandle($handle);
$section = $sectionService->getSectionByHandle($handle);

if ($section) {
return Craft::$app->sections->deleteSection($section);
return $sectionService->deleteSection($section);
}

return false;
Expand Down
12 changes: 6 additions & 6 deletions src/services/SeedingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public function seed()
$tags = $this->generateTags();
$categories = $this->generateCategories();
$images = $this->generateImages();

$section = Craft::$app->sections->getSectionByHandle(Handles::CHANNEL);
$sectionService = Craft::$app->getEntries();
$section = $sectionService->getSectionByHandle(Handles::CHANNEL);
$entryType = $section->getEntryTypes()[0];

$entry = new Entry([
Expand All @@ -32,7 +32,7 @@ public function seed()
'typeId' => $entryType->id,
'authorId' => FakerService::arrayElement($this->getUsers()),
'title' => FakerService::sentence(),
'postDate'=>FakerService::date()
'postDate' => FakerService::date()
]);

$entry->setFieldValue(Handles::POST_IMAGE, [FakerService::arrayElement($images)]);
Expand Down Expand Up @@ -111,7 +111,7 @@ private function generateImages()
$path = new Path();
$tempDirPath = $path->getTempPath();

for ($i=1; $i<=11; $i++) {
for ($i = 1; $i <= 11; $i++) {
$image = "img_{$i}";
// move file from plugin assets to project temp folder
$filename = $image . '.jpg';
Expand Down Expand Up @@ -158,8 +158,8 @@ private function uploadNewAsset($folder, string $path, $filename)

private function getUsers()
{
return array_map(function($user) {
return array_map(function ($user) {
return $user->id;
},User::findAll());
}, User::findAll());
}
}

0 comments on commit d98f871

Please sign in to comment.