How to show user image in the logout module? Topic is solved

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Post Reply
User avatar
camerata
Joomla! Apprentice
Joomla! Apprentice
Posts: 21
Joined: Sun Jun 07, 2009 12:02 pm

How to show user image in the logout module?

Post by camerata » Thu Sep 29, 2022 4:08 am

I would like to show some user fields like profile image in the logout module. Just I have a template custom module but I need to get these user field values.

Thanks in advance.

User avatar
ceford
Joomla! Ace
Joomla! Ace
Posts: 1382
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: How to show user image in the logout module?

Post by ceford » Thu Sep 29, 2022 5:13 pm

Create a login module override. Open the default_logout.php file - you will see various uses of $user->get()

Can you get the user profile image url from that? There is no default profile image so I guess you added that field type.

User avatar
camerata
Joomla! Apprentice
Joomla! Apprentice
Posts: 21
Joined: Sun Jun 07, 2009 12:02 pm

Re: How to show user image in the logout module?

Post by camerata » Thu Sep 29, 2022 5:25 pm

Ok. Thanks.
The correct question would be: How to display a user custom field value in the logout module?

ceford wrote:
Thu Sep 29, 2022 5:13 pm
Create a login module override. Open the default_logout.php file - you will see various uses of $user->get()

Can you get the user profile image url from that? There is no default profile image so I guess you added that field type.

User avatar
camerata
Joomla! Apprentice
Joomla! Apprentice
Posts: 21
Joined: Sun Jun 07, 2009 12:02 pm

Re: How to show user image in the logout module?

Post by camerata » Sat Oct 01, 2022 11:33 pm

Help, please.
Doesn't anybody know how to display (user) custom fields?

Thanks.

User avatar
ceford
Joomla! Ace
Joomla! Ace
Posts: 1382
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: How to show user image in the logout module?

Post by ceford » Sun Oct 02, 2022 5:23 am

You get the user like this:

$user = Factory::getUser();

and you can get any parameter with

$user->getParam('name_of_parameter')

So for example $user->getParam('editor') will return the users preferred editor if something other than the default has been selected.

User avatar
camerata
Joomla! Apprentice
Joomla! Apprentice
Posts: 21
Joined: Sun Jun 07, 2009 12:02 pm

Re: How to show user image in the logout module?

Post by camerata » Sun Oct 02, 2022 6:12 am

Ok. It's clear how to get the user fields and params. But the question is how to get the 'custom' fields values. The documentations is not too clear or I don't find the correct information.

Could you help me to research about this?

ceford wrote:
Sun Oct 02, 2022 5:23 am
You get the user like this:

$user = Factory::getUser();

and you can get any parameter with

$user->getParam('name_of_parameter')

So for example $user->getParam('editor') will return the users preferred editor if something other than the default has been selected.

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 15260
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: How to show user image in the logout module?

Post by toivo » Sun Oct 02, 2022 6:58 am

The following lines added to the login module override display a custom media field 'photo'.

Code: Select all

// custom code to access custom field 'photo'
use Joomla\CMS\Factory;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
$customFields 	= FieldsHelper::getFields('com_users.user', Factory::getUser(), true); 
$photo		= '';
foreach ($customFields as $field)
{
	switch ($field->name)
	 {
		 case 'photo':
		        // echo print_r($field, true);
			$photo 	= $field->value;
		default:
	}
}
Display the photo:

Code: Select all

<?php 
if (!empty($photo))
{
	echo $photo;
}
?>
The function print_r() displays the attributes of the object, if you prefer to extract the details of the image from 'rawvalue' in JSON format, for example

Code: Select all

{"imagefile":"images\/sampledata\/cassiopeia\/nasa2-400.jpg#joomlaImage:\/\/local-images\/sampledata\/cassiopeia\/nasa2-400.jpg?width=400&height=400","alt_text":""}
Toivo Talikka, Global Moderator

User avatar
ceford
Joomla! Ace
Joomla! Ace
Posts: 1382
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: How to show user image in the logout module?

Post by ceford » Sun Oct 02, 2022 3:25 pm

Thank you @tiovo! I should have known that because I wrote some documentation for Fields and Field Groups as it applies to articles. I have added the the code you provided to show the procedure to display a User field. It is here: https://docs.joomla.org/J4.x:Fields_and_Field_Groups

User avatar
camerata
Joomla! Apprentice
Joomla! Apprentice
Posts: 21
Joined: Sun Jun 07, 2009 12:02 pm

Re: How to show user image in the logout module?

Post by camerata » Sun Oct 02, 2022 11:46 pm

Great, @toivo.
It works. The $photo just is an image formated to display, but I need to include my own css class value.

How to get only the photo URL?
Last edited by toivo on Mon Oct 03, 2022 12:57 am, edited 1 time in total.
Reason: mod note: removed unnecessary quote

User avatar
camerata
Joomla! Apprentice
Joomla! Apprentice
Posts: 21
Joined: Sun Jun 07, 2009 12:02 pm

Re: How to show user image in the logout module?

Post by camerata » Mon Oct 03, 2022 12:20 am

@toivo : Using 'rawvalue',

Code: Select all

$field->rawvalue;
I get the field value in JSON format. How to filter to use only the image URL without other data?
The 'print_r($field, true)' displays same image coded like '$field->value'.
Last edited by toivo on Mon Oct 03, 2022 12:31 am, edited 1 time in total.
Reason: mod note: unnecessary quote removed

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 15260
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: How to show user image in the logout module?

Post by toivo » Mon Oct 03, 2022 12:41 am

This version decodes and retrieves the URL from the rawvalue attribute and also gets the alt text, which could store the name of the user:

Code: Select all

// display custom field 'photo'
use Joomla\CMS\Factory;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
$customFields 	= FieldsHelper::getFields('com_users.user', Factory::getUser(), true); 
$photo		= '';
foreach ($customFields as $field)
{
	switch ($field->name)
	 {
		 case 'photo':
			// echo print_r($field, true);
			$photo 		= $field->value;
			$object		= json_decode($field->rawvalue, false);
			$string		= $object->imagefile;
			$image		= substr($string, 0, strpos($string, '#'));
			$alt_text	= $object->alt_text;	
			echo 'image URL = ' . $image . '<br />';
			echo 'alt_text = ' .  $alt_text;
		default:
	}
}
Toivo Talikka, Global Moderator

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 15260
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: How to show user image in the logout module?

Post by toivo » Mon Oct 03, 2022 2:28 am

This improved version does not display warnings if the custom field has not been filled in:

Code: Select all

// display custom field 'photo'
use Joomla\CMS\Factory;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
$customFields 	= FieldsHelper::getFields('com_users.user', Factory::getUser(), true); 
$photo		= '';
$image 		= '';
$alt_text	= '';
foreach ($customFields as $field)
{
	switch ($field->name)
	 {
		 case 'photo':
			// echo print_r($field, true);
			$photo 		= $field->value;
			$object		=  json_decode($field->rawvalue, false);
			if (is_object($object))
			{
				$string		= $object->imagefile;
				$image		= substr($string, 0, strpos($string, '#'));
				$alt_text	= $object->alt_text;
			}
			echo 'image URL = ' . $image . '<br />';
			echo 'alt_text = ' .  $alt_text;
		default:
	}
}[code]
Toivo Talikka, Global Moderator

User avatar
camerata
Joomla! Apprentice
Joomla! Apprentice
Posts: 21
Joined: Sun Jun 07, 2009 12:02 pm

Re: How to show user image in the logout module?

Post by camerata » Mon Oct 03, 2022 4:00 am

Great job. Thanks!

toivo wrote:
Mon Oct 03, 2022 2:28 am
This improved version does not display warnings if the custom field has not been filled in:

Code: Select all

// display custom field 'photo'
use Joomla\CMS\Factory;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
$customFields 	= FieldsHelper::getFields('com_users.user', Factory::getUser(), true); 
$photo		= '';
$image 		= '';
$alt_text	= '';
foreach ($customFields as $field)
{
	switch ($field->name)
	 {
		 case 'photo':
			// echo print_r($field, true);
			$photo 		= $field->value;
			$object		=  json_decode($field->rawvalue, false);
			if (is_object($object))
			{
				$string		= $object->imagefile;
				$image		= substr($string, 0, strpos($string, '#'));
				$alt_text	= $object->alt_text;
			}
			echo 'image URL = ' . $image . '<br />';
			echo 'alt_text = ' .  $alt_text;
		default:
	}
}[code]
[/quote]

User avatar
ceford
Joomla! Ace
Joomla! Ace
Posts: 1382
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: How to show user image in the logout module?

Post by ceford » Mon Oct 03, 2022 5:39 am

toivo wrote:
Mon Oct 03, 2022 2:28 am
This improved version does not display warnings if the custom field has not been filled in:
Please test this code. It does not put anything sensible in the Logout box. The original did not display a warning if the field is not filled in.

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 15260
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: How to show user image in the logout module?

Post by toivo » Mon Oct 03, 2022 7:30 am

ceford wrote:
Mon Oct 03, 2022 5:39 am
The original did not display a warning if the field is not filled in.
Are you using PHP 8.0 or below? The improvements to the mod_login override were done to get rid of the Deprecated messages in PHP 8.1.9.

ceford wrote:
Mon Oct 03, 2022 5:39 am
Please test this code. It does not put anything sensible in the Logout box.
The attached override worked well for testing purposes with Joomla 4.2.3 in Wampserver and PHP 8.1.9, even when Error Reporting was set to 'Maximum'.

default_logout.php

Code: Select all

<?php

/**
 * @package     Joomla.Site
 * @subpackage  mod_login
 *
 * @copyright   (C) 2006 Open Source Matters, Inc. <https://www.joomla.org>
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;

// display custom field 'photo'
use Joomla\CMS\Factory;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
$customFields 	= FieldsHelper::getFields('com_users.user', Factory::getUser(), true); 
$photo		= '';
$image 	= '';
$alt_text	= '';
foreach ($customFields as $field)
{
	switch ($field->name)
	 {
		 case 'photo':
			// echo print_r($field, true);
			$photo 		= $field->value;
			$object		=  json_decode($field->rawvalue, false);
			if (is_object($object))
			{
				$string		= $object->imagefile;
				$image		= substr($string, 0, strpos($string, '#'));
				$alt_text	= $object->alt_text;
			}
			echo 'image URL = ' . $image . '<br />';
			echo 'alt_text = ' .  $alt_text;
		default:
	}
}

HTMLHelper::_('behavior.keepalive');
?>
<form class="mod-login-logout form-vertical" action="<?php echo Route::_('index.php', true); ?>" method="post" id="login-form-<?php echo $module->id; ?>">
<?php if ($params->get('greeting', 1)) : ?>
    <div class="mod-login-logout__login-greeting login-greeting">
    <?php if (!$params->get('name', 0)) : ?>
        <?php echo Text::sprintf('MOD_LOGIN_HINAME', htmlspecialchars($user->get('name'), ENT_COMPAT, 'UTF-8')); ?>
    <?php else : ?>
        <?php echo Text::sprintf('MOD_LOGIN_HINAME', htmlspecialchars($user->get('username'), ENT_COMPAT, 'UTF-8')); ?>
    <?php endif; ?>
    </div>
<?php endif; ?>

<?php 
if (!empty($photo))
{
	echo $photo;
}
?>

<?php if ($params->get('profilelink', 0)) : ?>
    <ul class="mod-login-logout__options list-unstyled">
        <li>
            <a href="<?php echo Route::_('index.php?option=com_users&view=profile'); ?>">
            <?php echo Text::_('MOD_LOGIN_PROFILE'); ?></a>
        </li>
    </ul>
<?php endif; ?>
    <div class="mod-login-logout__button logout-button">
        <button type="submit" name="Submit" class="btn btn-primary"><?php echo Text::_('JLOGOUT'); ?></button>
        <input type="hidden" name="option" value="com_users">
        <input type="hidden" name="task" value="user.logout">
        <input type="hidden" name="return" value="<?php echo $return; ?>">
        <?php echo HTMLHelper::_('form.token'); ?>
    </div>
</form>
Toivo Talikka, Global Moderator


Post Reply

Return to “Joomla! 4.x Coding”