ebess / advanced-nova-media-library

A Laravel Nova field for displaying, creating, updating and ordering a Spatie Media Library model.
596 stars 297 forks source link

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

Open WilsonParker opened 1 year ago

WilsonParker commented 1 year ago

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 commented 1 year ago

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);
}
paulineohlivd commented 1 year ago

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

jan-tricks commented 1 year ago

@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.

manuglopez commented 1 year ago

Is there any solution to this issue?

Napia commented 10 months ago

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

rik43 commented 10 months ago

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.

fsavina-akqa commented 9 months ago

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

AJSolis commented 8 months ago

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

srchema commented 8 months ago

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!!!