Skip to content

Unable to Import Featured Images During theme:seed (Importer Rejects featured_image_urls) #591

@SamBrishes

Description

@SamBrishes

I’m trying to seed RainLab.Blog posts using php artisan theme:seed command, and I’d like to include demo images as well.

Thus, in my data.yaml, I first import the media files, then the RainLab.Blog posts:

-
    name: Demo Media Files
    class: Media\Models\MediaLibraryItemImport
    file: seeds/data/media-files.json
    attributes:
        file_format: json
-
    name: Demo RainLab.Blog Posts
    class: RainLab\Blog\Models\PostImport
    file: seeds/data/rainlab-blog.json
    attributes:
        file_format: json
        auto_create_categories: true

My media-files.json is as simple as

[
    {
        "type": "folder",
        "path": "newshub/demo",
        "source": "seeds/media"
    }
]

The problem is that the images cannot be imported. The exported Blog Posts contain a featured_image_urls attribute (which is empty, though), but the importer rejects it because this attribute does not exist on the Post model.

Error: SQLSTATE[HY000]: General error: 1 table rainlab_blog_posts has no column named featured_image_urls

My intended approach was to stay using the featured_image_urls, but - at least for the seeder - with internal storage paths that already exist from the media importer. Example Post:

[

    {
        "title": "Title",
        "categories": ["Articles", "Featured"],
        "created_at": "2022-08-13T14:53:24.000000Z",
        "updated_at": "2022-08-13T14:53:24.000000Z",
        "published_at": "2022-08-13 14:53:14",
        "content": "Post Content.",
        "content_html": "<p>Post HTML Content</p>",
        "excerpt": "",
        "slug": "title",
        "author_email": "[email protected]",
        "featured_image_urls": [
            "newshub/demo/hero-image-01.jpg"
        ],
        "published": 1
    },
]

Then in your PostImport.php on line 67, create File objects like this:

foreach (array_except($data, $except) as $attribute => $value) {
    if (in_array($attribute, $post->getDates()) && empty($value)) {
        continue;
    }
    if ($attribute == 'featured_image_urls') {
        $value = is_array($value) ? $value : [$value];
        $files = [];
        foreach ($value AS $image) {
            $filePath = storage_path('app/media/' . $image);
            if (empty($image) || !file_exists($filePath) || !is_file($filePath)) {
                continue;
            }

            $files[] = (new \System\Models\File)->fromFile(
                $filePath,
                basename($image)
            );
        }
        if (!empty($files)) {
            $post->featured_images = $files;
        }
    } else {
        $post->{$attribute} = isset($value) ? $value : null;
    }
}

Or is there a more native or recommended way to import images along with the Blog posts?

Thanks.

~Sam.

Metadata

Metadata

Assignees

No one assigned

    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