bobdenotter / conimex

🍛 Content Importer and Exporter (as YAML) for Bolt 4
MIT License
6 stars 15 forks source link

Unable to import custom content with null slug #61

Open michaelborn opened 2 years ago

michaelborn commented 2 years ago

If we have a content type with no slug field defined, and we use conimex to export that content, the slug field will be null.

    -
        id: 80
        contentType: reviews
...
        fields:
            name: PuzzleBoxWorld
            content: 'An amazing sequence of steps to open and somewhat difficult to even put back in the original position. Very imaginative!'
            featured: true
            slug: null

Attempting to import this content fails, even though the content worked fine in the source Bolt website.

Error:

8/72 [=====>--------------------------------------------]  11%TypeError {#4502
  #message: "Argument 1 passed to Bolt\Common\Str::slug() must be of the type string, null given, called in /home/user/mySite/vendor/bobdenotter/conimex/src/Import.php on line 143"
  #code: 0
  #file: "./vendor/bolt/common/src/Str.php"
  #line: 235
  trace: {
    ./vendor/bolt/common/src/Str.php:235 { …}
    ./vendor/bobdenotter/conimex/src/Import.php:143 { …}
    ./vendor/bobdenotter/conimex/src/Import.php:118 { …}
    ./vendor/bobdenotter/conimex/src/Import.php:87 { …}
    ./vendor/bobdenotter/conimex/src/Command/ImportCommand.php:62 { …}
    ./vendor/symfony/console/Command/Command.php:298 { …}
    ./vendor/symfony/console/Application.php:1042 { …}
    ./vendor/symfony/framework-bundle/Console/Application.php:96 { …}
    ./vendor/symfony/console/Application.php:299 { …}
    ./vendor/symfony/framework-bundle/Console/Application.php:82 { …}
    ./vendor/symfony/console/Application.php:171 { …}
    ./bin/console:48 {
      › $application = new Application($kernel);
      › $application->run($input);
      › 
    }
  }
}
michaelborn commented 2 years ago

I highly recommend we change Import.php to not attempt to find the content by slug if the slug is null:

$content = null; // NEW LINE
if ( !is_null( $slug ) ){ // NEW LINE
    $content = $this->contentRepository->findOneBySlug(Str::slug($slug), $contentType);
} // NEW LINE
if (! $content) {
    $content = new Content($contentType);
    $content->setAuthor($user);
}

See https://github.com/bobdenotter/conimex/blob/master/src/Import.php#L143

michaelborn commented 2 years ago

It looks as though we'll need to wrap an is_null() check around line 356 as well:

if ( !is_null( $item ) ){
    $this->contentEditController->updateField($field, $item, null);
}

See https://github.com/bobdenotter/conimex/blob/master/src/Import.php#L359

Senne commented 1 year ago

I think this PR fixes this issue, but it hasn't been merged in yet: https://github.com/bobdenotter/conimex/pull/64