Skip to content

Commit

Permalink
Add authorInfo module for solo installs
Browse files Browse the repository at this point in the history
  • Loading branch information
louderthan10 committed Nov 21, 2024
1 parent cde9eb5 commit 9965bf3
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 3 deletions.
3 changes: 2 additions & 1 deletion backend/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
},
"autoload": {
"psr-4": {
"modules\\": "modules/"
"modules\\": "modules/",
"backend\\": "."
}
},
"config": {
Expand Down
4 changes: 4 additions & 0 deletions backend/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@

return [
'id' => App::env('CRAFT_APP_ID') ?: 'CraftCMS',
'modules' => [
'authorinfo' => modules\authorinfo\Module::class,
],
'bootstrap' => ['authorinfo'],
];
100 changes: 100 additions & 0 deletions backend/modules/authorinfo/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
namespace modules\authorinfo;

use Craft;
use craft\elements\User;
use craft\events\DefineGqlTypeFieldsEvent;
use craft\gql\TypeManager;
use GraphQL\Type\Definition\Type;
use yii\base\Event;

class Module extends \yii\base\Module
{
public static $instance;

/**
* Cache for the admin user to avoid multiple queries
*/
private ?User $adminUser = null;

public function init()
{
parent::init();
self::$instance = $this;

Event::on(
TypeManager::class,
TypeManager::EVENT_DEFINE_GQL_TYPE_FIELDS,
[$this, 'handleDefineGqlTypeFields']
);
}

/**
* Get the admin user, caching the result
*/
private function getAdminUser(): ?User
{
if ($this->adminUser === null) {
$this->adminUser = User::find()
->admin(true)
->status(null)
->orderBy(['elements.id' => SORT_ASC])
->one();

if ($this->adminUser) {
Craft::info(
"Found admin user: ID: {$this->adminUser->id}, Name: {$this->adminUser->fullName}",
__METHOD__
);
} else {
Craft::warning('No admin user found', __METHOD__);
}
}

return $this->adminUser;
}

/**
* Handle the DefineGqlTypeFields event
*/
public function handleDefineGqlTypeFields(DefineGqlTypeFieldsEvent $event): void
{
if ($event->typeName !== 'EntryInterface') {
return;
}

$event->fields['authorId'] = [
'name' => 'authorId',
'type' => Type::int(),
'description' => 'The entry author\'s ID',
'resolve' => function($source) {
try {
if ($source->getSection()->type === 'single') {
return $this->getAdminUser()?->id;
}
return $source->authorId;
} catch (\Throwable $e) {
Craft::error("Error resolving authorId: {$e->getMessage()}", __METHOD__);
return null;
}
}
];

$event->fields['authorName'] = [
'name' => 'authorName',
'type' => Type::string(),
'description' => 'The entry author\'s name',
'resolve' => function($source) {
try {
if ($source->getSection()->type === 'single') {
return $this->getAdminUser()?->fullName;
}
return $source->getAuthor()?->fullName;
} catch (\Throwable $e) {
Craft::error("Error resolving authorName: {$e->getMessage()}", __METHOD__);
return null;
}
}
];
}
}
3 changes: 1 addition & 2 deletions frontend/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ export default defineNuxtConfig({
AUTH_HEADER: process.env.AUTH_HEADER,
CRAFT_URL: process.env.CRAFT_URL,
BASE_URL: process.env.BASE_URL,
SITE_NAME: process.env.SITE_NAME,
ADMIN_USER_ID: process.env.ADMIN_USER_ID
SITE_NAME: process.env.SITE_NAME
}
},
vite: {
Expand Down
2 changes: 2 additions & 0 deletions frontend/queries/blogPosts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const BLOG_POSTS_QUERY = `
title
pageSubheading
pageContent
authorName
authorId
postDate @formatDateTime(format: "F j, Y")
image {
alt
Expand Down

0 comments on commit 9965bf3

Please sign in to comment.