Skip to content

Copying asset to component gives CDN url #22

@Noaber

Description

@Noaber

We get a story from using the Management API and copy that over to a new story with altered data and images.
When copying an asset using the Asset API into a Component the asset is saved using the CDN url.

This goes wrong $nestedImage->setAsset('image', $asset);
This works $nestedImage->setData($galleryItem);


Expected Behavior

I expect the Storyblok asset url (a.storyblok.com) instead of the CDN url

Current Behavior

I get the CDN url

Steps to Reproduce / Code

if ($block['component'] === 'standaard_content_gallery') {
                $offSetGallerySection[] = $index;

                // loop over every gallery item
                foreach ($block['gallery_items'] as $galleryItem) {
                    // just copy from the template
                    if ($galleryItem['component'] === 'nested_image') {
                        $nestedImage = new StoryComponent('nested_image');
                        $nestedImage->setData($galleryItem);

                        $galleryImages['nested_image'] = $nestedImage;
                    }

                    // get image based on root category, sub category and company
                    if ($galleryItem['component'] === 'nested_image_categories_with_sub_company') {
                        $company = $this->vacancy->details()->where('name', 'relation')->first()?->value;

                        $asset = $this->assetService
                            ->getAssetsByTags([strtolower($this->vacancy->root_category->name), strtolower($this->vacancy->category->name), strtolower($company)])
                            ->randomize();

                        $nestedImage = new StoryComponent('nested_image');
                        if ($asset !== null) {
                            $nestedImage->setAsset('image', $asset);

                            $this->log['vacancy']['assets']['gallery']['nested_image_categories_with_sub_company'] = $nestedImage->toArray();
                        }
                        $galleryImages['nested_image_categories_with_sub_company'] = $nestedImage;
                    }
                }
            }

In above snippet we are loop over a component and try to add assets based on the gallery item component type.
So for $galleryItem['component'] === 'nested_image' we just copy the asset from the template (this is a Story we get before this loop). This asset is shown in the blade template (we use Laravel)

But for $galleryItem['component'] === 'nested_image_categories_with_sub_company' we get an asset based on tags from the Asset API. The randomize() function returns an specific (but random :) asset.
Now we try to set the asset using setAsset. This works in the Storyblok app. We see the image, however in the blade file we don't get the a.storyblok.com url, but the CDN url.

below the Asset API call. The randomize() function gets just 1 Asset of the $this->assets array

public function getAssetsByTags(array $tags): static
    {
        $this->assetApi = new AssetApi($this->client, $this->spaceId);

        $params = [
            'withTags' => implode(',', $tags),
        ];

        // If folder is set, add it
        if ($this->folder !== null) {
            $params['inFolder'] = $this->folder;
        }

        $this->assets = $this->assetApi->page(
            new AssetsParams(...$params),
        )->data();

        $this->tags = $tags;

        return $this;
    }

Hopefully above "explanation" makes sense :)

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions