Search
Other Sourcerer questions
Forum

How to deal with PHP API functions giving <tag> instead of [[tag]] in their return output?

Nifa's Avatar Nifa
Hi,

Using the incredible Sourcerer, I got used to preventing tags from being stripped phenomenon. So within my Sourcerer PHP code I use the double bracket syntax, [[tag]] instead of <tag>.

However, now I want to use a third-party API function, which returns a value with HTML-code. A display of that value is giving no output, most likely because Sourcerer stripped the <tag>'s inside that returned value.

Do you have any idea how to solve this? How can I use and display that returnde value (with <tag>'s inside), when the complete PHP code runs in Sourcerer?

Many thanks in advance,
Nico
Peter van Westen's Avatar Peter van Westen ADMIN
Sorry, I don't understand completely what you mean.
Where are you placing the {source} code?
And how does that relate to that 3rd party API?
Can you show me the code?
Please post a rating at the Joomla! Extensions Directory
Nifa's Avatar Nifa
Hoi Peter,

Sorry, I find it hard to explain, but will give it another try.

For example, I want to use this API www.joomlapolis.com/documentation/279-co...hrough-getfields-api in a module using Sourcerer->PHP.

The last line of the page above gives exact what I want.
$avatar = $cbUser->getField( 'avatar', null, 'html', 'none', 'list' );
It returns a full HTML enriched avatar code, if avatar is available or else a nice SVG code, for examples see below.

1. For users with an uploaded avatar, the $avatar value will return something like:
<img src="https://mywebsite.nl/images/comprofiler/tn1737_62e7f1d8785a7.jpg" alt="Deelnemer 1" title="Deelnemer 1" class="cbImgPict cbThumbPict img-thumbnail cbImgAvatar">
2. For users, for example Peter van Western, with no avatar the API will return a svg-code with their initials:
<svg viewBox="0 0 100 100" class="cbImgPict cbImgPictInitial cbThumbPict img-thumbnail cbImgAvatar"><rect fill="#6C007F" width="100" height="100" cx="50" cy="50" r="50"></rect><text x="50%" y="50%" style="color: #ffffff; line-height: 1;" alignment-baseline="middle" text-anchor="middle" font-size="40" font-weight="600" dy="0.1em" dominant-baseline="middle" fill="#ffffff">PW</text></svg>

The code runs without error. PHP test gives in all cases $avatar is not empty. However, in first case (echo $avatar;) gives no output at all and in the second case it only echo users initials (here PW) as that is the only value that is not inside any <>.

I know from experience that sourcerer can remove everything within any <>, and I think this is the reason why I cannot use the API output within Sourcerer.

The API call seems to work fine because when I choose raw output, which doesn't contain any <> it gives the expected output
For example:
$avatar = $cbUser->getField( 'avatar', false, false );
Which gives for users with an avatar set the URL of that avatar image file. But this doesn't create the SVG for all users without an avatar. That is one of the reassons why I really want to use the full HTML CB API call.

In answer to your last question, the essential code part:
{source}
<?php
global $_CB_framework;

$db =& JFactory::getDBO();
$query = 'SELECT user_id from #__comprofiler limit 3';
$db->setQuery($query);
$result = $db->query();

while($row = mysqli_fetch_assoc( $result ))
{
$cbUser		=	CBuser::getInstance($row['user_id'], false );
$avatar 	= 	$cbUser->getField( 'avatar', null, 'html', 'none', 'list' );
echo $avatar ;
?>
{/source}

I hope you can help.

With kind regards, Nico
Peter van Westen's Avatar Peter van Westen ADMIN
The stripping of tags is just an editor thing, that only applies to the code you actually place in the editor (so your actual code between the {source} tags.
It does not affect anything the PHP code should output on the frontend.

I recommend you output the value of $avatar via print for testing purposes, and check the HTML output of the frontend to see what it actually outputs:
echo "\n\n<pre>==========================\n";
print_r($avatar);
echo "\n==========================</pre>\n\n";
Please post a rating at the Joomla! Extensions Directory
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription