Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ public function upload(): void
try {
$model->upload($data);

$item = $model->getItem($model->getState($model->getName() . '.id'));
// if no "created_by_alias" is stored in DB, we need to load creator user info (name and email) to avoid showing user id
if( $item->created_by_alias == "" ){
Comment thread
laoneo marked this conversation as resolved.
$item = (object) \array_merge((array)$item, $model->getAuthor($item->created_by));
}
$content = $this->app->bootComponent('dpattachments')->renderLayout(
'attachment.render',
['attachment' => $model->getItem($model->getState($model->getName() . '.id'))]
['attachment' => $item]
);
$returnData['html'] = '<div>' . $content . '</div>';

Expand Down
17 changes: 16 additions & 1 deletion com_dpattachments/admin/src/Model/AttachmentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Model\AdminModel;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\User\UserFactoryAwareInterface;
use Joomla\CMS\User\UserFactoryAwareTrait;
use Joomla\Filesystem\File;
use Joomla\Utilities\ArrayHelper;

class AttachmentModel extends AdminModel
class AttachmentModel extends AdminModel implements UserFactoryAwareInterface
{
use UserFactoryAwareTrait;

protected $text_prefix = 'COM_DPATTACHMENTS';

/**
Expand Down Expand Up @@ -227,4 +231,15 @@ public function delete(&$pks)

return $success;
}

public function getAuthor(int $userId): array {
$userInfo = array();
$user = $this->getUserFactory()->loadUserById($userId);
// "loadUserById" always returns a "User" object, even if no user found, no need to check for null
$userInfo['author_id'] = $user->id;
$userInfo['author_name'] = $user->name;
$userInfo['author_email'] = $user->email;

return $userInfo;
}
}