0

Using the Element API, I want to return the values from checkboxes (the $block->product field). These checkboxes are within a matrix that I am already gathering data from. Currently am doing this:

return [
'endpoints' => [
    'fullmembers.json' => [
        'elementType' => 'User',
        'criteria' => ['group' => 'fullMembership'],
        'transformer' => function(UserModel $member) {
            $businessCategories = [];
            foreach ($member->businessTypeMarketSectorsProducts as $block) {
                switch ($block->type->handle) {
                    case 'businessCategory':

                        $businessCategories[] = [
                            'businessType' => $block->businessType->value,
                            'marketSector' => $block->marketSector->value,
                            'product' => $block->product //Here you can see I'm calling the product field... 
                        ];
                        break;
                }
            }

            return [
                'id' => $member->id,
                'companyName' => $member->membersCompanyName,
                'addressLine3' => $member->membersAddressLine3,
                'lat' => $member->memberAddress->lat,
                'lng' => $member->memberAddress->lng,
                'email' => $member->email,
                // 'profile' => '/directory/company/' . $member->id . '/' . $member->username,
                'profile' => UrlHelper::getUrl("directory/company/{$member->id}"),
                'businessCategories' => $businessCategories
            ];
        },
    ]
]
];

Here is a sample response:

    {
  "id": "1506",
  "companyName": "Airsprung Group Plc",
  "addressLine3": "",
  "lat": "51.33188454",
  "lng": "-2.20657303",
  "email": "[email protected]",
  "profile": "http:\/\/craft.fira\/directory\/company\/1506",
  "businessCategories": [{
    "businessType": "Manufacture",
    "marketSector": "Domestic",
    "product": {
      "0": {
        "label": "Beds",
        "value": "Beds",
        "selected": true
      },
      "1": {
        "label": "Mattresses",
        "value": "Mattresses",
        "selected": true
      },
      "2": {
        "label": "Seating \/ upholstery",
        "value": "Seating \/ upholstery",
        "selected": true
      }
    }
  }, {
    "businessType": "Manufacture",
    "marketSector": "Contract",
    "product": {
      "0": {
        "label": "Beds",
        "value": "Beds",
        "selected": true
      },
      "1": {
        "label": "Mattresses",
        "value": "Mattresses",
        "selected": true
      },
      "2": {
        "label": "Seating \/ upholstery",
        "value": "Seating \/ upholstery",
        "selected": true
      }
    }
  }]
}

I would like the response to just show the values of the product checkboxes. Can you suggest a way to do this so that I am not returning the label and selected states? Ideally something like this:

"product": {"Beds", "Mattresses", "Seating \/ upholstery"}

1 Answer 1

1

You should be able to foreach the $block->product to include the selected values (or labels)

return [
    'endpoints' => [
        'fullmembers.json' => [
            'elementType' => 'User',
            'criteria' => ['group' => 'fullMembership'],
            'transformer' => function(UserModel $member) {
                $businessCategories = [];
                foreach ($member->businessTypeMarketSectorsProducts as $block) {
                    switch ($block->type->handle) {
                        case 'businessCategory':
                            $productValues = [];
                            foreach ($block->product as $product) {
                                if ($product->selected) {
                                    $productValues[] = $product->value;
                                }
                            }
                            $businessCategories[] = [
                                'businessType' => $block->businessType->value,
                                'marketSector' => $block->marketSector->value,
                                'product' => $productValues
                            ];
                            break;
                    }
                }

                return [
                    'id' => $member->id,
                    'companyName' => $member->membersCompanyName,
                    'addressLine3' => $member->membersAddressLine3,
                    'lat' => $member->memberAddress->lat,
                    'lng' => $member->memberAddress->lng,
                    'email' => $member->email,
                    // 'profile' => '/directory/company/' . $member->id . '/' . $member->username,
                    'profile' => UrlHelper::getUrl("directory/company/{$member->id}"),
                    'businessCategories' => $businessCategories
                ];
            },
        ]
    ]
];
1
  • 1
    Thanks chris. You've nailed it! The output is now: {"businessType":"Manufacture","marketSector":"Contract","product":["Beds","Mattresses","Seating \/ upholstery"]}]}, Jan 29, 2016 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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