Skip to content

Commit

Permalink
[4.0] Fix image preview for media form field (#34833)
Browse files Browse the repository at this point in the history
  • Loading branch information
joomdonation committed Jul 23, 2021
1 parent 16d2bd2 commit d7f8b3f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
14 changes: 11 additions & 3 deletions layouts/joomla/form/field/media.php
Expand Up @@ -11,6 +11,7 @@

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Helper\MediaHelper;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
Expand Down Expand Up @@ -73,12 +74,19 @@
}

// Pre fill the contents of the popover
if ($showPreview) {
if ($value && file_exists(JPATH_ROOT . '/' . $value)) {
if ($showPreview)
{
$cleanValue = MediaHelper::getCleanMediaFieldValue($value);

if ($cleanValue && file_exists(JPATH_ROOT . '/' . $cleanValue))
{
$src = Uri::root() . $value;
} else {
}
else
{
$src = '';
}

$width = $previewWidth;
$height = $previewHeight;
$style = '';
Expand Down
21 changes: 21 additions & 0 deletions libraries/src/Helper/MediaHelper.php
Expand Up @@ -464,4 +464,25 @@ public static function isValidLocalDirectory($directory)

return false;
}

/**
* Helper method get clean data for value stores in a Media form field by removing adapter information
* from the value if available (in this case, the value will have this format:
* images/headers/blue-flower.jpg#joomlaImage://local-images/headers/blue-flower.jpg?width=700&height=180)
*
* @param string $value
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
public static function getCleanMediaFieldValue($value)
{
if ($pos = strpos($value, '#'))
{
return substr($value, 0, $pos);
}

return $value;
}
}

0 comments on commit d7f8b3f

Please sign in to comment.