Hot answers tagged

15

You should use the serializer service if you want to serialize a Drupal data type (Typed Data API) so that normalization is run properly. This requires enabling the serialization module as pointed out by Alari Truuts, which defines the serializer service. The serialize method is a little confusing as to what the $context parameter means. For Drupal Typed ...


12

To return $data in json format using Drupal-7's page callback output, you should return through : drupal_json_output($data); in your page callback function. For more ref : http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_json_output/7 Note : in your javascript in car.module, var pageUrl seems undefined. Make sure it is defined.


10

In case you want to do it more the D8 way here is a rough outline what you could do, which allows you to be flexible without the requirement to do everything in your own. A proper rest integration requires you to work with all the different HTTP status codes, header fields etc. Rest takes partly care of that The rest module provides a generic abstraction ...


9

Use the Json::decode() method: use Drupal\Component\Serialization\Json; ... $decoded = Json::decode($data);


9

This is all you need to set up caching for a response: $response = CacheableJsonResponse::create($output); return $response; If you use a response that implements the CacheableResponseInterface it will be cached indefinitely. Cache tags When you add a list of taxonomy terms you have to add these tags: $list_tags = $this->entityTypeManager()->...


8

Perhaps Services Views module could be of interest here: Views support for the Services module version 3.x and later. It has currently two features: - Create view based resource creating Services display in a view - Execute any view of the system via views resource call After enabling "views" resource in your services endpoint you can do ...


7

Converting an IP to a latitude and longitude isn't a good idea since the user could be using a proxy. A better way would be to use HTML5 geolocation, which tries to get a location based on a couple of parameters: IP, wireless connection, cell tower location, GPS ... Try: navigator.geolocation.getCurrentPosition(show_map); function show_map(position) { ...


7

Up until Make serialization_class optional (#2419825) issue has been fixed for 8.2.x branch you couldn't get away without creating a custom serialization class which merely passes the provided data to satisfy the requirement for mandatory serialization. If you need to make it work on Drupal <= 8.1, try the following: Create a custom normalizer. Expose ...


6

As I still ran into a lot of 403 Forbidden errors I'll summarize my overall solution here (Drupal 8.0.1): 1.) Setup & Configuration Enable all core web services modules (HAL, HTTP Basic Authentication, RESTful Web Services, Serialization Enable relevant permissions of RESTful Web Services and for creating the relevant Nodes. 2.) Get a CSRF Token: ...


5

The following JSON appears to be the minimum you need to post a comment: { "nid":"1", "subject":"Comment Subject Text", "comment_body":{ "und":[{ "value":"Comment body text" }] } } Since there is no user, commenting on this "example.com/node/1" needs to be turned on and permissions for anonymous users to post comments need to be set.


5

The advantage of having in Drupal.settings is the values can be globally accessed across the JavaScript functions. In your case the function "my_js_function" only can access it because of its scope. It is based on the need and if you feel it is unnecessary to have it in settings then you can go with your method. But best practice is to use Drupal settings. ...


5

Go to the configuration of your Views display which produces the JSON output. Click on the Settings link of the JSON data document format. Make sure that Views API mode is not enabled. The description of this setting explains it all: Not using View API mode means the JSON gets output directly and the server ceases normal page processing. Using it ...


5

I think you have a problem with the "context", and title query. I've tried this configuration and worked for me. The context is the first query and in this case just selects all the nodes, the second one takes the value of the key "last" ant put it on the node title. Then you can play with other querys, to create the nodes, but at least this one works.


5

To change the structure of the Serializer output you should create a custom Serializer here is the steps of your use case: Create CustomSerializer extends the Drupal\rest\Plugin\views\style\Serializer and update the render method to your needs. Create MY_MODULE/Plugin/views/style/CustomSerializer.php namespace Drupal\MY_MODULE\Plugin\views\style; use ...


4

There is now a Drupal 7 version of Views Datasource. Here is a quote about it (from the module's project page): ... is a set of plugins for Drupal Views for rendering content in a number of shareable, reusable formats based on XML, JSON and XHTML. These formats allow content in a Drupal site to be easily used as data sources for Semantic Web clients and ...


4

In Drupal 7 you can use drupal_json_decode() which is only a one line wrapper to json_decode() anyway. If you structure your table smartly you can probably just write the output of json_decode() straight to the database (if the columns in the table agree with the data); otherwise you will have to manipulate the data a bit.


4

You could create a small module that will be called through ajax: function MYMODULE_node_menu() { return array(//$items 'MYMODULE/%' => array( 'page callback' => 'MYMODULE_display_view', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, ) );...


4

Background Services uses drupal_execute() with the node form to save updates to nodes, which won't entirely merge updates in with existing data. If the update doesn't set a field at all, then it won't overwrite what's there, so in a sense it does merge data in that case. For instance, in your example, you don't set "title" in your update, and therefore it ...


4

As @lenni rightly says it's a serialized PHP variable. But data is a property of the user entity type so you don't actually need to unserialize it yourself. When the user object is loaded from the database it's automatically populated with the unserialized version of the data column. So all you need to do to access your 'user tokens' field (if such a field ...


4

I find my 1st answer informative regarding auxillery information regarding views and views_datasource. So here's 1 way I think you get the url you want. You said: My guess is that Views Datasource is processing the data after I've seen it in my hook implementations. It is, look at what views_json_view_pre_render() is doing: function ...


4

I dont know why do you need to convert the data to JSON format. My idea is that you just return an array. The returned data of your custom service will be based on how we call it. function gymcam_sendemail($data) { $data = array('status' => 'error', 'msg' => 'Mail not sent'); return $data; } If you want to use JSON for returned data, just call /...


4

The available hooks for Services are documented in the api docs for services You probably want hook_services_request_postprocess_alter($controller, $args, $result); which allows you to change the array of $result any way you want.


4

To setup a service which returns a json formatted data, you should set the delivery callback to drupal_json_output. This will only output the service data and not all the HTML defined in your theme. For example function YOURMODULE_menu() { $items = array(); $items['service/ajax'] = array( 'title' => t('Ajax service'), 'page ...


4

Hope this will help you, In Drupal 7 you can use the below JSON format to add custom fields(field_user_image) { "name": "test", "pass": "password", "mail": "test@gmail.com", "field_user_image": { "und": [{ "fid": "2" }] } } I have tried in my local and it is working fine. Created new field UUID Calling the ...


4

Why not just do it custom? Here is an example to get you started. Set your cron on the server to run every hour. This requires entity api /** implements hook_cron **/ function poloniex_cron(){ poloniex_get_feed(); } function poloniex_get_feed(){ //Get the feed and turn it into json $feed = file_get_contents("https://poloniex.com/public?command=...


4

In the controller's function how can I get the data posted with ajax and how to convert it to associative array? use Drupal\Component\Serialization\Json; //... class MyController { public function myPostAction(Request $request) { $params = Json::decode($request->getContent()); //... } }


4

TL;DR: You should check_plain it. Because the json-enoded string itself can contain insecure code. This is our example. $data[1] and $data[2] should not be executed. $data = array( 'Hukana Matata', '<script>alert("Oh no!")</script>', '<a href="#" onclick="javascript:alert(\"Oh no!\')">CLick here</a>', ); You encode this ...


4

I ended up creating a module that does the basics for getting a menu via rest. I can just put the menus machine name in the URL and it will give me the title, url and weight in JSON format. I uploaded it to github so you can test it out. It is by no means ready for productions web sites. I was just messing around with angular and REST in D8. Using angular, ...


4

You could use the Forena module (disclosure: I'm a co-maintainer of it) as your 1-stop-solution (using only 1 contributed module) to: Build your custom query to fetch the data, whereas these data can be retrieved directly from your Drupal database. Obviously, in this case it's be an SQL to retrieve your CONTENT_ID, CONTENT_NAME, LARGE_THUMBNAIL, ZIP_FILE ......


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