Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
WilsonParker opened this issue Aug 25, 2023 · 9 comments · May be fixed by #425
Open

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

WilsonParker opened this issue Aug 25, 2023 · 9 comments · May be fixed by #425

Comments

@WilsonParker
Copy link

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
Copy link
Author

WilsonParker commented Aug 25, 2023

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
Copy link

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

@jan-tricks
Copy link

@ebess : The problem is caused by overwriting $requestAttribute in

$requestAttribute = "{$key}.{$index}.{$field->attribute}";

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

@manuglopez
Copy link

Is there any solution to this issue?

@Napia
Copy link

Napia commented Jan 11, 2024

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

@rik43
Copy link

rik43 commented Jan 15, 2024

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
Copy link

fsavina-akqa commented Feb 22, 2024

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

@AJSolis
Copy link

AJSolis commented Mar 1, 2024

Hello @ebess, What are the plans for this to be merged. I was able to test this pull request locally #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
Copy link

srchema commented Mar 21, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
9 participants