Skip to content

Commit

Permalink
PHP 8.1 compatability on post save in admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorvansach committed May 10, 2022
1 parent 1aa9cbd commit b1a81ca
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Controller/Adminhtml/Post/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,27 +114,34 @@ protected function _beforeSave($model, $request)
}

/* Prepare Tags */
$tagInput = trim($request->getPost('tag_input'));
$tagInput = trim((string)$request->getPost('tag_input'));
if ($tagInput) {
$tagInput = explode(',', $tagInput);

$tagsCollection = $this->_objectManager->create(\Magefan\Blog\Model\ResourceModel\Tag\Collection::class);
$allTags = [];
foreach ($tagsCollection as $item) {
$allTags[strtolower($item->getTitle())] = $item->getId();
if (!$item->getTitle()) {
continue;
}
$allTags[strtolower((string)$item->getTitle())] = $item->getId();
}

$tags = [];
foreach ($tagInput as $tagTitle) {
if (empty($allTags[strtolower($tagTitle)])) {
$tagTitle = strtolower(trim((string)$tagTitle));
if (!$tagTitle) {
continue;
}
if (empty($allTags[$tagTitle])) {
$tagModel = $this->_objectManager->create(\Magefan\Blog\Model\Tag::class);
$tagModel->setData('title', $tagTitle);
$tagModel->setData('is_active', 1);
$tagModel->save();

$tags[] = $tagModel->getId();
} else {
$tags[] = $allTags[strtolower($tagTitle)];
$tags[] = $allTags[$tagTitle];
}
}
$model->setData('tags', $tags);
Expand Down

0 comments on commit b1a81ca

Please sign in to comment.