I'm having an issue creating a matrix block from a module. I'm currently doing the following:
- Calling an API using Guzzle
- Getting that request and iterating over a
media
block. - Trying to create a new Matrix Block with each iteration.
- Adding data from the request into each Matrix Block.
** EDIT **
I got this working using the following code:
$data = json_decode($json, true);
$number = 1;
foreach($data['media'] as $element) {
$matrixData = array();
$matrixData["new$number"] = array(
'type' => 'media',
'enabled' => true,
'fields' => array(
'mediaDescription' => $element['likes'],
)
);
$user->setFieldValue('recentPosts', $matrixData);
$number++;
}
Example JSON
"media": [
{
"media_id": "2053313765945828720",
"instagram_user_id": "1",
"shortcode": "Bx-1f2_nuVw",
"created_date": "1558994096",
"caption": "",
"comments": "7077",
"likes": "1131719",
"media_url": "https://www.instagram.com/p/Bx-1f2_nuVw",
"media_image_url": "https://scontent-lhr3-1.cdninstagram.com/vp/8e6c054cec7d3c3657bc73d49dae0fd9/5D7B651B/t51.2885-15/e35/c0.134.1080.1080/s150x150/61993210_834852143539503_2474404095511077560_n.jpg?_nc_ht=scontent-lhr3-1.cdninstagram.com",
"type": "IMAGE",
"mentions": [],
"hashtags": [],
"date": "2019-05-28 02:44:09",
"last_check_date": "2019-05-28 20:25:24"
},
...
Unfortunately, now my forloop isn't saving new matrix blocks.