I've created an additional media field to include in some articles, and I noticed that the ALT and Title attribute for the media field can not be set to the image. I need suggestions for an override or how to implement these tags.
1 Answer
First, you'll need to create a separate field for the alt tag. Make a note of this and the image field's ID.
You then need to override the article layout. To do this, copy components\com_content\views\article\tmpl\default.php
to templates\yourtemplate\html\com_content\article\default.php
(where 'yourtemplate' is the name of the template you are using).
Where you want the image to appear, you should then add some code along the lines of:
echo JHTML::_('image', $this->item->jcfields[1]->rawvalue,$this->item->jcfields[2]->rawvalue, ["title"=>$this->item->jcfields[2]->rawvalue]);
This code assumes that your image field has an ID of 1, and your alt and title field has an ID of 2. Just swap these numbers if not.
If this doesn't work, try adding
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
above the code, though I think this helper should already be called on the article view.
Additional validation code may be required if it is not a required field, but the above should be the basics.
-
Hi Richard, your solution is very interesting, but I thought of a modification of the media.php file of the media fields plugin, especially on line 34: " $buffer .= '<img src="' . htmlentities($path) . '"' . $class . '/>'; "– JosoJun 25, 2017 at 20:50