Skip to content

Commit

Permalink
Fixed broken attachment image in Flex Objects Admin when `destination…
Browse files Browse the repository at this point in the history
…: self@` used [#3225]
  • Loading branch information
mahagr committed Feb 16, 2021
1 parent 2195bf2 commit a1306d9
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions system/src/Grav/Common/Flex/FlexObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Grav\Common\Flex\Traits\FlexGravTrait;
use Grav\Common\Flex\Traits\FlexObjectTrait;
use Grav\Framework\Flex\Traits\FlexMediaTrait;
use function is_array;

/**
* Class FlexObject
Expand All @@ -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();
}
}
26 changes: 0 additions & 26 deletions system/src/Grav/Common/Flex/Types/Users/UserObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,32 +280,6 @@ public function authorize(string $action, string $scope = null): ?bool
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
Expand Down
55 changes: 55 additions & 0 deletions system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public function getMedia()
return $media;
}

/**
* @param string $field
* @return array|null
*/
protected function getFieldSettings(string $field): ?array
{
if ($field === '') {
Expand Down Expand Up @@ -109,6 +113,57 @@ protected function getMediaFieldSettings(string $field): array
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
Expand Down

0 comments on commit a1306d9

Please sign in to comment.