Skip to main content
edited title
Link
Brad Bell
  • 67.6k
  • 6
  • 75
  • 145

What criteria can the elementElement API plugin accept?

Set the code language and added the Element API plugin tag.
Source Link
Brandon Kelly
  • 34.4k
  • 2
  • 74
  • 139
<?php
namespace Craft;

return [
  'endpoints' => [
    'locations.json' => [
      'elementType' => 'Entry',
      'criteria' => [
        'section' => 'locations',
        'type' => 'location'
      ],
      'transformer' => function(EntryModel $entry) {
        return [
          'title' => $entry->title,
          'url' => $entry->url,
          'latitude' => $entry->latitude,
          'longitude' => $entry->longitude,
          'jsonUrl' => UrlHelper::getUrl("locations/{$entry->id}.json")
        ];
      },
    ],
    'locations/<slug:{slug}>.json' => function($slug) {
      return [
        'elementType' => 'Entry',
        'criteria' => [
          'section' => 'locations',
          'type' => 'state',
          'slug' => $slug
        ],
        'transformer' => function(EntryModel $entry) {
          return [
            'title' => $entry->title,
            'url' => $entry->url,
            'latitude' => $entry->latitude,
            'longitude' => $entry->longitude,
            'jsonUrl' => UrlHelper::getUrl("locations/{$entry->id}.json")
          ];
        },
      ];
    },
  ]
];
<?php
namespace Craft;

return [
  'endpoints' => [
    'locations.json' => [
      'elementType' => 'Entry',
      'criteria' => [
        'section' => 'locations',
        'type' => 'location'
      ],
      'transformer' => function(EntryModel $entry) {
        return [
          'title' => $entry->title,
          'url' => $entry->url,
          'latitude' => $entry->latitude,
          'longitude' => $entry->longitude,
          'jsonUrl' => UrlHelper::getUrl("locations/{$entry->id}.json")
        ];
      },
    ],
    'locations/<slug:{slug}>.json' => function($slug) {
      return [
        'elementType' => 'Entry',
        'criteria' => [
          'section' => 'locations',
          'type' => 'state',
          'slug' => $slug
        ],
        'transformer' => function(EntryModel $entry) {
          return [
            'title' => $entry->title,
            'url' => $entry->url,
            'latitude' => $entry->latitude,
            'longitude' => $entry->longitude,
            'jsonUrl' => UrlHelper::getUrl("locations/{$entry->id}.json")
          ];
        },
      ];
    },
  ]
];
<?php
namespace Craft;

return [
  'endpoints' => [
    'locations.json' => [
      'elementType' => 'Entry',
      'criteria' => [
        'section' => 'locations',
        'type' => 'location'
      ],
      'transformer' => function(EntryModel $entry) {
        return [
          'title' => $entry->title,
          'url' => $entry->url,
          'latitude' => $entry->latitude,
          'longitude' => $entry->longitude,
          'jsonUrl' => UrlHelper::getUrl("locations/{$entry->id}.json")
        ];
      },
    ],
    'locations/<slug:{slug}>.json' => function($slug) {
      return [
        'elementType' => 'Entry',
        'criteria' => [
          'section' => 'locations',
          'type' => 'state',
          'slug' => $slug
        ],
        'transformer' => function(EntryModel $entry) {
          return [
            'title' => $entry->title,
            'url' => $entry->url,
            'latitude' => $entry->latitude,
            'longitude' => $entry->longitude,
            'jsonUrl' => UrlHelper::getUrl("locations/{$entry->id}.json")
          ];
        },
      ];
    },
  ]
];
<?php
namespace Craft;

return [
  'endpoints' => [
    'locations.json' => [
      'elementType' => 'Entry',
      'criteria' => [
        'section' => 'locations',
        'type' => 'location'
      ],
      'transformer' => function(EntryModel $entry) {
        return [
          'title' => $entry->title,
          'url' => $entry->url,
          'latitude' => $entry->latitude,
          'longitude' => $entry->longitude,
          'jsonUrl' => UrlHelper::getUrl("locations/{$entry->id}.json")
        ];
      },
    ],
    'locations/<slug:{slug}>.json' => function($slug) {
      return [
        'elementType' => 'Entry',
        'criteria' => [
          'section' => 'locations',
          'type' => 'state',
          'slug' => $slug
        ],
        'transformer' => function(EntryModel $entry) {
          return [
            'title' => $entry->title,
            'url' => $entry->url,
            'latitude' => $entry->latitude,
            'longitude' => $entry->longitude,
            'jsonUrl' => UrlHelper::getUrl("locations/{$entry->id}.json")
          ];
        },
      ];
    },
  ]
];
Source Link

What criteria can the element API accept?

I have a structure called "locations" that contains two entry types:

  • State
  • Location

I'd like to access specific states at the URL locations/ny.json, for example, and get a JSON object with all the location entries nested under that state.

The entries are already properly nested for HTML output, but I'm trying to get the JSON to match.

This is my elementsapi.php file right now:

<?php
namespace Craft;

return [
  'endpoints' => [
    'locations.json' => [
      'elementType' => 'Entry',
      'criteria' => [
        'section' => 'locations',
        'type' => 'location'
      ],
      'transformer' => function(EntryModel $entry) {
        return [
          'title' => $entry->title,
          'url' => $entry->url,
          'latitude' => $entry->latitude,
          'longitude' => $entry->longitude,
          'jsonUrl' => UrlHelper::getUrl("locations/{$entry->id}.json")
        ];
      },
    ],
    'locations/<slug:{slug}>.json' => function($slug) {
      return [
        'elementType' => 'Entry',
        'criteria' => [
          'section' => 'locations',
          'type' => 'state',
          'slug' => $slug
        ],
        'transformer' => function(EntryModel $entry) {
          return [
            'title' => $entry->title,
            'url' => $entry->url,
            'latitude' => $entry->latitude,
            'longitude' => $entry->longitude,
            'jsonUrl' => UrlHelper::getUrl("locations/{$entry->id}.json")
          ];
        },
      ];
    },
  ]
];

The second endpoint is currently returning a single state entry, but I would like it to return all locations that are a descendant of that state.

Is this possible using a criteria param such as descendantOf? What criteria params are available?