? No Code Attached Yet
avatar brianteeman
brianteeman
16 Nov 2022

Is your feature request related to a problem? Please describe.

whenever you use the media field to select an image the image is always appended with the dimensions and extra path information.

This additional information is not always needed.

For example when using the media field to select a background image in mod_custom.

As a result if you want to be able to have a clean image name you need to run it through a helper (cleanImageURL)

Describe the solution you'd like

Instead of having to rewrite the stored image details to remove the un-needed additional data there should be an option available in the field definition to store just a clean image.

It just seems crazy to me that if I am writing code that I know will never use the additional data that I still collect that data only to have to write additional code to remove it.

Example

				<field
					name="backgroundimage"
					type="media"
					label="MOD_CUSTOM_FIELD_BACKGROUNDIMAGE_LABEL"
				/>

requires this php

<?php 
use Joomla\CMS\HTML\HTMLHelper;
HTMLHelper::_('cleanImageURL', $params->get('backgroundimage'))->url . '");
?>

Could become

				<field
					name="backgroundimage"
					type="media"
					clean="yes"
					label="MOD_CUSTOM_FIELD_BACKGROUNDIMAGE_LABEL"
				/>

Which would allow me to write php

<?php
$params->get('backgroundimage');
?>
avatar brianteeman brianteeman - open - 16 Nov 2022
avatar joomla-cms-bot joomla-cms-bot - change - 16 Nov 2022
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 16 Nov 2022
avatar joomdonation
joomdonation - comment - 17 Nov 2022

I imagine that we can a filter rule. Something like:

/**
 * Joomla! Content Management System
 *
 * @copyright  (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
 * @license        GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\CMS\Form\Filter;

use Joomla\CMS\Form\Form;
use Joomla\CMS\Form\FormFilterInterface;
use Joomla\CMS\Helper\MediaHelper;
use Joomla\Registry\Registry;

// phpcs:disable PSR1.Files.SideEffects
\defined('JPATH_PLATFORM') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
 * Form Filter class to clean image url for media form field
 *
 * @since  4.0.0
 */
class CleanImageUrlFilter implements FormFilterInterface
{
	/**
	 * Method to filter a field value.
	 *
	 * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
	 * @param   mixed              $value    The form field value to validate.
	 * @param   string             $group    The field name group control value. This acts as an array container for the field.
	 *                                       For example if the field has name="foo" and the group value is set to "bar" then the
	 *                                       full field name would end up being "bar[foo]".
	 * @param   Registry           $input    An optional Registry object with the entire data set to validate against the entire form.
	 * @param   Form               $form     The form object for which the field is being tested.
	 *
	 * @return  mixed   The filtered value.
	 *
	 * @since   4.0.0
	 */
	public function filter(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
	{
		if ($value)
		{
			$value = MediaHelper::getCleanMediaFieldValue($value);
		}

		return $value;
	}
}

Then you can have a field like below:

<field
					name="backgroundimage"
					type="media"
					filter="CleanImageUrl"
					label="MOD_CUSTOM_FIELD_BACKGROUNDIMAGE_LABEL"
				/>

And it should work exactly like that. The limitation is that this will only work for local adapter. Not sure if there is anything else we have to care about, or if we have a better idea to handle this request.

avatar brianteeman
brianteeman - comment - 17 Nov 2022

looks good to me

avatar Hackwar Hackwar - change - 22 Feb 2023
Labels Added: ?
avatar Hackwar Hackwar - labeled - 22 Feb 2023

Add a Comment

Login with GitHub to post a comment