2

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.

2

Fixed using the following code. Also includes fetching an API via Guzzle and parsing it into Craft CMS. Thought I would post here so that if anyone else is having trouble they can decipher bits from this.

                // Set a base URI
                $baseUri = 'http://domain.com';
                // Build the Guzzle Client and use the Base URI
                $client = Craft::createGuzzleClient([
                  'base_uri' => $baseUri,
                ]);
                // Send a Request!
                $response = $client->request('GET', '/mytestendpoint/');
                // Parse the Response
                $json = $response->getBody();
                // Decode it
                $data = json_decode($json, true);

                // Set some values from it!
                $user->setFieldValue('userTotalFollowers', $data['followers'] );
                $user->setFieldValue('userEngagementRating', $data['details']['total_likes'] );
                $user->setFieldValue('verified', $data['is_verified'] );

                $number = 0;
                $matrixData = array();

                foreach($data['media'] as $element) {
                  $number++;
                  $matrixData["new$number"] = array(
                    'type' => 'media',
                    'enabled' => true,
                    'fields' => array(
                      'mediaDescription' => $element['likes'],
                      'mediaLikes' => count($element),
                    )
                  );
                }
                $user->setFieldValue('recentPosts', $matrixData);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.