Skip to content

Commit f58c5b3

Browse files
authored
Merge pull request #341 from magefan/4301-blog-with-deleted-author
Add check if customer exists
2 parents 494b626 + d28ef36 commit f58c5b3

File tree

2 files changed

+63
-41
lines changed

2 files changed

+63
-41
lines changed

Model/Comment.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,30 +179,40 @@ public function getAuthor()
179179
$this->getAuthorType()
180180
);
181181

182+
$guestData = [
183+
'nickname' => $this->getAuthorNickname(),
184+
'email' => $this->getAuthorEmail(),
185+
];
186+
182187
switch ($this->getAuthorType()) {
183188
case \Magefan\Blog\Model\Config\Source\AuthorType::GUEST:
184-
$this->author->setData([
185-
'nickname' => $this->getAuthorNickname(),
186-
'email' => $this->getAuthorEmail(),
187-
]);
189+
$this->author->setData($guestData);
188190
break;
189191
case \Magefan\Blog\Model\Config\Source\AuthorType::CUSTOMER:
190192
$customer = $this->customerFactory->create();
191193
$customer->load($this->getCustomerId());
192-
$this->author->setData([
193-
'nickname' => $customer->getName(),
194-
'email' => $this->getEmail(),
195-
'customer' => $customer,
196-
]);
194+
if ($customer->getId()) {
195+
$this->author->setData([
196+
'nickname' => $customer->getName(),
197+
'email' => $this->getEmail(),
198+
'customer' => $customer,
199+
]);
200+
} else {
201+
$this->author->setData($guestData);
202+
}
197203
break;
198204
case \Magefan\Blog\Model\Config\Source\AuthorType::ADMIN:
199205
$admin = $this->userFactory->create();
200206
$admin->load($this->getAdminId());
201-
$this->author->setData([
202-
'nickname' => $admin->getName(),
203-
'email' => $this->getEmail(),
204-
'admin' => $admin,
205-
]);
207+
if ($admin->getId()) {
208+
$this->author->setData([
209+
'nickname' => $admin->getName(),
210+
'email' => $this->getEmail(),
211+
'admin' => $admin,
212+
]);
213+
} else {
214+
$this->author->setData($guestData);
215+
}
206216
break;
207217
}
208218
}

Ui/DataProvider/Comment/Form/CommentDataProvider.php

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -108,39 +108,51 @@ public function getData()
108108
];
109109

110110
$author = $comment->getAuthor();
111+
$guestData = [
112+
'url' => 'mailto:' . $author->getEmail(),
113+
'title' => $author->getNickname(),
114+
'text' => $author->getNickname() .
115+
' - ' . $author->getEmail() .
116+
' (' . __('Guest') . ')',
117+
];
118+
111119
switch ($comment->getAuthorType()) {
112120
case \Magefan\Blog\Model\Config\Source\AuthorType::GUEST:
113-
$this->loadedData[$comment->getId()]['author_url'] = [
114-
'url' => 'mailto:' . $author->getEmail(),
115-
'title' => $author->getNickname(),
116-
'text' => $author->getNickname() .
117-
' - ' . $author->getEmail() .
118-
' (' . __('Guest') . ')',
119-
];
121+
$this->loadedData[$comment->getId()]['author_url'] = $guestData;
120122
break;
121123
case \Magefan\Blog\Model\Config\Source\AuthorType::CUSTOMER:
122-
$this->loadedData[$comment->getId()]['author_url'] = [
123-
'url' => $this->url->getUrl(
124-
'customer/index/edit',
125-
['id' => $comment->getCustomerId()]
126-
),
127-
'title' => $author->getNickname(),
128-
'text' => '#' . $comment->getCustomerId() .
129-
'. ' . $author->getNickname() .
130-
' (' . __('Customer') . ')',
131-
];
124+
125+
if ($author->getCustomer()) {
126+
$this->loadedData[$comment->getId()]['author_url'] = [
127+
'url' => $this->url->getUrl(
128+
'customer/index/edit',
129+
['id' => $comment->getCustomerId()]
130+
),
131+
'title' => $author->getNickname(),
132+
'text' => '#' . $comment->getCustomerId() .
133+
'. ' . $author->getNickname() .
134+
' (' . __('Customer') . ')',
135+
];
136+
} else {
137+
$this->loadedData[$comment->getId()]['author_url'] = $guestData;
138+
}
139+
132140
break;
133141
case \Magefan\Blog\Model\Config\Source\AuthorType::ADMIN:
134-
$this->loadedData[$comment->getId()]['author_url'] = [
135-
'url' => $this->url->getUrl(
136-
'admin/user/edit',
137-
['id' => $comment->getAdminId()]
138-
),
139-
'title' => $author->getNickname(),
140-
'text' => '#' . $comment->getAdminId() .
141-
'. ' . $author->getNickname() .
142-
' (' . __('Admin') . ')',
143-
];
142+
if ($author->getAdmin()) {
143+
$this->loadedData[$comment->getId()]['author_url'] = [
144+
'url' => $this->url->getUrl(
145+
'admin/user/edit',
146+
['id' => $comment->getAdminId()]
147+
),
148+
'title' => $author->getNickname(),
149+
'text' => '#' . $comment->getAdminId() .
150+
'. ' . $author->getNickname() .
151+
' (' . __('Admin') . ')',
152+
];
153+
} else {
154+
$this->loadedData[$comment->getId()]['author_url'] = $guestData;
155+
}
144156
break;
145157
}
146158

0 commit comments

Comments
 (0)