diff --git a/libraries/src/Form/Field/AccessiblemediaField.php b/libraries/src/Form/Field/AccessiblemediaField.php index 526edc67c6968..aade4cd71e6f5 100644 --- a/libraries/src/Form/Field/AccessiblemediaField.php +++ b/libraries/src/Form/Field/AccessiblemediaField.php @@ -130,25 +130,40 @@ public function __set($name, $value) */ 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 a string, it is + * most likely within a custom field of type subform + * and the value is a stdClass with properties + * imagefile and alt_text. So it is fine. + */ + if (\is_string($value)) { - /** - * 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 + json_decode($value); + + // Check if value is a valid JSON string. + if ($value !== '' && json_last_error() !== JSON_ERROR_NONE) { - $value = ''; + /** + * 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 = ''; + } } } + elseif (!is_object($value) + || !property_exists($value, 'imagefile') + || !property_exists($value, 'alt_text')) + { + return false; + } if (!parent::setup($element, $value, $group)) { diff --git a/plugins/fields/media/media.php b/plugins/fields/media/media.php index c1b10da4016e1..e47d47a1cf9b3 100644 --- a/plugins/fields/media/media.php +++ b/plugins/fields/media/media.php @@ -63,7 +63,7 @@ public function onCustomFieldsBeforePrepareField($context, $item, $field) } // Check if the field value is an old (string) value - $field->apivalue = $this->checkValue($field->value); + $field->value = $this->checkValue($field->value); } /** diff --git a/plugins/fields/media/tmpl/media.php b/plugins/fields/media/tmpl/media.php index 53c8015ac0ecf..a9ef2b447ba19 100644 --- a/plugins/fields/media/tmpl/media.php +++ b/plugins/fields/media/tmpl/media.php @@ -20,7 +20,8 @@ $class = ' class="' . htmlentities($class, ENT_COMPAT, 'UTF-8', true) . '"'; } -$value = $field->apivalue; +$value = $field->value; + $buffer = ''; if ($value) diff --git a/tests/Unit/Libraries/Cms/Form/Field/AccessiblemediaFieldTest.php b/tests/Unit/Libraries/Cms/Form/Field/AccessiblemediaFieldTest.php index caa12baf98124..f8a7e1c9fd2ec 100644 --- a/tests/Unit/Libraries/Cms/Form/Field/AccessiblemediaFieldTest.php +++ b/tests/Unit/Libraries/Cms/Form/Field/AccessiblemediaFieldTest.php @@ -190,6 +190,56 @@ public function testSetupWithValueThatIsNotValid() $this->assertEquals('', $accessiblemediafield->__get('value')); } + /** + * Tests method to attach a Form object to the field. + * If the value is a stdClass that is valid. + * This does change nothing. The value should be the same than before. + + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function testSetupWithValueThatIsValidStdclass() + { + $accessiblemediafield = $this->createAccessiblemediaField(); + + $obj = new \stdClass; + $obj->imagefile = '/images/joomla_black.png'; + $obj->alt_text = 'some alt text'; + + $element = new \SimpleXMLElement(''); + + $this->assertTrue($accessiblemediafield->setup($element, $obj, null)); + $this->assertEquals($obj, $accessiblemediafield->__get('value')); + } + + /** + * Tests method to attach a Form object to the field. + * If the value is a stdClass that is not valid for the accessible media field. + * This does change in the setup method. Result is false. + + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function testSetupWithValueThatIsNoValidStdclass() + { + $accessiblemediafield = $this->createAccessiblemediaField(); + + $obj1 = new \stdClass; + $obj2 = new \stdClass; + $obj3 = new \stdClass; + + $obj1->imagefile = '/images/joomla_black.png'; + $obj2->alt_text = 'some alt text'; + + $element = new \SimpleXMLElement(''); + + $this->assertFalse($accessiblemediafield->setup($element, $obj1, null)); + $this->assertFalse($accessiblemediafield->setup($element, $obj2, null)); + $this->assertFalse($accessiblemediafield->setup($element, $obj3, null)); + } + /** * Tests method to attach a Form object to the field. * If the SimpleXMLElement is not of the field type, the setup method returns false.