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

Update getContentTypeName(), add getContentTypeSingularName() #1468

Merged
merged 1 commit into from
Jun 12, 2020
Merged
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
1 change: 0 additions & 1 deletion config/bolt/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ search_results_records: 10
# for each content type.
records_per_page: 8


# Default settings for thumbnails.
#
# quality: Quality should be between 0 (horrible, small file) and 100 (best, huge file).
Expand Down
11 changes: 10 additions & 1 deletion src/Entity/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function __construct(?ContentType $contentTypeDefinition = null)

public function __toString(): string
{
$contentName = $this->getDefinition() ? $this->getContentTypeName() : 'Content';
$contentName = $this->getDefinition() ? $this->getContentTypeSingularName() : 'Content';
if ($this->getId()) {
return sprintf('%s #%d', $contentName, $this->getId());
}
Expand Down Expand Up @@ -283,6 +283,15 @@ public function getContentTypeName(): string
throw new \RuntimeException('Content not fully initialized');
}

return $this->getDefinition()->get('name') ?: $this->getContentTypeSlug();
}

public function getContentTypeSingularName(): string
{
if ($this->getDefinition() === null) {
throw new \RuntimeException('Content not fully initialized');
}

return $this->getDefinition()->get('singular_name') ?: $this->getContentTypeSlug();
}

Expand Down
13 changes: 3 additions & 10 deletions src/Event/Listener/ContentFillListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Bolt\Repository\UserRepository;
use Bolt\Twig\ContentExtension;
use Doctrine\ORM\Event\LifecycleEventArgs;
use RuntimeException;

class ContentFillListener
{
Expand Down Expand Up @@ -58,7 +57,7 @@ public function prePersist(LifecycleEventArgs $args): void

if ($entity instanceof Content) {
if ($entity->getAuthor() === null) {
$entity->setAuthor($this->guesstimateAuthor($entity->getContentTypeName()));
$entity->setAuthor($this->guesstimateAuthor());
}

if ($entity->getPublishedAt() === null && $entity->getStatus() === Statuses::PUBLISHED) {
Expand All @@ -82,14 +81,8 @@ public function fillContent(Content $entity): void
$entity->setContentExtension($this->contentExtension);
}

private function guesstimateAuthor($contenttype): User
private function guesstimateAuthor(): User
{
$user = $this->users->getFirstAdminUser();

if ($user === null) {
throw new RuntimeException('Error persisting record of type ' . $contenttype . ' without author. Could not guesstimate author.');
}

return $user;
return $this->users->getFirstAdminUser();
}
}