advanced-nova-media-library icon indicating copy to clipboard operation
advanced-nova-media-library copied to clipboard

When there are multiple custom properties, only the first property is updated

Open WilsonParker opened this issue 1 year ago • 9 comments

I use 4.1.6 version When the code is as below, only 'a' is updated Images::make('Images', 'gallery') ->customPropertiesFields([ Text::make('a'), Text::make('b'), ])

WilsonParker avatar Aug 25 '23 08:08 WilsonParker

I changed the code below to

$key = str_replace($collection, '__media-custom-properties__.'.$collection, $requestAttribute);

// replace to $key = '__media-custom-properties__.' . $collection;

original code is as below

namespace Ebess\AdvancedNovaMediaLibrary\Fields;

trait HandlesCustomPropertiesTrait
...
private function fillMediaCustomPropertiesFromRequest(
         ...
         $key = str_replace($collection, '__media-custom-properties__.'.$collection, $requestAttribute);
}

WilsonParker avatar Aug 25 '23 08:08 WilsonParker

Up on this issue. I can't make it worked eather. @ebess is it possible to fix this ?

paulineohlivd avatar Sep 01 '23 14:09 paulineohlivd

@ebess : The problem is caused by overwriting $requestAttribute in https://github.com/ebess/advanced-nova-media-library/blob/83b8410fe63b607dc7eda6b990cef33f971a6f9f/src/Fields/HandlesCustomPropertiesTrait.php#L85

Renaming the new variable to something like $targetRequestAttribute should fix the problem.

jan-tricks avatar Sep 05 '23 15:09 jan-tricks

Is there any solution to this issue?

manuglopez avatar Oct 27 '23 15:10 manuglopez

@jan-tricks solution works for me ... could you fix this? thank you!

Napia avatar Jan 11 '24 09:01 Napia

I have some issue with 5 custom fields (where saved only first).

if i print dump($requestAttribute); in fillMediaCustomPropertiesFromRequest then receive:

"__media-custom-properties__.images.0.alt"
"__media-custom-properties__.__media-custom-properties__.images.0.alt.0.url"
"__media-custom-properties__.__media-custom-properties__.__media-custom-properties__.images.0.alt.0.url.0.adv_token"
"__media-custom-properties__.__media-custom-properties__.__media-custom-properties__.__media-custom-properties__.images.0.alt.0.url.0.adv_token.0.date_from"
"__media-custom-properties__.__media-custom-properties__.__media-custom-properties__.__media-custom-properties__.__media-custom-properties__.images.0.alt.0.url.0.adv_token.0.date_from.0.date_to"

my solution to put the $key = str_replace(...) code before the foreach loop:

    private function fillMediaCustomPropertiesFromRequest(NovaRequest $request, Media $media, int $index, string $collection, string $requestAttribute): void
    {
        // prevent overriding the custom properties set by other processes like generating conversions
        $media->refresh();

        // If we are dealing with nested resources or multiple panels, custom property fields are prefixed.
        $key = str_replace($collection, '__media-custom-properties__.'.$collection, $requestAttribute);

        /** @var Field $field */
        foreach ($this->customPropertiesFields as $field) {
            $targetAttribute = "custom_properties->{$field->attribute}";
            $requestAttribute = "{$key}.{$index}.{$field->attribute}";

            $field->fillInto($request, $media, $targetAttribute, $requestAttribute);
        }
        $media->save();
    }        

PS but the solution above (PR) should also work.

rik43 avatar Jan 15 '24 12:01 rik43

Hi @ebess, any plans fot the PR to be merged? Thanks

fsavina-akqa avatar Feb 22 '24 07:02 fsavina-akqa

Hello @ebess, What are the plans for this to be merged. I was able to test this pull request locally https://github.com/ebess/advanced-nova-media-library/pull/425 and was successful. I went ahead and threw a approve on it as well but not sure if that means anything for you

AJSolis avatar Mar 01 '24 19:03 AJSolis

I have some issue with 5 custom fields (where saved only first).

if i print dump($requestAttribute); in fillMediaCustomPropertiesFromRequest then receive:

"__media-custom-properties__.images.0.alt"
"__media-custom-properties__.__media-custom-properties__.images.0.alt.0.url"
"__media-custom-properties__.__media-custom-properties__.__media-custom-properties__.images.0.alt.0.url.0.adv_token"
"__media-custom-properties__.__media-custom-properties__.__media-custom-properties__.__media-custom-properties__.images.0.alt.0.url.0.adv_token.0.date_from"
"__media-custom-properties__.__media-custom-properties__.__media-custom-properties__.__media-custom-properties__.__media-custom-properties__.images.0.alt.0.url.0.adv_token.0.date_from.0.date_to"

my solution to put the $key = str_replace(...) code before the foreach loop:

    private function fillMediaCustomPropertiesFromRequest(NovaRequest $request, Media $media, int $index, string $collection, string $requestAttribute): void
    {
        // prevent overriding the custom properties set by other processes like generating conversions
        $media->refresh();

        // If we are dealing with nested resources or multiple panels, custom property fields are prefixed.
        $key = str_replace($collection, '__media-custom-properties__.'.$collection, $requestAttribute);

        /** @var Field $field */
        foreach ($this->customPropertiesFields as $field) {
            $targetAttribute = "custom_properties->{$field->attribute}";
            $requestAttribute = "{$key}.{$index}.{$field->attribute}";

            $field->fillInto($request, $media, $targetAttribute, $requestAttribute);
        }
        $media->save();
    }        

PS but the solution above (PR) should also work.

Thank you!! It works for me!!!

srchema avatar Mar 21 '24 10:03 srchema