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 all 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 @@ -256,6 +256,7 @@ JLIB_FILTER_PARAMS_TEXT="Text"

JLIB_FORM_BUTTON_CLEAR="Clear"
JLIB_FORM_BUTTON_SELECT="Select"
JLIB_FORM_CHANGE_FILE="Change File"
JLIB_FORM_CHANGE_IMAGE="Change Image"
JLIB_FORM_CHANGE_IMAGE_BUTTON="Change Image Button"
JLIB_FORM_CHANGE_USER="Select User"
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 @@ -255,6 +255,7 @@ JLIB_FILTER_PARAMS_TEXT="Text"

JLIB_FORM_BUTTON_CLEAR="Clear"
JLIB_FORM_BUTTON_SELECT="Select"
JLIB_FORM_CHANGE_FILE="Change File"
JLIB_FORM_CHANGE_IMAGE="Change Image"
JLIB_FORM_CHANGE_IMAGE_BUTTON="Change Image Button"
JLIB_FORM_CHANGE_USER="Select User"
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: 5 additions & 3 deletions layouts/joomla/form/field/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
$width = $previewWidth;
$height = $previewHeight;
$style = ($width > 0) ? 'max-width:' . $width . 'px;' : '';
$style .= ($height > 0) ? 'max-height:' . $height . 'px;' : '';
$style .= ($height > 0) ? 'max-height:' . $height . 'px;' : '';

$imgattr = [
'class' => 'media-preview',
Expand All @@ -97,7 +97,7 @@

$img = HTMLHelper::_('image', $src, Text::_('JLIB_FORM_MEDIA_PREVIEW_ALT'), $imgattr);

$previewImg = '<div class="preview_img">' . $img . '</div>';
$previewImg = '<div class="preview_img">' . $img . '</div>';
$previewImgEmpty = '<div class="preview_empty"' . ($src ? ' class="hidden"' : '') . '>'
. Text::_('JLIB_FORM_MEDIA_PREVIEW_EMPTY') . '</div>';

Expand Down Expand Up @@ -155,6 +155,8 @@
]);
}

$imagesOnly = count($mediaTypeNames) == 1 && in_array('images', $mediaTypeNames);

?>
<joomla-field-media class="field-media-wrapper"
types="<?php echo $this->escape(implode(',', $mediaTypeNames)); ?>"
Expand All @@ -164,7 +166,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
26 changes: 26 additions & 0 deletions layouts/joomla/html/audio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

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

/**
* Layout variables
* -----------------
* @var array $displayData Array with all the given attributes for the audio element.
* Eg: src, class, controls, controlslist, autoplay, loop, style, data-*
* See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio
*/
defined('_JEXEC') or die;

use Joomla\Utilities\ArrayHelper;

if (array_key_exists('src', $displayData) && !empty($displayData['src'])) {
$displayData['src'] = $this->escape($displayData['src']);
}

echo '<audio ' . ArrayHelper::toString($displayData) . '></audio>';
40 changes: 40 additions & 0 deletions layouts/joomla/html/video.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

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

/**
* Layout variables
* -----------------
* @var array $displayData Array with all the given attributes for the video element.
* Eg: src, class, controls, width, height, autoplay, decoding, style, data-*
* Note: <source> tags for <video> - is array of arrays with `src` and `type` keys.
* $displayData['source'] = [ ['src' => 'path/to/video.mp4', 'type' => 'video/mp4'] ];
* See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video
*/
defined('_JEXEC') or die;

use Joomla\Utilities\ArrayHelper;

if (array_key_exists('src', $displayData) && !empty($displayData['src'])) {
$displayData['src'] = $this->escape($displayData['src']);
}

$source = [];
if (isset($displayData['source']) && is_array($displayData['source'])) {
$source = $displayData['source'];
unset($displayData['source']);
}

echo '<video ' . ArrayHelper::toString($displayData) . '>';
if (!empty($source)) {
foreach ($source as $sourseData) {
echo '<source src="' . $this->escape($sourseData['src']) . '" type="' . $sourseData['type'] . '" />';
}
}
echo '</video>';
96 changes: 60 additions & 36 deletions libraries/src/Form/Field/AccessiblemediaField.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Joomla\CMS\Form\Field;

use Joomla\CMS\Language\Text;

// phpcs:disable PSR1.Files.SideEffects
\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,55 @@ 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');
}

if (\in_array('documents', $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'));
}

$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>
71 changes: 65 additions & 6 deletions plugins/fields/media/tmpl/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,79 @@

defined('_JEXEC') or die;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Helper\MediaHelper;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Layout\LayoutHelper;
use Joomla\Filesystem\File;

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');
$comMediaParams = ComponentHelper::getParams('com_media');

$audiosExt = array_map(
'trim',
explode(
',',
$comMediaParams->get(
'audio_extensions',
'mp3,m4a,mp4a,ogg'
)
)
);
$videosExt = array_map(
'trim',
explode(
',',
$comMediaParams->get(
'video_extensions',
'mp4,mp4v,mpeg,mov,webm'
)
)
);
$documentsExt = array_map(
'trim',
explode(
',',
$comMediaParams->get(
'doc_extensions',
'doc,odg,odp,ods,odt,pdf,ppt,txt,xcf,xls,csv'
)
)
);

$fileExtension = File::getExt($fileUrl);

$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'],
];

echo LayoutHelper::render('joomla.html.image', $options);
} elseif (\in_array($fileExtension, $audiosExt)) {
$options['src'] = $fileUrl;
$options['controls'] = 'controls';

echo LayoutHelper::render('joomla.html.audio', $options);
} elseif (\in_array($fileExtension, $videosExt)) {
$options['src'] = $fileUrl;
$options['controls'] = 'controls';

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

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