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

[4.0] Media fields accessibility - second try #27712

Merged
merged 59 commits into from May 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
3d05001
add param for alt
astridx Jan 29, 2020
28c8312
change fieldtype
astridx Jan 29, 2020
018b314
accessiblemedia
astridx Jan 30, 2020
7d9a661
layout
astridx Jan 30, 2020
1e42c33
Merge branch '4.0-dev' of https://github.com/joomla/joomla-cms into m…
astridx Jan 30, 2020
79b829d
correction
astridx Jan 30, 2020
fa42562
frontend alt text dispay correction
astridx Jan 30, 2020
89a45c9
change layout
astridx Jan 30, 2020
59791c8
fixes
astridx Jan 30, 2020
6b65939
language fix
astridx Jan 30, 2020
f31c5d9
Merge branch '4.0-dev' of https://github.com/joomla/joomla-cms into m…
astridx Jan 31, 2020
97b8319
delete param in plugin, because we want to force the use. It should n…
astridx Jan 31, 2020
f1f8f69
move formsource because it belongs to the field and not to the plugin
astridx Jan 31, 2020
c7ef342
change layout
astridx Jan 31, 2020
88f9ab3
make field use media variables
astridx Jan 31, 2020
ba554b8
correciton
astridx Jan 31, 2020
edf38f6
correciton
astridx Jan 31, 2020
b5b072f
correction
astridx Jan 31, 2020
f5c302c
language files
astridx Feb 1, 2020
5a2a4b0
add parents variables
astridx Feb 1, 2020
75aa70f
Merge branch '4.0-dev' into media2
astridx Feb 1, 2020
480e655
https://github.com/joomla/joomla-cms/pull/27746
astridx Feb 1, 2020
08b9709
wip
astridx Feb 9, 2020
89c30e3
conflict
astridx Feb 9, 2020
f4b3b0b
change code form layout to field
astridx Feb 9, 2020
19e4ed1
correction
astridx Feb 9, 2020
572a900
wip
astridx Feb 9, 2020
1f67de2
wip
astridx Feb 9, 2020
6828c48
wip
astridx Feb 9, 2020
3c8a1f4
wip
astridx Feb 9, 2020
bc499c5
Merge branch '4.0-dev' of https://github.com/joomla/joomla-cms into m…
astridx Feb 10, 2020
4da9620
old values
astridx Feb 10, 2020
cb3b9bd
wip
astridx Feb 10, 2020
0f07ac8
wip
astridx Feb 10, 2020
aec816f
wip
astridx Feb 10, 2020
c57fb9f
Merge branch '4.0-dev' of https://github.com/joomla/joomla-cms into m…
astridx Feb 11, 2020
fc3c671
cs
astridx Feb 11, 2020
473afb3
Remove outside of else section and remove empty else section.
astridx Feb 12, 2020
59811e1
Merge branch '4.0-dev' of https://github.com/joomla/joomla-cms into m…
astridx Feb 12, 2020
8d378b0
Merge branch '4.0-dev' of https://github.com/joomla/joomla-cms into m…
astridx Feb 15, 2020
8327138
nun aber
astridx Feb 15, 2020
a7d8f8a
try
astridx Feb 15, 2020
c4d5130
wip
astridx Feb 15, 2020
f3b19a0
Merge branch '4.0-dev' of https://github.com/joomla/joomla-cms into m…
astridx Feb 16, 2020
561ecaa
redo
astridx Feb 16, 2020
468e721
Merge branch '4.0-dev' of https://github.com/joomla/joomla-cms into m…
astridx Mar 1, 2020
f42dbae
Merge branch '4.0-dev' of https://github.com/joomla/joomla-cms into m…
astridx Mar 14, 2020
53b4698
Merge branch '4.0-dev' of https://github.com/joomla/joomla-cms into m…
astridx Mar 24, 2020
6e0fb6d
Merge branch '4.0-dev' of https://github.com/joomla/joomla-cms into m…
astridx Mar 29, 2020
6618d64
Merge branch '4.0-dev' of https://github.com/joomla/joomla-cms into m…
astridx Apr 13, 2020
a9ff995
Merge branch '4.0-dev' of https://github.com/joomla/joomla-cms into m…
astridx Apr 18, 2020
272ab32
wip
astridx Apr 18, 2020
e8fc9d5
add comment
astridx Apr 18, 2020
4a24cb4
add comment
astridx Apr 18, 2020
aa900db
cs
astridx Apr 18, 2020
3973cf8
extend check
astridx Apr 18, 2020
3188685
conflict
astridx May 10, 2020
8294355
Merge branch '4.0-dev' of https://github.com/joomla/joomla-cms into m…
astridx May 23, 2020
7b4068e
Merge branch '4.0-dev' of https://github.com/joomla/joomla-cms into m…
astridx May 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions administrator/language/en-GB/lib_joomla.ini
Expand Up @@ -335,6 +335,9 @@ JLIB_FORM_FIELD_INVALID_MIN_TIME="The time you entered is before the minimum tim
JLIB_FORM_FIELD_INVALID_TIME_INPUT="Invalid time format. Please use hh:mm."
JLIB_FORM_FIELD_INVALID_TIME_INPUT_SECONDS="Invalid time format. Please use hh:mm:ss."
JLIB_FORM_FIELD_INVALID_VALUE="This value is not valid"
JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_LABEL="Media"
JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_IMAGEFILE_LABEL="Image"
JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_TEXT_LABEL="Alt Text"
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
23 changes: 23 additions & 0 deletions layouts/joomla/form/field/media/accessiblemedia.php
@@ -0,0 +1,23 @@
<?php
/**
* @package Joomla.Site
* @subpackage Layout
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

extract($displayData);

$form = $forms[0];

$formfields = $form->getGroup('');
?>

<div class="subform-wrapper">
<?php foreach ($formfields as $field) : ?>
<?php echo $field->renderField(); ?>
<?php endforeach; ?>
</div>
197 changes: 197 additions & 0 deletions libraries/src/Form/Field/AccessiblemediaField.php
@@ -0,0 +1,197 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Form\Field;

\defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\Form\Field\SubformField;

/**
* The Field to load the form inside current form
*
* @since __DEPLOY_VERSION__
*/
class AccessiblemediaField extends SubformField
{
/**
* The form field type.
*
* @var string
* @since __DEPLOY_VERSION__
*/
protected $type = 'Accessiblemedia';

/**
* The preview.
*
* @var string
* @since __DEPLOY_VERSION__
*/
protected $preview;

/**
* The directory.
*
* @var string
* @since __DEPLOY_VERSION__
*/
protected $directory;

/**
* The previewWidth.
*
* @var integer
* @since __DEPLOY_VERSION__
*/
protected $previewWidth;

/**
* The previewHeight.
*
* @var integer
* @since __DEPLOY_VERSION__
*/
protected $previewHeight;

/**
* Layout to render
*
* @var string
* @since __DEPLOY_VERSION__
*/
protected $layout;

/**
* Method to get certain otherwise inaccessible properties from the form field object.
*
* @param string $name The property name for which to get the value.
*
* @return mixed The property value or null.
*
* @since __DEPLOY_VERSION__
*/
public function __get($name)
{
switch ($name)
{
case 'directory':
case 'preview':
case 'previewHeight':
case 'previewWidth':
return $this->$name;
}

return parent::__get($name);
}

/**
* Method to set certain otherwise inaccessible properties of the form field object.
*
* @param string $name The property name for which to set the value.
* @param mixed $value The value of the property.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function __set($name, $value)
{
switch ($name)
{
case 'directory':
case 'preview':
$this->$name = (string) $value;
break;

case 'previewHeight':
case 'previewWidth':
$this->$name = (int) $value;
break;

default:
parent::__set($name, $value);
}
}

/**
* Method to attach a Form object to the field.
*
* @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.
*
* @return boolean True on success.
*
* @since __DEPLOY_VERSION__
*/
public function setup(\SimpleXMLElement $element, $value, $group = null)
{
json_decode($value);

// Check if value is a valid JSON string.
if ($value !== '' && json_last_error() !== JSON_ERROR_NONE)
{
/**
* If the value is not empty and is not a valid JSON string,
* it is most likely a custom field created in Joomla 3 and
* the value is a string that contains the file name.
*/
if (file_exists(JPATH_ROOT . '/' . $value))
{
$value = '{"imagefile":"' . $value . '","alt_text":""}';
}
else
{
$value = '';
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in the plugin.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it that way.
Then the picture of an in the past created field is only displayed correctly in the front. It is not displayed in the back end if you edit an article.
What am I doing wrong?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. But this will need to be fixed somehow :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that the PR works again I fixed it in the field again. I see no way to influence the value of the field in the custom field - just the markup. If someone sees a possibility: I would be happy to learn. I also think that this should be done better in Custom Field.


if (!parent::setup($element, $value, $group))
{
return false;
}

$this->directory = (string) $this->element['directory'];
$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;

$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"
/>

<field
name="alt_text"
type="text"
label="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_TEXT_LABEL"
/>
</fieldset>
</form>
XML;
$this->formsource = $xml;

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

return true;
}
}
70 changes: 70 additions & 0 deletions plugins/fields/media/media.php
Expand Up @@ -9,11 +9,81 @@

defined('_JEXEC') or die;

use Joomla\CMS\Form\Form;

/**
* Fields Media Plugin
*
* @since 3.7.0
*/
class PlgFieldsMedia extends \Joomla\Component\Fields\Administrator\Plugin\FieldsPlugin
{
/**
* Transforms the field into a DOM XML element and appends it as a child on the given parent.
*
* @param stdClass $field The field.
* @param DOMElement $parent The field node parent.
* @param Form $form The form.
*
* @return DOMElement
*
* @since __DEPLOY_VERSION__
*/
public function onCustomFieldsPrepareDom($field, DOMElement $parent, Form $form)
{
$fieldNode = parent::onCustomFieldsPrepareDom($field, $parent, $form);

if (!$fieldNode)
{
return $fieldNode;
}

$fieldNode->setAttribute('type', 'accessiblemedia');

return $fieldNode;
}

/**
* Before prepares the field value.
*
* @param string $context The context.
* @param \stdclass $item The item.
* @param \stdclass $field The field.
*
* @return void
*
* @since __DEPLOAY_VERSION__
*/
public function onCustomFieldsBeforePrepareField($context, $item, $field)
{
// Check if the field should be processed by us
if (!$this->isTypeSupported($field->type))
{
return;
}

// Check if the field value is an old (string) value
$field->apivalue = $this->checkValue($field->value);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc block is missing @astridx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @zero-24 . Out of laziness, I often use the Joomla internal cs-checker and this doesn't show such errors.

/**
* Before prepares the field value.
*
* @param string $value The value to check.
*
* @return array The checked value
*
* @since __DEPLOAY_VERSION__
*/
private function checkValue($value)
{
json_decode($value);

if (json_last_error() === JSON_ERROR_NONE)
{
return (array) json_decode($value, true);
}

return array('imagefile' => $value, 'alt_text' => '');
}
}
20 changes: 11 additions & 9 deletions plugins/fields/media/tmpl/media.php
Expand Up @@ -20,20 +20,22 @@
$class = ' class="' . htmlentities($class, ENT_COMPAT, 'UTF-8', true) . '"';
}

$value = (array) $field->value;
$value = $field->apivalue;
$buffer = '';

foreach ($value as $path)
if ($value)
{
if (!$path)
$path = $value['imagefile'];
$alt = ' alt="' . htmlentities($value['alt_text'], ENT_COMPAT, 'UTF-8', true) . '"';

if (file_exists($path))
{
continue;
$buffer .= sprintf('<img loading="lazy" src="%s"%s%s>',
htmlentities($path, ENT_COMPAT, 'UTF-8', true),
$class,
$alt
);
}

$buffer .= sprintf('<img loading="lazy" src="%s"%s>',
htmlentities($path, ENT_COMPAT, 'UTF-8', true),
$class
);
}

echo $buffer;