Get image in my custom module in J!5 Topic is solved

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

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
b_wp_b
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Feb 27, 2024 1:04 pm

Get image in my custom module in J!5

Post by b_wp_b » Tue Feb 27, 2024 1:11 pm

Hi,

how to display a photo inserted into a module field?

<field name="myimage" type="media" directory="stories" />

How to receive a field value and return an image with dimensions?

Thanks

gws
Joomla! Champion
Joomla! Champion
Posts: 5932
Joined: Tue Aug 23, 2005 1:56 pm
Location: South coast, UK
Contact:

Re: Get image in my custom module in J!5

Post by gws » Tue Feb 27, 2024 2:23 pm


b_wp_b
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Feb 27, 2024 1:04 pm

Re: Get image in my custom module in J!5

Post by b_wp_b » Tue Feb 27, 2024 5:23 pm

Thank you for the link, but where is the answer to my question?

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

Re: Get image in my custom module in J!5

Post by ceford » Wed Feb 28, 2024 6:12 am

Your question is ambiguous. What do you mean by module field? What type of module? Where do you see the code you quoted?

If you mean a custom module: use the TinyMCE CMS Content / Media buttons to select an image to insert.

b_wp_b
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Feb 27, 2024 1:04 pm

Re: Get image in my custom module in J!5

Post by b_wp_b » Wed Feb 28, 2024 7:46 am

I'm creating my own custom module that I created, and it has a MEDIA field in it.
Module type: mod_b_wp_b

mod_b_wp_b.xml: <field name="myimage" type="media" directory="stories" />
mod_b_wp_b.php: $myimage = $params->get('myimage', '');

I don't know how to receive the MEDIA field in the module TMPL to display the image and width and height

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

Re: Get image in my custom module in J!5

Post by ceford » Wed Feb 28, 2024 9:27 am

I can't answer your question directly. However...

Note your custom module id and look in the #__modules table for that id. What is in the params column. Your input form should have collected the url and width and height (and perhaps alt text). Are they there?

What is actually in $myimage in your tmpl php file - do a var_dump. It may be an object. You probably need to compose your own img tag from its parts:

Code: Select all

<?php echo '<img src=" . $xxx . '" width="'. $yyy . '" height="' . $zzz . '">"'; ?>
Where $xxx is something like $myimage->url (if $myimage is an object).

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

Re: Get image in my custom module in J!5

Post by toivo » Wed Feb 28, 2024 9:39 am

Mod note: moved to 5.x Coding
Toivo Talikka, Global Moderator

b_wp_b
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Feb 27, 2024 1:04 pm

Re: Get image in my custom module in J!5

Post by b_wp_b » Wed Feb 28, 2024 11:14 am

Unfortunately, the image address has been changed in J!4...

I found!

Code: Select all

$img_one = HTMLHelper::cleanImageURL($img_one);
echo '<img src="'. $img_one->url .'" alt="" width="'. $img_one->attributes['width'] .'" height="'. $img_one->attributes['height'] .'" loading="lazy">';

$img_two = HTMLHelper::cleanImageURL($img_two);
echo '<img src="'. $img_two->url .'" alt="" width="'. $img_two->attributes['width'] .'" height="'. $img_two->attributes['height'] .'" loading="lazy">';

$img_three = HTMLHelper::cleanImageURL($img_three);
echo '<img src="'. $img_three->url .'" alt="" width="'. $img_three->attributes['width'] .'" height="'. $img_three->attributes['height'] .'" loading="lazy">';

Can it be done more efficiently?

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

Re: Get image in my custom module in J!5

Post by ceford » Wed Feb 28, 2024 11:58 am

Where found? Does the code you have quoted work for you?

You could put your images in an array and iterate over the array to save some lines of code.

In what way has the image address changed?

b_wp_b
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Feb 27, 2024 1:04 pm

Re: Get image in my custom module in J!5

Post by b_wp_b » Wed Feb 28, 2024 12:19 pm

It works

mod_b_wp_b.xml:

Code: Select all

<field name="img_one" type="media" directory="stories" />
<field name="img_two" type="media" directory="stories" />
<field name="img_three" type="media" directory="stories" />

mod_b_wp_b.php:

Code: Select all

$img_one   = $params->get('img_one', false);
$img_two   = $params->get('img_two', false);
$img_three = $params->get('img_three', false);


Post Reply

Return to “Joomla! 5.x Coding”