Custom Fields - URL Button Topic is solved

General questions relating to Joomla! 3.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Post Reply
bins_uk
Joomla! Intern
Joomla! Intern
Posts: 66
Joined: Tue Sep 04, 2007 11:53 am

Custom Fields - URL Button

Post by bins_uk » Thu Oct 29, 2020 1:29 pm

Hiya,

I am creating some custom fields to use with articles and one of them will be a url.

Rather than display the URL, how can I have a button inserted with the link?

I am using Helix framework and cannot fine any pointers anywhere!!!

TIA.

User avatar
bruno28
Joomla! Guru
Joomla! Guru
Posts: 515
Joined: Wed May 16, 2012 5:41 am
Location: Chartres, France
Contact:

Re: Custom Fields - URL Button

Post by bruno28 » Thu Oct 29, 2020 3:33 pm

bins_uk wrote:
Thu Oct 29, 2020 1:29 pm
Hiya,

I am creating some custom fields to use with articles and one of them will be a url.

Rather than display the URL, how can I have a button inserted with the link?

I am using Helix framework and cannot fine any pointers anywhere!!!

TIA.
Hello bins_uk

Have a look here : https://www.tassos.gr/joomla-extensions ... tom-fields

Bruno
Cordialement, Bruno28 :)
Joomla! 3.9.23 - php 7.4
www.bp2i.fr - www.moneglisesurle.net

bins_uk
Joomla! Intern
Joomla! Intern
Posts: 66
Joined: Tue Sep 04, 2007 11:53 am

Re: Custom Fields - URL Button

Post by bins_uk » Thu Oct 29, 2020 4:58 pm

Hiya,

thanks for the pointer - I have already tried this and there isn't a button option or guidance.

annahersh
Joomla! Guru
Joomla! Guru
Posts: 713
Joined: Wed Aug 15, 2018 8:23 pm

Re: Custom Fields - URL Button

Post by annahersh » Thu Oct 29, 2020 4:59 pm

bins_uk wrote:
Thu Oct 29, 2020 1:29 pm
Rather than display the URL, how can I have a button inserted with the link?
Create a template override of plg_fields_url and format the display as desired.

Example

Code: Select all

<?php defined('_JEXEC') or die;

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

$attributes = '';
if( !JUri::isInternal($field->value) ) 
	$attributes = ' rel="nofollow noopener noreferrer" target="_blank"';

echo '<a href="'.htmlspecialchars($field->value).'"'.$attributes.'>Visit This Website</a>';

User avatar
bruno28
Joomla! Guru
Joomla! Guru
Posts: 515
Joined: Wed May 16, 2012 5:41 am
Location: Chartres, France
Contact:

Re: Custom Fields - URL Button

Post by bruno28 » Thu Oct 29, 2020 5:12 pm

bins_uk wrote:
Thu Oct 29, 2020 4:58 pm
Hiya,

thanks for the pointer - I have already tried this and there isn't a button option or guidance.
Do you want something like this : https://www.eglise-albertville2.passera ... tions-2018 ?

Bruno
Cordialement, Bruno28 :)
Joomla! 3.9.23 - php 7.4
www.bp2i.fr - www.moneglisesurle.net

bins_uk
Joomla! Intern
Joomla! Intern
Posts: 66
Joined: Tue Sep 04, 2007 11:53 am

Re: Custom Fields - URL Button

Post by bins_uk » Thu Oct 29, 2020 6:55 pm

@annahersh
thanks, I changed the copy for an image and this worked.

Whilst this will not affect me, adding the image somewhere in the field setup would be better, otherwise if I had more than one URL field and needed words, not an image, I think I would have an issue!

@bruno28
Yes, that looks like what I want (and have got).

Bottom line is that adding something into Render Class seems more logical!

annahersh
Joomla! Guru
Joomla! Guru
Posts: 713
Joined: Wed Aug 15, 2018 8:23 pm

Re: Custom Fields - URL Button

Post by annahersh » Thu Oct 29, 2020 8:04 pm

bins_uk wrote:
Thu Oct 29, 2020 6:55 pm
Whilst this will not affect me, adding the image somewhere in the field setup would be better, otherwise if I had more than one URL field and needed words, not an image, I think I would have an issue!
Every aspect of coding logic can be handled with a conditional query, and Joomla's excellent override system gives unlimited flexibility.
com-fields-override.jpg
You can create many field output formats and select per field. The conditionals can be inserted based on a variety of inputs.

Make an override of com_fields layout (best to use the Joomla template override function).
yourtpl > html > layouts > com_fields > field

File - yourfieldrender.php

Code: Select all

<?php
/**
 * @package     Joomla.Site
 * @subpackage  com_fields
 *
 * @copyright   Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */
defined('_JEXEC') or die;

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

$field = $displayData['field'];
$label = JText::_($field->label);
$value = $field->value;
$showLabel = $field->params->get('showlabel');
$labelClass = $field->params->get('label_render_class');

// add render class to insert an output class
$renderclass = $field->params->get('render_class');

if ($value == '')
{
	return;
}

/* set a condition if the field is checkbox
* just to allow normal rendering of other fields
* which may use this override
*/
if( $field->type == 'checkboxes' ) 
{
	$vals = explode(', ', $value);
	$value = '<ul>';
	foreach($vals as $val) {
		$value .= '<li>'.$val.'</li>';
	}
	$value .= '</ul>';
}
if( $field->type == 'url' ) {
// do this other format
}
if( $field->type == 'text' ) {
// do some great format
}

?>
<?php if ($showLabel == 1) : ?>
	<span class="field-label <?php echo $labelClass; ?>"><?php echo htmlentities($label, ENT_QUOTES | ENT_IGNORE, 'UTF-8'); ?>: </span>
<?php endif; ?>

<div class="field-value<?php echo (!empty($renderclass) ? ' '.$renderclass : ''); ?>">
<?php echo $value; ?>
</div>
Essentially the forward thinking minds of the Joomla development core have given users the ability to construct any website display desired.
You do not have the required permissions to view the files attached to this post.

User avatar
bruno28
Joomla! Guru
Joomla! Guru
Posts: 515
Joined: Wed May 16, 2012 5:41 am
Location: Chartres, France
Contact:

Re: Custom Fields - URL Button

Post by bruno28 » Fri Oct 30, 2020 6:34 am

bins_uk wrote:
Thu Oct 29, 2020 6:55 pm
@annahersh
thanks, I changed the copy for an image and this worked.

Whilst this will not affect me, adding the image somewhere in the field setup would be better, otherwise if I had more than one URL field and needed words, not an image, I think I would have an issue!

@bruno28
Yes, that looks like what I want (and have got).

Bottom line is that adding something into Render Class seems more logical!
Super it works for you

Have a good day

Bruno
Cordialement, Bruno28 :)
Joomla! 3.9.23 - php 7.4
www.bp2i.fr - www.moneglisesurle.net


Post Reply

Return to “General Questions/New to Joomla! 3.x”