You need to be logged in to post in the forum - Log In

An active JCE Pro Subscription is required to post in the forum - Buy a Subscription

Support is currently Offline

Official support hours
Monday to Friday
09:00 - 17:00 Europe/London (BST)

Please create a new Ticket and we will get back to you as soon as we can.

#109097 Custom field JCE File Browser layout override

Posted in ‘Editor’
This is a public ticket

Everybody will be able to see its contents. Do not include usernames, passwords or any other sensitive information.

Latest post by Ryan on Wednesday, 01 February 2023 10:42 GMT

pixedbo
Hello, i'm using your custom fiel "JCE File Browser" to add files to an article. I would like to override the layout (now is "default") to show the files in a specific way. How can i override it? Or add a new layout?
Thank you.

Ryan
The layout of the JCE File Browser cannot be overriden, as it does not use the Joomla View and Layout system.

Ryan Demmer

Lead Developer / CEO / CTO

Just because you're not paranoid doesn't mean everybody isn't out to get you.

pixedbo
Ok thanks, but i can see that it has the option as others fields to choose the layout (see attachment). Is there a way to add a new one instead of overriding the existing one? I have to upload CSV but i don't need them to be downloadable but to show the contents in the article itself.
Thanks

Attachments

Ryan
Apologies I misunderstood, I though you were referring to the layout of the File Browser window itself.

The layout file is located in plugins/fields/mediajce/tmpl/mediajce.php and so you can override it in the same way as you would the core Media Field, using a template override.

Ryan Demmer

Lead Developer / CEO / CTO

Just because you're not paranoid doesn't mean everybody isn't out to get you.

pixedbo
Thanks!
Can you please write me the full path of the override i have to insert in my /template/MYTEMPLATE/ folder?
Thank you

Ryan
It might be - templates/MYTEMPLATE/html/layouts/joomla/form/field/mediajce.php

Ryan Demmer

Lead Developer / CEO / CTO

Just because you're not paranoid doesn't mean everybody isn't out to get you.

pixedbo
It works if I modify directly the file mediajce.php in /plugins/fields/mediajce/tmpl
but if i copy the php file and put it in /templates/protostar/html/layouts/joomla/form/field all modifications i made are ignored (i'm using the default protostart for testing purpose).
Do i have to activate the override somewhere in jce options?
Thanks

Ryan
Sorry, wrong path.

Copy the mediajce.php file from plugins/fields/mediajce/tmpl/ to templates/protostar/html/layouts/com_fields/field/

Make the modifications you require.

You should then be able to select it as the Layout when creating the Field.

Ryan Demmer

Lead Developer / CEO / CTO

Just because you're not paranoid doesn't mean everybody isn't out to get you.

pixedbo
Getting closer, but really sorry to bother you again.
I've copied the exact file mediajce.php here /templates/protostar/html/layouts/com_fields/field and actually i can see it in the fields layout options (see attachment):



the problem is that with the original file i get blank results. No output in the article. With default selected:



With override selected:



The file is the same, what can cause the problem?
Thanks

Attachments

Ryan
Add the following to the top of the mediajce.php file (after jimport('joomla.filesystem.path');)

if (!key_exists('field', $displayData))
{
	return;
}

$field = $displayData['field'];
$fieldParams = $field->params;

Ryan Demmer

Lead Developer / CEO / CTO

Just because you're not paranoid doesn't mean everybody isn't out to get you.

pixedbo
Still not working, sorry

Ryan
It's working in my tests.

What other changes have you made to the mediajce.php file?

Ryan Demmer

Lead Developer / CEO / CTO

Just because you're not paranoid doesn't mean everybody isn't out to get you.

Ryan
Ok I see the problem, but this looks like a Joomla bug to me, because it should be loading the original mediajce.php file if the Layout is set back to "Default", but instead it appears to be loading components/com_fields/layouts/field/render.php

Ryan Demmer

Lead Developer / CEO / CTO

Just because you're not paranoid doesn't mean everybody isn't out to get you.

pixedbo
None, is the original one from rom plugins/fields/mediajce/tmpl/ to templates/protostar/html/layouts/com_fields/field/ and added the lines of code:

Attachments

pixedbo
[quotePost id="109116"]Ok I see the problem, but this looks like a Joomla bug to me, because it should be loading the original mediajce.php file if the Layout is set back to "Default", but instead it appears to be loading components/com_fields/layouts/field/render.php[/quotePost]

Ok, so the only way to solve this right now is to modify the original in plugins/fields/mediajce/tmpl/ and remove the override and the re-apply the patch every time i update JCE?

Ryan
Ok I see the problem, but this looks like a Joomla bug to me, because it should be loading the original mediajce.php file if the Layout is set back to "Default", but instead it appears to be loading components/com_fields/layouts/field/render.php


No, I think I'm wrong about this, I'm looking into it.

Ryan Demmer

Lead Developer / CEO / CTO

Just because you're not paranoid doesn't mean everybody isn't out to get you.

pixedbo
[quotePost id="109119"]
Ok I see the problem, but this looks like a Joomla bug to me, because it should be loading the original mediajce.php file if the Layout is set back to "Default", but instead it appears to be loading components/com_fields/layouts/field/render.php


No, I think I'm wrong about this, I'm looking into it.[/quotePost]

Great, thanks! Let me know,
G.

Ryan
There appears to be some kind of Joomla bug where it processes the field trhough the default layout file first anyway, even if you have set an alternative. This happens even when using the core Media field. So to get around this, we need to create a custom override that test for this, eg:

if (!key_exists('field', $displayData))
{
	return;
}

$field = $displayData['field'];
$fieldParams = $field->params;

if ($field->value == '') {
    return;
}

$value = $field->value;

// already processed...restart
if (strpos($value, '<') == 0) {
	$value = $field->rawvalue;
}

$text = basename($value);
$text = htmlentities($text, ENT_COMPAT, 'UTF-8', true);

$buffer = '';

// example attributes...
$attribs = array('class=""');

$element = '<img src="https://www.joomlacontenteditor.net/%s"%s alt="%s" />';

// not an image, so create a link
if (!preg_match('#\.(jpeg|jpeg|png|gif)$#', $value)) {
    $element = '<a href="https://www.joomlacontenteditor.net/%s"%s>%s</a>';
    
    // add blank target for links
    $attribs['target="_blank"'];
}

$buffer .= sprintf($element,
    htmlentities($value, ENT_COMPAT, 'UTF-8', true),
    implode(' ', $attribs),
    $text
);

echo $buffer;

Ryan Demmer

Lead Developer / CEO / CTO

Just because you're not paranoid doesn't mean everybody isn't out to get you.

pixedbo
it works! Thanks!

sven.j
hi i´am in Jomla 4 and this fix doesn' t work anymore, any other solution ?

My goal is to override a pdf link to <a href="https://www.joomlacontenteditor.net/link/to/pdffile">{$fieldtitle}</a>

Ryan
@sven.j# Which template are you using?

Ryan Demmer

Lead Developer / CEO / CTO

Just because you're not paranoid doesn't mean everybody isn't out to get you.

sven.j
my own :D

i think i put it in my article override

Ryan
Create the file templates/your_template/html/plg_fields_mediajce/mediajce.php and add the following content (editied for your uses):

<?php

defined('_JEXEC') or die;

use Joomla\Utilities\ArrayHelper;

if (empty($field->value) || empty($field->value['media_src']))
{
	return;
}

$value = $field->value['media_src'];

$text = basename($value);
$text = htmlentities($text, ENT_COMPAT, 'UTF-8', true);

$buffer = '';

// example attributes...
$attribs = array();

$element = '<img src="https://www.joomlacontenteditor.net/%s"%s alt="%s" />';

// not an image, so create a link
if (!preg_match('#\.(jpeg|jpg|png|gif)$#', $value)) {
    $element = '<a href="https://www.joomlacontenteditor.net/%s"%s>%s</a>';
    
    // add blank target for links
    $attribs['target'] = "_blank";
}

$buffer .= sprintf($element,
    htmlentities($value, ENT_COMPAT, 'UTF-8', true),
    ArrayHelper::toString($attribs),
    $text
);

echo $buffer;

Ryan Demmer

Lead Developer / CEO / CTO

Just because you're not paranoid doesn't mean everybody isn't out to get you.