<p>This is totally untested but something like this could work (or at least give you some hints)</p>

<p><strong>edit:</strong> since I was bored I tested the code.. it works</p>

<pre><code>public function actionImport(){
    $jsonData = // getYouJsonDatasomehow here
    foreach($jsonData as $json){
        $entry = craft()-&gt;elements-&gt;getElementById($json['id']);
        /** @var \Craft\MatrixBlockModel[] $matrix */
        $matrix = $entry-&gt;Content-&gt;find();
        /** @var \Craft\MatrixBlockModel $block */
        foreach ($matrix as $block){
            // I pretend there is already the required block for the entry otherwise you have to create it

            if($block-&gt;getType()-&gt;handle === 'allImages'){
                // long version...
                $attributes = $block-&gt;getContent()-&gt;getAttributes();
                $attributes['images'] = $json['imageIds'];
                // some other changes if you want... title or whatever
                $block-&gt;getContent()-&gt;setAttributes($attributes);

                // short version would be
                $block-&gt;getContent()-&gt;setAttribute('images', $json['imageIds']);
                craft()-&gt;matrix-&gt;saveBlock($block);
            }
        }

    }
}
</code></pre>

<p>Since you did not say if the blocks/entries already exist or if the images even exist or if they have to be imported as well I decided to pretend everything is ready to use and your json images contain just an array of ids. If your images contains only local paths you would have to import them first with something like</p>

<pre><code>$arrImages = [];
foreach($json['images'] as $image){
    $path = $image['path'];
    $name = $image['name'];

    $response = craft()-&gt;assets-&gt;insertFileByLocalPath(
        $yourFilePath,
        $nameOfImage,
        $folderId,
        AssetConflictResolution::Replace
    );

    if($newFildeId = $response-&gt;getResponseData()['fileId']){
        $arrImages[] = $newFildeId;
    }
}


$block-&gt;getContent()-&gt;setAttribute('images', $arrImages);
</code></pre>

CC BY-SA 3.0
2018-01-04T15:50:15.170Z
2018-01-04T16:12:32.637Z
2018-01-04T16:12:32.637Z
Robin Schambach
<p>I have a json file with a bunch of id's &amp; associated images.</p>

<p>The id's relate to existing entries in my Craft installation. </p>

<p>I have a Matrix field called Content with a block type called allImages - the allImages block type has a single field called images defined as an Asset field type. </p>

<p>What I would like to do is import the json file &amp; update the existing entries with the images as defined in the json.</p>

<p>I cannot for the life of me figure out how to add images to the Matrix field in code. There are plenty of samples around submitting post variables from a custom form to the saveEntry action.</p>

<p>Any tips would be greatly appreciated </p>

<plugin-development><assets><matrix><craft.entries>
How do I programmatically add assets to an assets fieldtype on a Matrix
# How do I programmatically add assets to an assets fieldtype on a Matrix

(Asked by: username_0 on 2018-01-04)

I have a json file with a bunch of id's & associated images.

The id's relate to existing entries in my Craft installation.

I have a Matrix field called Content with a block type called allImages - the allImages block type has a single field called images defined as an Asset field type.

What I would like to do is import the json file & update the existing entries with the images as defined in the json.

I cannot for the life of me figure out how to add images to the Matrix field in code. There are plenty of samples around submitting post variables from a custom form to the saveEntry action.

Any tips would be greatly appreciated

**Tags:** plugin-development, assets, matrix, craft.entries

---

## 1 answers

### Answer by username_1 on 2018-01-04. Score: 2 | Accepted answer

This is totally untested but something like this could work (or at least give you some hints)
edit: since I was bored I tested the code.. it works
public function actionImport(){
    $jsonData = // getYouJsonDatasomehow here
    foreach($jsonData as $json){
        $entry = craft()->elements->getElementById($json['id']);
        /** @var \Craft\MatrixBlockModel[] $matrix */
        $matrix = $entry->Content->find();
        /** @var \Craft\MatrixBlockModel $block */
        foreach ($matrix as $block){
            // I pretend there is already the required block for the entry otherwise you have to create it

            if($block->getType()->handle === 'allImages'){
                // long version...
                $attributes = $block->getContent()->getAttributes();
                $attributes['images'] = $json['imageIds'];
                // some other changes if you want... title or whatever
                $block->getContent()->setAttributes($attributes);

                // short version would be
                $block->getContent()->setAttribute('images', $json['imageIds']);
                craft()->matrix->saveBlock($block);
            }
        }

    }
}

Since you did not say if the blocks/entries already exist or if the images even exist or if they have to be imported as well I decided to pretend everything is ready to use and your json images contain just an array of ids. If your images contains only local paths you would have to import them first with something like
$arrImages = [];
foreach($json['images'] as $image){
    $path = $image['path'];
    $name = $image['name'];

    $response = craft()->assets->insertFileByLocalPath(
        $yourFilePath,
        $nameOfImage,
        $folderId,
        AssetConflictResolution::Replace
    );

    if($newFildeId = $response->getResponseData()['fileId']){
        $arrImages[] = $newFildeId;
    }
}


$block->getContent()->setAttribute('images', $arrImages);


---

