Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file types for media custom field plugin. #45013

Open
wants to merge 31 commits into
base: 5.3-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b109423
Add file types for media custom field plugin.
sergeytolkachyov Feb 25, 2025
73b3696
Update plugins/fields/media/params/media.xml
sergeytolkachyov Feb 26, 2025
667d5db
Fix language string and strings sorting
sergeytolkachyov Feb 26, 2025
4d745f4
Merge remote-tracking branch 'origin/5.3-dev' into 5.3-dev
sergeytolkachyov Feb 26, 2025
9a36d9b
Update libraries/src/Form/Field/AccessiblemediaField.php
sergeytolkachyov Feb 26, 2025
dff217a
Update administrator/language/en-GB/plg_fields_media.ini
sergeytolkachyov Feb 26, 2025
8a2e00b
Update plugins/fields/media/params/media.xml
sergeytolkachyov Feb 26, 2025
60ff74c
Merge branch 'joomla:5.3-dev' into 5.3-dev
sergeytolkachyov Feb 27, 2025
81757c4
- Rebuild XML constructing with SimpleXMLElement
sergeytolkachyov Feb 27, 2025
c42fd84
- add `Change file` language string for modal select window
sergeytolkachyov Feb 27, 2025
0c0f885
- change new field name to `linktext`
sergeytolkachyov Feb 27, 2025
2955904
language strings sorted alphabetically
sergeytolkachyov Feb 27, 2025
bd4ce70
fix namespace place
sergeytolkachyov Feb 27, 2025
eda1127
fix namespace place
sergeytolkachyov Feb 27, 2025
0d03c71
fix code style for continuous-integration/drone/pr
sergeytolkachyov Feb 27, 2025
2407a79
fix code style for continuous-integration/drone/pr - global_namespace…
sergeytolkachyov Feb 27, 2025
a426e7f
fix code style for continuous-integration/drone/pr - layouts/joomla/f…
sergeytolkachyov Feb 27, 2025
02796b9
fix code style for continuous-integration/drone/pr - layouts/joomla/f…
sergeytolkachyov Feb 27, 2025
7fc5168
remove the space before/after `=`
sergeytolkachyov Feb 27, 2025
ea80139
Update layouts/joomla/form/field/media.php
sergeytolkachyov Feb 27, 2025
fd46369
Update language/en-GB/lib_joomla.ini
sergeytolkachyov Feb 27, 2025
2bc5c45
Merge branch 'joomla:5.3-dev' into 5.3-dev
sergeytolkachyov Feb 27, 2025
10653ba
`Change file` to `Change File`
sergeytolkachyov Feb 27, 2025
4ae22c2
revert space changes in layout variables list
sergeytolkachyov Feb 27, 2025
742afc4
remove spaces
sergeytolkachyov Feb 27, 2025
3358c7e
code style
sergeytolkachyov Feb 27, 2025
2f0fc13
Alpha order namespace import
sergeytolkachyov Feb 27, 2025
884ff61
Merge branch 'joomla:5.3-dev' into 5.3-dev
sergeytolkachyov Feb 28, 2025
3d4cf29
Add audio and video layouts
sergeytolkachyov Feb 28, 2025
887919c
Link text field only for `documents` file type. `Audios` and `videos`…
sergeytolkachyov Feb 28, 2025
8e77148
Add support own layouts for `audios` and `videos` file types.
sergeytolkachyov Feb 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions administrator/language/en-GB/lib_joomla.ini
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ JLIB_FORM_BUTTON_CLEAR="Clear"
JLIB_FORM_BUTTON_SELECT="Select"
JLIB_FORM_CHANGE_IMAGE="Change Image"
JLIB_FORM_CHANGE_IMAGE_BUTTON="Change Image Button"
JLIB_FORM_CHANGE_FILE="Change file"
JLIB_FORM_CHANGE_USER="Select User"
JLIB_FORM_CONTAINS_INVALID_FIELDS="The form cannot be submitted as it's missing required data. <br> Please correct the marked fields and try again."
JLIB_FORM_ERROR_FIELDS_CATEGORY_ERROR_EXTENSION_EMPTY="Extension attribute is empty in the category field."
Expand All @@ -277,6 +278,9 @@ JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_EMPTY_DESC="Decorative Image -
JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_EMPTY_LABEL="No Description"
JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_TEXT_LABEL="Image Description (Alt Text)"
JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_IMAGEFILE_LABEL="Image"
JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_LINKTEXT = "Link text"
JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_LINKTEXT_DEFAULT_VALUE = "Download"
JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_LINKTEXT_DESC = "Specify the link text for non-image file."
JLIB_FORM_FIELD_PARAM_CALENDAR_FORMAT_DESC="The date format to be used. This is in the format used by PHP to specify date string formats (see below). If no format argument is given, '%Y-%m-%d' is assumed (giving dates like '2008-04-16')."
JLIB_FORM_FIELD_PARAM_CALENDAR_FORMAT_LABEL="Format"
JLIB_FORM_FIELD_PARAM_CHECKBOX_MULTIPLE_DESC="Allow multiple values to be selected."
Expand Down
6 changes: 6 additions & 0 deletions administrator/language/en-GB/plg_fields_media.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ PLG_FIELDS_MEDIA_PARAMS_DIRECTORY_LABEL="Directory"
PLG_FIELDS_MEDIA_PARAMS_IMAGE_CLASS_LABEL="Image Class"
PLG_FIELDS_MEDIA_PARAMS_PREVIEW_INLINE="Inline"
PLG_FIELDS_MEDIA_PARAMS_PREVIEW_LABEL="Preview"
PLG_FIELDS_MEDIA_PARAMS_TYPES_AUDIOS = "Audio"
PLG_FIELDS_MEDIA_PARAMS_TYPES_DESCRIPTION = "You can configure a file types in Media manager settings"
PLG_FIELDS_MEDIA_PARAMS_TYPES_DOCUMENTS = "Documents"
PLG_FIELDS_MEDIA_PARAMS_TYPES_IMAGES = "Images"
PLG_FIELDS_MEDIA_PARAMS_TYPES_LABEL = "File Types"
PLG_FIELDS_MEDIA_PARAMS_TYPES_VIDEOS = "Video"
PLG_FIELDS_MEDIA_XML_DESCRIPTION="This plugin lets you create new fields of type 'media' in any extensions where custom fields are supported."
4 changes: 4 additions & 0 deletions language/en-GB/lib_joomla.ini
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ JLIB_FORM_BUTTON_CLEAR="Clear"
JLIB_FORM_BUTTON_SELECT="Select"
JLIB_FORM_CHANGE_IMAGE="Change Image"
JLIB_FORM_CHANGE_IMAGE_BUTTON="Change Image Button"
JLIB_FORM_CHANGE_FILE="Change file"
JLIB_FORM_CHANGE_USER="Select User"
JLIB_FORM_CONTAINS_INVALID_FIELDS="The form cannot be submitted as it's missing required data. <br> Please correct the marked fields and try again."
JLIB_FORM_ERROR_FIELDS_CATEGORY_ERROR_EXTENSION_EMPTY="Extension attribute is empty in the category field."
Expand All @@ -276,6 +277,9 @@ JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_EMPTY_DESC="Decorative Image -
JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_EMPTY_LABEL="No Description"
JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_TEXT_LABEL="Image Description (Alt Text)"
JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_IMAGEFILE_LABEL="Image"
JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_LINKTEXT = "Link text"
JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_LINKTEXT_DEFAULT_VALUE = "Download"
JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_LINKTEXT_DESC = "Specify the link text for non-image file."
JLIB_FORM_FIELD_PARAM_CALENDAR_FORMAT_DESC="The date format to be used. This is in the format used by PHP to specify date string formats (see below). If no format argument is given, '%Y-%m-%d' is assumed (giving dates like '2008-04-16')."
JLIB_FORM_FIELD_PARAM_CALENDAR_FORMAT_LABEL="Format"
JLIB_FORM_FIELD_PARAM_CHECKBOX_MULTIPLE_DESC="Allow multiple values to be selected."
Expand Down
8 changes: 7 additions & 1 deletion layouts/joomla/form/field/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@
]);
}

$imagesOnly = false;

if(count($mediaTypeNames) == 1 && in_array('images', $mediaTypeNames)){
$imagesOnly = true;
}

?>
<joomla-field-media class="field-media-wrapper"
types="<?php echo $this->escape(implode(',', $mediaTypeNames)); ?>"
Expand All @@ -164,7 +170,7 @@
input=".field-media-input"
button-select=".button-select"
button-clear=".button-clear"
modal-title="<?php echo $this->escape(Text::_('JLIB_FORM_CHANGE_IMAGE')); ?>"
modal-title="<?php echo $this->escape(Text::_($imagesOnly ? 'JLIB_FORM_CHANGE_IMAGE' : 'JLIB_FORM_CHANGE_FILE')); ?>"
preview="static"
preview-container=".field-media-preview"
preview-width="<?php echo $previewWidth; ?>"
Expand Down
100 changes: 64 additions & 36 deletions libraries/src/Form/Field/AccessiblemediaField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace Joomla\CMS\Form\Field;

// phpcs:disable PSR1.Files.SideEffects
use Joomla\CMS\Language\Text;

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

Expand Down Expand Up @@ -68,6 +70,14 @@ class AccessiblemediaField extends SubformField
*/
protected $layout;

/**
* Media types like 'images, audios, documents, videos'
*
* @var string
* @since __DEPLOY_VERSION__
*/
protected $types;

/**
* Method to get certain otherwise inaccessible properties from the form field object.
*
Expand All @@ -84,6 +94,7 @@ public function __get($name)
case 'preview':
case 'previewHeight':
case 'previewWidth':
case 'types':
return $this->$name;
}

Expand All @@ -105,6 +116,7 @@ public function __set($name, $value)
switch ($name) {
case 'directory':
case 'preview':
case 'types':
$this->$name = (string) $value;
break;

Expand Down Expand Up @@ -179,43 +191,59 @@ public function setup(\SimpleXMLElement $element, $value, $group = null)
$this->preview = (string) $this->element['preview'];
$this->previewHeight = isset($this->element['preview_height']) ? (int) $this->element['preview_height'] : 200;
$this->previewWidth = isset($this->element['preview_width']) ? (int) $this->element['preview_width'] : 200;
$this->types = isset($this->element['types']) ? (string) $this->element['types'] : 'images';

// Build the form source
$xml = new \SimpleXMLElement('<form />');
$fieldset = $xml->addChild('fieldset');
$fieldset->addAttribute('name', 'accessiblemedia');
$fieldset->addAttribute('label', 'JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_LABEL');

$imagefile = $fieldset->addChild('field');
$imagefile->addAttribute('name', 'imagefile');
$imagefile->addAttribute('type', 'media');
$imagefile->addAttribute('label', 'JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_LABEL');
$imagefile->addAttribute('directory', $this->directory);
$imagefile->addAttribute('preview', $this->preview);
$imagefile->addAttribute('preview_width', $this->previewWidth);
$imagefile->addAttribute('preview_height', $this->previewHeight);
$imagefile->addAttribute('types', $this->types);
$imagefile->addAttribute('schemes', 'http,https,ftp,ftps,data,file');
$imagefile->addAttribute('validate', 'url');
$imagefile->addAttribute('relative', 'true');

$mediaTypes = explode(',', $this->types);

// We need in alt text and alt empty checkbox only for images
if (in_array('images', $mediaTypes)) {
$altText = $fieldset->addChild('field');
$altText->addAttribute('name', 'alt_text');
$altText->addAttribute('type', 'text');
$altText->addAttribute('label', 'JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_TEXT_LABEL');

$altEmpty = $fieldset->addChild('field');
$altEmpty->addAttribute('name', 'alt_empty');
$altEmpty->addAttribute('type', 'checkbox');
$altEmpty->addAttribute('label', 'JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_EMPTY_LABEL');
$altEmpty->addAttribute('description', 'JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_EMPTY_DESC');
}

foreach (['audios', 'videos', 'documents'] as $fileType) {
if (in_array($fileType, $mediaTypes)) {
$fileName = $fieldset->addChild('field');
$fileName->addAttribute('name', 'linktext');
$fileName->addAttribute('type', 'text');
$fileName->addAttribute('label', 'JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_LINKTEXT');
$fileName->addAttribute('description', 'JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_LINKTEXT_DESC');
$fileName->addAttribute('filter', 'string');
$fileName->addAttribute('default', Text::_('JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_LINKTEXT_DEFAULT_VALUE'));
$fileName->addAttribute('hint', Text::_('JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_LINKTEXT_DEFAULT_VALUE'));

break;
}
}

$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset
name="accessiblemedia"
label="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_LABEL"
>
<field
name="imagefile"
type="media"
label="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_IMAGEFILE_LABEL"
directory="$this->directory"
preview="$this->preview"
preview_width="$this->previewWidth"
preview_height="$this->previewHeight"
schemes="http,https,ftp,ftps,data,file"
validate="url"
relative="true"
/>

<field
name="alt_text"
type="text"
label="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_TEXT_LABEL"
/>

<field
name="alt_empty"
type="checkbox"
label="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_EMPTY_LABEL"
description="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_EMPTY_DESC"
/>
</fieldset>
</form>
XML;
$this->formsource = $xml;
$this->formsource = $xml->asXML();

$this->layout = 'joomla.form.field.media.accessiblemedia';

Expand Down
15 changes: 15 additions & 0 deletions plugins/fields/media/params/media.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@
label="PLG_FIELDS_MEDIA_PARAMS_IMAGE_CLASS_LABEL"
validate="CssIdentifier"
/>

<field
name="types"
type="list"
label="PLG_FIELDS_MEDIA_PARAMS_TYPES_LABEL"
description="PLG_FIELDS_MEDIA_PARAMS_TYPES_DESCRIPTION"
default="images"
multiple="true"
layout="joomla.form.field.list-fancy-select"
>
<option value="images">PLG_FIELDS_MEDIA_PARAMS_TYPES_IMAGES</option>
<option value="audios">PLG_FIELDS_MEDIA_PARAMS_TYPES_AUDIOS</option>
<option value="videos">PLG_FIELDS_MEDIA_PARAMS_TYPES_VIDEOS</option>
<option value="documents">PLG_FIELDS_MEDIA_PARAMS_TYPES_DOCUMENTS</option>
</field>
</fieldset>
</fields>
</form>
29 changes: 23 additions & 6 deletions plugins/fields/media/tmpl/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,37 @@

defined('_JEXEC') or die;

use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Layout\LayoutHelper;
use Joomla\CMS\Helper\MediaHelper;
use Joomla\CMS\Language\Text;

if (empty($field->value) || empty($field->value['imagefile'])) {
return;
}

$class = $fieldParams->get('image_class');
$options = [
'src' => $field->value['imagefile'],
'alt' => empty($field->value['alt_text']) && empty($field->value['alt_empty']) ? false : $field->value['alt_text'],
];
$fileUrl = MediaHelper::getCleanMediaFieldValue($field->value['imagefile']);

$class = $fieldParams->get('image_class');
$options = [];

if ($class) {
$options['class'] = $class;
}

echo LayoutHelper::render('joomla.html.image', $options);
if (MediaHelper::isImage($fileUrl) || MediaHelper::getMimeType($fileUrl) === 'image/svg+xml') {
$options = [
'src' => $field->value['imagefile'],
'alt' => empty($field->value['alt_text']) && empty($field->value['alt_empty']) ? false : $field->value['alt_text'],
];

if ($class) {
$options['class'] = $class;
}

echo LayoutHelper::render('joomla.html.image', $options);
} else {
$linkText = $field->value['linktext'] ?? Text::_('JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_LINKTEXT_DEFAULT_VALUE');

echo HTMLHelper::link($fileUrl, $linkText, $options);
}