1

How can I use Element API to move my entry fields into a matrix-based content builder? I'm going off the docs found at https://docs.craftcms.com/feed-me/v4/guides/importing-into-matrix.html#setup-your-feed but I can't return a key for matrixBlock multiple times, as it overwrites it with the last instance.

'export/departments.json' => function () {
  return [
    'elementType' => Entry::class,
    'criteria' => [
      'section' => 'departments',
      'type' => 'departments'
    ],
    'transformer' => function (Entry $entry) {
      return [
        'title' => $entry->title,
        'id' => $entry->id,
        'matrix' => [
          'serviceBlock' => !empty($entry->pageServices->all()) ? [
            'title' => "Services",
            cbEntries("pageServices", $entry)
          ] : null,
          'resourceBlock' => !empty($entry->pageResources->all()) ? [
            'title' => "Resources",
            cbEntries("pageResources", $entry)
          ] : null,
          'documentBlock' => !empty($entry->pageDocuments->all()) ? [
            'title' => "Documents",
            cbEntries("pageDocuments", $entry)
          ] : null,
        ]
      ];
    },
  ];
},

Right now this is where I'm at with my elements. I tried naming them all matrixBlock but as a result, the last element, or documentBlock as its called here would render.

For reference, here’s the cbEntries function I’m using, as well:

function cbEntries($section, $element)
{
  $sectionArray = [];
  if (!empty($element->$section)) {
    foreach ($element->$section as $value) {
      $sectionArray[] = $value->id;
    }
  }
  return [$sectionArray];
}

Your Answer

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

Browse other questions tagged or ask your own question.