Skip to content

Commit

Permalink
Merge pull request #1468 from bolt/fix/contentypename
Browse files Browse the repository at this point in the history
Update `getContentTypeName()`, add `getContentTypeSingularName()`
  • Loading branch information
I-Valchev authored Jun 12, 2020
2 parents 733618f + c133712 commit 26eabd4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
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();
}
}

0 comments on commit 26eabd4

Please sign in to comment.