Hot answers tagged

27 votes
Accepted

How can I retrieve channel data in json format?

Twig is great for outputting HTML/XML markup, but it’s not exactly the best option when you just want to output some data as JSON. The Element API plugin aims to make outputting entries (and other ...
user avatar
27 votes

How can I retrieve channel data in json format?

I just threw together a add-on that will provide a Twig filter called prune to allow you to more easily do what you're asking here. https://github.com/mattstauffer/craftcms-prune With prune you can ...
user avatar
12 votes

How can I retrieve channel data in json format?

You could also loop through all fields and output it depending on the field-type. Example: {% spaceless %} [ {% for entry in craft.entries.section('news').limit(1) %} { {# General ...
user avatar
  • 8,326
10 votes

Can I change which 'Format' options are available in my Rich Text field?

You certainly can using the Redactor Formatting config. Locate the config file for the Redactor setting you want to edit in craft/config/redactor and add this line: "formatting": ["h1", "h2", "h3"], ...
user avatar
  • 3,604
9 votes
Accepted

HTML Purifier Config File

Something like this in a .json file should work: { "HTML.SafeIframe": true, "URI.SafeIframeRegexp": "%^(https?:)?//(www\\.youtube(?:-nocookie)?\\.com/embed/)%", &...
user avatar
  • 66.5k
8 votes
Accepted

Console script tags garbling up non-html output (e.g. json) when in devMode

Just remove these two logging routes from your plugin's init() method in your PluginHandlePlugin.php file: craft()->log->removeRoute('WebLogRoute'); craft()->log->removeRoute('...
user avatar
  • 66.5k
8 votes

HTML/View Source button not working/showing in WYSIWYG Craft 2.5 / Redactor II 1.1.0

It turns out that the latest redactor seems to now have a plugin for viewing the source. You can enable this in your redactor JSON config file under plugins. Just add the "source" plugin like so: { ...
user avatar
  • 325
8 votes

Can I change which 'Format' options are available in my Rich Text field?

What I like to do is to override the default options of the Format dropdown by setting "formatting" to null and adding my own list of options using "formattingAdd". This allows you to come up with ...
user avatar
  • 35.9k
7 votes
Accepted

Passing multiple parameters to the Element API

You can fetch GET parameters using HttpRequestService::getQuery() or getRequiredQuery(): 'entries-by-category' => function() { $categoryIds = craft()->request->getRequiredQuery('...
user avatar
7 votes

HTML/View Source button not working/showing in WYSIWYG Craft 2.5 / Redactor II 1.1.0

You need to add "html" in the buttons array, and not "source" in the plugins array.
user avatar
7 votes
Accepted

Formatting postDate using the ElementAPI plugin

$entry->postDate will give you the postDate property of the EntryModel, which itself is a DateTime object. The JSON transformer will do its best to convert that object to JSON by returning its ...
user avatar
  • 3,400
6 votes
Accepted

What criteria can the Element API plugin accept?

The $entry variable that is passed to your endpoint’s transformer function will be an EntryModel object - the same type of object you use when interacting with entries in your templates. So everything ...
user avatar
6 votes

Creating entries from JSON feed - plugin?

Check out the FeedMe plugin. It's pretty similar to DataGrab and Importer. From the README: Feed Me Feed Me is a Craft plugin to make it easy to import entries and entry data from XML, RSS, ...
user avatar
  • 3,402
5 votes
Accepted

Craft CMS and JS/MVVM frameworks

I am just starting out on a project using both Craft and Angular. I ran into an early issue because both Angular and Twig use the {{ }} braces, but that was quickly fixed by changing Angular’s ...
user avatar
5 votes

How can I retrieve channel data in json format?

You could also do what Victor In has suggested, but deal with everything in arrays and hashes instead of building a big string. That way you only have to encode once at the end, and it's easier (for ...
user avatar
  • 3,061
5 votes
Accepted

Get Assets in JSON with ElementAPI

Something like the following should work: // elementapi.php return [ 'endpoints' => [ 'assets.json' => [ 'paginate' => false, 'elementType' => ...
user avatar
5 votes
Accepted

Removing P tag's in rich text area's

Seems like a simple striptags filter would do nicely in this case: <h1>{{ entry.titleField|striptags('<strong><em><br>')|raw }}</h1> If you're having issues with ...
user avatar
5 votes
Accepted

Passing JSON to Twig

The answer is to create a custom plugin that will read the json files from the folders in the public directory, parse the json, and pull them into the template. An alternative solution is to use ...
user avatar
4 votes
Accepted

BaseController::returnJson & devMode interference

I had the same problem. See Brad's response here to so this without messing with devMode: Console script tags garbling up non-html output (e.g. json) when in devMode It would be nice if this happened ...
user avatar
  • 3,061
4 votes

How to store json data in a custom fieldtype so that it's data can be referenced as attribute values?

You already mentioned the related answer in custom fieldtype with multiple values, and it actually is perfect for your usecase. It doesn't really matter how you transport the data over to Craft, ...
user avatar
  • 2,957
4 votes

Craft as a CMS for mobile app content

This is most likely possible by serving up restful resources that refer to Craft data with some sort of PHP API that you'll need to write. The APIs you write will just need to grab data using Craft's ...
user avatar
  • 312
4 votes
Accepted

Return json formatted entries and include a header for Access-Control-Allow-Origin

You can set the header in Twig with the {% header %} function. Like this: {% header "Content-Type: application/json; charset=utf-8" %} {% header "Access-Control-Allow-Origin: *" %} As to generating ...
user avatar
  • 7,248
4 votes
Accepted

Use of undefined constant JSON_HEX_TAG

You're likely missing the PHP JSON extension, which is a requirement to run Craft.
user avatar
  • 66.5k
4 votes
Accepted

Looping out a lot of entries.

There isn't really a good way to extract large chunks of Craft entries using the core features in Twig. Every entry you fetch is a EntryModel and those take up some memory due to relations and other ...
user avatar
  • 2,824
4 votes
Accepted

Creating environment variable from JSON file

You could write some PHP at the top of your general.php file that reads the JSON manifest in your public root directory, and add its keys/values to the config array. I wouldn't use environment ...
user avatar
3 votes
Accepted

How to set a response 200 from a plugin?

See here for various methods of setting the response code depending on what version of PHP and which PHP module you're running.
user avatar
  • 66.5k
3 votes

How to store json data in a custom fieldtype so that it's data can be referenced as attribute values?

Completely untested, but a possibility would be to define your content attribute like so: return array(AttributeType::Mixed, 'model' => 'MyPluginContentModel'); MyPluginContentModel would have ...
user avatar
  • 66.5k
3 votes
Accepted

Creating entries from JSON feed - plugin?

The Import plugin by Bob Olde Hampsink can import CSVs. JSON support is on the roadmap, but in the meantime, it’s easy enough to convert JSON to a CSV with a little bit of Javascript. Just paste this ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible