Fixed broken attachment image in Flex Objects Admin when destination: self@ used [#3225]

This commit is contained in:
Matias Griese
2021-02-16 12:44:30 +02:00
parent 2195bf2307
commit a1306d9eb4
4 changed files with 101 additions and 26 deletions

View File

@@ -32,6 +32,7 @@
* Fixed `<meta name="flattr:*" content="*">` to use name instead property [#3010](https://github.com/getgrav/grav/pull/3010)
* Fixed behavior of opposite filters in `Pages::getCollection()` to match Grav 1.6 [#3216](https://github.com/getgrav/grav/pull/3216)
* Fixed modular content with missing template file ending up using non-modular template [#3218](https://github.com/getgrav/grav/issues/3218)
* Fixed broken attachment image in Flex Objects Admin when `destination: self@` used [#3225](https://github.com/getgrav/grav/issues/3225)
# v1.7.5
## 02/01/2021

View File

@@ -14,6 +14,7 @@ namespace Grav\Common\Flex;
use Grav\Common\Flex\Traits\FlexGravTrait;
use Grav\Common\Flex\Traits\FlexObjectTrait;
use Grav\Framework\Flex\Traits\FlexMediaTrait;
use function is_array;
/**
* Class FlexObject
@@ -25,4 +26,48 @@ abstract class FlexObject extends \Grav\Framework\Flex\FlexObject
use FlexGravTrait;
use FlexObjectTrait;
use FlexMediaTrait;
/**
* {@inheritdoc}
* @see FlexObjectInterface::getFormValue()
*/
public function getFormValue(string $name, $default = null, string $separator = null)
{
$value = $this->getNestedProperty($name, null, $separator);
// Handle media order field.
if (null === $value && $name === 'media_order') {
return implode(',', $this->getMediaOrder());
}
// Handle media fields.
$settings = $this->getFieldSettings($name);
if ($settings['media_field'] ?? false === true) {
return $this->parseFileProperty($value, $settings);
}
return $value ?? $default;
}
/**
* {@inheritdoc}
* @see FlexObjectInterface::prepareStorage()
*/
public function prepareStorage(): array
{
// Remove extra content from media fields.
$fields = $this->getMediaFields();
foreach ($fields as $field) {
$data = $this->getNestedProperty($field);
if (is_array($data)) {
foreach ($data as $name => &$image) {
unset($image['image_url'], $image['thumb_url']);
}
unset($image);
$this->setNestedProperty($field, $data);
}
}
return parent::prepareStorage();
}
}

View File

@@ -280,32 +280,6 @@ class UserObject extends FlexObject implements UserInterface, Countable
return $this->getGroups()->authorize($action, $scope);
}
/**
* Get value from a page variable (used mostly for creating edit forms).
*
* @param string $name Variable name.
* @param mixed $default
* @param string|null $separator
* @return mixed
*/
public function getFormValue(string $name, $default = null, string $separator = null)
{
$value = parent::getFormValue($name, null, $separator);
$settings = $this->getFieldSettings($name);
if ($settings['media_field'] ?? false === true) {
return $this->parseFileProperty($value);
}
if (null === $value) {
if ($name === 'media_order') {
return implode(',', $this->getMediaOrder());
}
}
return $value ?? $default;
}
/**
* @param string $property
* @param mixed $default

View File

@@ -73,6 +73,10 @@ trait FlexMediaTrait
return $media;
}
/**
* @param string $field
* @return array|null
*/
protected function getFieldSettings(string $field): ?array
{
if ($field === '') {
@@ -109,6 +113,57 @@ trait FlexMediaTrait
return $settings + ['accept' => '*', 'limit' => 1000, 'self' => true];
}
protected function getMediaFields(): array
{
// Load settings for the field.
$schema = $this->getBlueprint()->schema();
$list = [];
foreach ($schema->getState()['items'] as $field => $settings) {
if (isset($settings['type']) && (in_array($settings['type'], ['avatar', 'file', 'pagemedia']) || !empty($settings['destination']))) {
$list[] = $field;
}
}
return $list;
}
/**
* @param array|mixed $value
* @param array $settings
* @return array|mixed
*/
protected function parseFileProperty($value, array $settings = [])
{
if (!is_array($value)) {
return $value;
}
$media = $this->getMedia();
$list = [];
foreach ($value as $filename => $info) {
if (!is_array($info)) {
continue;
}
/** @var Medium|null $thumbFile */
$imageFile = $media[$filename];
$url = $imageFile ? $imageFile->url() : null;
$list[$filename] = [
'name' => $info['name'] ?? null,
'type' => $info['type'] ?? null,
'size' => $info['size'] ?? null,
'path' => $filename,
'image_url' => $url,
'thumb_url' => $url
];
}
return $list;
}
/**
* @param UploadedFileInterface $uploadedFile
* @param string|null $filename