kql icon indicating copy to clipboard operation
kql copied to clipboard

how to select and transform data from structures

Open bnomei opened this issue 4 years ago • 8 comments

i tried field.toArray and field.toStructure combined with arrayItem and structureItem like in query language but that did not work. best i could find out was to use field.yaml or a custom pagemethod.

i would love to see support for arrayItem and structureItem in kql since that would be most consistent imho.

bnomei avatar Mar 31 '20 12:03 bnomei

{
    "query": "page('guides/print/business-materials').children",
    "select": {
        "url": true,
        "title": true,
        "id": true,
        "slides": {
            "query": "page.slides.toStructure",
            "select": {
                "image": {
                    "query": "structureItem.image.toFile",
                    "select": {
                    	"url": true,
                    	"srcset": "file.srcset([300, 800, 1024])"
                    }
                },
                "description" : "structureItem.description"
            }
        }
    },
    "pagination": {
        "limit": 10
    }
}

This works for me

ghost avatar Mar 31 '20 13:03 ghost

thanks @kms-rscholz i tried something similar but must have made an error somewhere.

bnomei avatar Apr 02 '20 09:04 bnomei

i can not get it to work.

        fields:
          action:
            label: Aktionen
            type: structure
            fields:
              link:
                type: text
                required: true
              title:
                type: text
                required: true
{
  "query": "page('somepage')",
  "select": {
     "thisworksbutnofieldmethods": "page.action.yaml",
    "thisdoesnotwork": {
      "query": "page.action.toStructure",
      "select": {
        "link": "structureItem.link",
        "title": "structureItem.title.html"
      }
    }
  }
}

bnomei avatar Apr 11 '20 15:04 bnomei

"actions": {
      "query": "page.action.toStructure",
      "select": {
        "link": true,
        "title": true
      }
    }

does work. but it seems to me structureItem does not.

bnomei avatar Apr 11 '20 15:04 bnomei

I feel like structureItem above might reference a field with that name and not a method. So if your fields are named differently, the example will not work out of the box.

If we consider toStructure from the field methods documentation to be the right method, this actually works (as you pointed out). As in the following example, I have a structure field called "items", so to correctly convert to an JSON structure, the items property looks like this:

{
  "select": {
    "title": true,
    "id": true,
    "slug": true,
    "items": "page.items.toStructure"
  }
}

renestalder avatar Nov 19 '20 16:11 renestalder

I justed tested this an structureItem is actually a method and not a field name.

This works for me:


// mediaStructure => name of the structure field
// mediaImage => name of the image/files field of mediaStructure
// structureItem => seems to be the reference for the query inside mediaStructure query
query: "page('projects/" + params.slug + "')",
  select: {
    title: true,
    id: true,
    mediaStructure: {
      query: 'page.mediaStructure.toStructure',
      select: {
        mediaImage: {
          query: 'structureItem.mediaImage.toFile'
        }
      }
   }
}

Could we make this part of the documentation? That would be great! Happy to help out if needed.

maxfrischknecht avatar Apr 30 '21 11:04 maxfrischknecht

I have a similar issue with values from a multiselect field…

const query = {
      query: `site.children.filterBy('uid', 'projekte').first`,
      language: this.$i18n.locale,
      select: {
        title: true,
        uid: true,
        projects: {
          query: 'page.children.listed',
          select: {
            title: true,
            uid: true,
            uri: true,
            year: true,
            student: {
              query: 'page.student.split',
              select: {
                // this will return null
                autoid: {
                  query: 'arrayItem.value',
                },
              },
            },
          },
        },
      },
    }

marco-land avatar Mar 09 '22 12:03 marco-land

+1

vin-ni avatar Mar 15 '22 10:03 vin-ni