Skip to content

Commit

Permalink
[4.0][api][com_content] render the image field (#29630)
Browse files Browse the repository at this point in the history
  • Loading branch information
alikon committed Jun 19, 2020
1 parent d9cfd73 commit 4aedffb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
Expand Up @@ -260,6 +260,7 @@ protected function getListQuery()
$db->quoteName('a.introtext'),
$db->quoteName('a.fulltext'),
$db->quoteName('a.note'),
$db->quoteName('a.images'),
]
)
)
Expand Down
40 changes: 40 additions & 0 deletions api/components/com_content/src/Helper/ContentHelper.php
@@ -0,0 +1,40 @@
<?php
/**
* @package Joomla.Api
* @subpackage com_content
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Component\Content\Api\Helper;

\defined('_JEXEC') or die;

use Joomla\CMS\Uri\Uri;

/**
* Content api helper.
*
* @since 4.0
*/
class ContentHelper
{
/**
* Fully Qualified Domain name for the image url
*
* @param string $uri The uri to resolve
*
* @return string
*/
public static function resolve(string $uri): string
{
// Check if external URL.
if (stripos($uri, 'http') !== 0)
{
return Uri::root() . $uri;
}

return $uri;
}
}
20 changes: 20 additions & 0 deletions api/components/com_content/src/View/Articles/JsonapiView.php
Expand Up @@ -15,8 +15,10 @@
use Joomla\CMS\Language\Multilanguage;
use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Component\Content\Api\Helper\ContentHelper;
use Joomla\Component\Content\Api\Serializer\ContentSerializer;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
use Joomla\Registry\Registry;

/**
* The article view
Expand All @@ -43,6 +45,7 @@ class JsonapiView extends BaseApiView
'category',
'created',
'author',
'images',
];

/**
Expand All @@ -63,6 +66,7 @@ class JsonapiView extends BaseApiView
'category',
'created',
'author',
'images',
];

/**
Expand Down Expand Up @@ -189,6 +193,22 @@ protected function prepareItem($item)
$item->tags = [];
}

if (isset($item->images))
{
$registry = new Registry($item->images);
$item->images = $registry->toArray();

if (!empty($item->images['image_intro']))
{
$item->images['image_intro'] = ContentHelper::resolve($item->images['image_intro']);
}

if (!empty($item->images['image_fulltext']))
{
$item->images['image_fulltext'] = ContentHelper::resolve($item->images['image_fulltext']);
}
}

return parent::prepareItem($item);
}
}

0 comments on commit 4aedffb

Please sign in to comment.