2

I have a matrix with a field to set a Entries URL. Unfortunately the entry is not being returned, only an empty object.

Here are my field settings below and element api code.

See below for the response within the json. I get an empty object?

Why is no data coming through?

enter image description here

I'm fetching the data within a function like so:

enter image description here

enter image description here

Here is the code from my Element API:

function panelsMatrix($array) {
    $panelBlocks = [];
    foreach ($array as $block) {
        switch ($block->type->handle) {
            case 'panel':
                $panelBlocks[] = [
                    'heading' => (string) $block->heading,
                    'text' => (string) $block->text,
                    'image' => $block->image[0]->url,
                    'imagePosition' => $block->imagePosition,
                    'textBaseColour' => $block->textBaseColour,
                    'textHorizontalAlignment' => $block->textHorizontalAlignment,
                    'textVerticalAlignment' => $block->textVerticalAlignment,
                    'imageOffsetX' =>$block->imageOffsetX,
                    'imageOffsetY' =>$block->imageOffsetY,
                    'backgroundColour' => $block->backgroundColour,
                    'subPageDirect' => $block->subPageDirect->first()
                ];
            break;
        }
    }
    return $panelBlocks;
}
1

That's because you don't actually fetch the entry. In order to receive the entry you have to execute your ElementCriteriaModel with first() or find() otherwise Craft tries to json encode a query

 $block->subPageDirect->first()

You should read the docs about relations and the Element Criteria Model

Btw stack exchange has a wonderful syntax highlighting please add your code directly the next time and not in a screenshot

Edit:

function panelsMatrix($array) {
    $panelBlocks = [];
    foreach ($array as $block) {
        switch ($block->type->handle) {
            case 'panel':
                $relatedEntries =  $block->subPageDirect->first();
                $attributes = $relatedEntries? $relatedEntries->getAttributes() : [];

                $panelBlocks[] = [
                    'heading' => (string) $block->heading,
                    'text' => (string) $block->text,
                    'image' => $block->image[0]->url,
                    'imagePosition' => $block->imagePosition,
                    'textBaseColour' => $block->textBaseColour,
                    'textHorizontalAlignment' => $block->textHorizontalAlignment,
                    'textVerticalAlignment' => $block->textVerticalAlignment,
                    'imageOffsetX' =>$block->imageOffsetX,
                    'imageOffsetY' =>$block->imageOffsetY,
                    'backgroundColour' => $block->backgroundColour,
                    'subPageDirect' => $attributes
                ];
            break;
        }
    }
    return $panelBlocks;
}
|improve this answer|||||
  • I think the use of the screenshots is appropriate this time, it helps explaining his problem better. ☺ – Rosana Ruiz Mar 9 '18 at 18:08
  • Why should a screenshot be better than actual code that you can copy, paste and test. You can't copy the code from a screenshot, so I don't see any advantage – Robin Schambach Mar 9 '18 at 18:10
  • Hi @Robin Schambach, thanks for this. I tried using your code however this didn't work. The only change I saw is that the data is returning null when an entry has NOT been selected. When an entry has been selected the data returns an empty object. – Filth Mar 12 '18 at 11:00
  • Of course you have to define the attributes you want to return from this element otherwise you'll receive an empty array. I would have postet the entire code but you only gave us a screenshot so copy&paste didn't work. – Robin Schambach Mar 12 '18 at 11:11
  • Updated my question with code from my Element API. – Filth Mar 12 '18 at 11:16

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.