From a1306d9eb404f27b706bbe1489c4cdd87f265eec Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Tue, 16 Feb 2021 12:44:30 +0200 Subject: [PATCH] Fixed broken attachment image in Flex Objects Admin when `destination: self@` used [#3225] --- CHANGELOG.md | 1 + system/src/Grav/Common/Flex/FlexObject.php | 45 +++++++++++++++ .../Common/Flex/Types/Users/UserObject.php | 26 --------- .../Framework/Flex/Traits/FlexMediaTrait.php | 55 +++++++++++++++++++ 4 files changed, 101 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a729ebaf4..2a6511b44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ * Fixed `` 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 diff --git a/system/src/Grav/Common/Flex/FlexObject.php b/system/src/Grav/Common/Flex/FlexObject.php index bb62d4104..2a43eaa44 100644 --- a/system/src/Grav/Common/Flex/FlexObject.php +++ b/system/src/Grav/Common/Flex/FlexObject.php @@ -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(); + } } diff --git a/system/src/Grav/Common/Flex/Types/Users/UserObject.php b/system/src/Grav/Common/Flex/Types/Users/UserObject.php index 1d803bd6a..4b87824b6 100644 --- a/system/src/Grav/Common/Flex/Types/Users/UserObject.php +++ b/system/src/Grav/Common/Flex/Types/Users/UserObject.php @@ -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 diff --git a/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php b/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php index 27ad3f814..6db2eecfe 100644 --- a/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php +++ b/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php @@ -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