Skip to content

Commit 000fdd1

Browse files
committed
Metadata.getPreviewText added + getDescription text crop added
1 parent bd9d1ba commit 000fdd1

File tree

2 files changed

+43
-31
lines changed

2 files changed

+43
-31
lines changed

lib/Type/Structure/Metadata.php

+42-30
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use DateTimeInterface;
66
use Exception;
7+
use Kreatif\Model;
8+
use Kreatif\Utils;
79
use RexGraphQL\Type\Media\Media;
810
use rex;
911
use rex_article;
@@ -15,6 +17,7 @@
1517
use TheCodingMachine\GraphQLite\Exceptions\GraphQLException;
1618
use Url\UrlManager;
1719

20+
1821
#[Type]
1922
class Metadata
2023
{
@@ -24,19 +27,26 @@ class Metadata
2427
private ?string $canonical;
2528
private ?string $image;
2629

27-
private ?int $createdAt;
28-
private ?int $updatedAt;
30+
private ?int $createdAt;
31+
private ?int $updatedAt;
2932
public ?string $type = 'article';
3033

31-
public function __construct(string $title = null, string $description = null, string $robots = null, string $canonical = null, string $image = null, ?int $createdAt = null, ?int $updatedAt = null)
32-
{
33-
$this->title = $title;
34+
public function __construct(
35+
string $title = null,
36+
string $description = null,
37+
string $robots = null,
38+
string $canonical = null,
39+
string $image = null,
40+
?int $createdAt = null,
41+
?int $updatedAt = null
42+
) {
43+
$this->title = $title;
3444
$this->description = $description;
35-
$this->robots = $robots;
36-
$this->canonical = $canonical;
37-
$this->image = $image;
38-
$this->createdAt = $createdAt;
39-
$this->updatedAt = $updatedAt;
45+
$this->robots = $robots;
46+
$this->canonical = $canonical;
47+
$this->image = $image;
48+
$this->createdAt = $createdAt;
49+
$this->updatedAt = $updatedAt;
4050
}
4151

4252
#[Field]
@@ -48,7 +58,15 @@ public function getTitle(): string
4858
#[Field]
4959
public function getDescription(): string
5060
{
51-
return $this->description;
61+
// shorten the description to 160 characters
62+
return Utils::cropText($this->description, 160);
63+
}
64+
65+
#[Field]
66+
public function getPreviewText(int $wordCount = 200, string $suffix = '...', string $allowedTags = '<p><strong>'): string
67+
{
68+
// crop description to $wordCount words and close open tags
69+
return Utils::cropText($this->description, $wordCount, $suffix, $allowedTags);
5270
}
5371

5472
#[Field]
@@ -113,11 +131,9 @@ public static function getByArticleId(int $elementId, int $clangId): self
113131
if (!$article) {
114132
throw new GraphQLException('Article not found');
115133
}
116-
$seo = new rex_yrewrite_seo($article->getId(), $clangId);
134+
$seo = new rex_yrewrite_seo($article->getId(), $clangId);
117135
$robots = 'noindex, nofollow';
118-
$index = $article->getValue(
119-
rex_yrewrite_seo::$meta_index_field,
120-
) ?? rex_yrewrite_seo::$index_setting_default;
136+
$index = $article->getValue(rex_yrewrite_seo::$meta_index_field,) ?? rex_yrewrite_seo::$index_setting_default;
121137
if (1 == $index || (0 == $index && $article->isOnline())) {
122138
$robots = 'index, follow';
123139
} elseif (2 == $index) {
@@ -139,35 +155,31 @@ public static function getByArticleId(int $elementId, int $clangId): self
139155
/**
140156
* @throws GraphQLException|rex_exception if the url object is not found
141157
*/
142-
public static function getByUrlObject(): self
158+
public static function getByUrlObject($modelObject = null): ?self
143159
{
144-
/** @var UrlManager $urlObject */
145-
$urlObject = rex::getProperty('url_object');
160+
if ($modelObject && $modelObject instanceof Model) {
161+
$urlObject = $modelObject->getUrlObject();
162+
} else {
163+
/** @var UrlManager $urlObject */
164+
$urlObject = rex::getProperty('url_object');
165+
}
146166
if (!$urlObject) {
147-
throw new GraphQLException('Url object not found');
167+
return null;
148168
}
149169
$createdAt = strtotime($urlObject->getValue('createdate'));
150170
$updatedAt = strtotime($urlObject->getValue('updatedate'));
151-
$title = htmlspecialchars_decode(trim(rex_yrewrite::getCurrentDomain()->getTitle()));
171+
$title = htmlspecialchars_decode(trim(rex_yrewrite::getCurrentDomain()->getTitle()));
152172
if ('' == $title) {
153173
$title = rex_yrewrite_seo::$title_scheme_default;
154174
}
155175
$title = str_replace('%T', $urlObject->getSeoTitle(), $title);
156176
$title = str_replace('%SN', rex::getServerName(), $title);
157177

158178
$image = $urlObject->getSeoImage();
159-
if($image) {
179+
if ($image) {
160180
$image = explode(',', $image)[0];
161181
}
162-
$item = new self(
163-
$title,
164-
$urlObject->getSeoDescription(),
165-
'index, follow',
166-
'',
167-
$image,
168-
$createdAt,
169-
$updatedAt,
170-
);
182+
$item = new self($title, $urlObject->getSeoDescription(), 'index, follow', '', $image, $createdAt, $updatedAt,);
171183
$item->type = $urlObject->getProfile()->getNamespace();
172184
return $item;
173185
}

package.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package: graphql
22
author: Friends Of REDAXO
3-
version: '0.0.1'
3+
version: '1.0.0'
44

55
page:
66
title: GraphQL

0 commit comments

Comments
 (0)