Headless Drupal 8
Click here to load reader
-
Upload
ruben-teijeiro -
Category
Technology
-
view
557 -
download
1
description
Transcript of Headless Drupal 8
![Page 1: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/1.jpg)
Headless Drupal 8#HeadlessDrupal
DrupalCamp Göteborg 2014
![Page 3: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/3.jpg)
![Page 4: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/4.jpg)
![Page 5: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/5.jpg)
![Page 6: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/6.jpg)
![Page 8: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/8.jpg)
Drupal 7 Front-end Sucks!
![Page 9: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/9.jpg)
DIVITIS
![Page 10: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/10.jpg)
![Page 11: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/11.jpg)
BUT
![Page 12: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/12.jpg)
Drupal 8 Front-end is MortenDK Certified
![Page 13: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/13.jpg)
I love Twig!! WTF!!WTF!!
![Page 14: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/14.jpg)
BUT
![Page 15: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/15.jpg)
"The front-end moves faster than Drupal, whether
Drupal likes it or not"
@eatings at DrupalCon Amsterdam
https://amsterdam2014.drupal.org/session/state-front-end
![Page 16: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/16.jpg)
"The front-end moves faster than Drupal, whether Drupal likes it or not"
![Page 17: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/17.jpg)
We have a solution
![Page 18: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/18.jpg)
DECOUPLING
![Page 19: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/19.jpg)
Decoupling Drupal Front-end makes easier to get
unexperienced front-end developers involved in Drupal
projects
![Page 20: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/20.jpg)
![Page 21: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/21.jpg)
Headless Drupal 8
![Page 22: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/22.jpg)
We don't need contrib anymoar!!
![Page 23: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/23.jpg)
Headless Drupal 8 REST modules are in Core
![Page 24: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/24.jpg)
Don't forget the permissions
![Page 25: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/25.jpg)
![Page 26: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/26.jpg)
A little bit of CRUD
![Page 27: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/27.jpg)
![Page 28: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/28.jpg)
CRUD
CreateReadUpdateDelete
![Page 29: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/29.jpg)
curl --include --request POST--user user:password--header 'Content-type: application/hal+json'http://localhost/drupal8/entity/node--data-binary '{"_links":{"type":{"href":"http://localhost/drupal8/rest/type/node/page"}}, "title":[{"value":"Node created using curl"}], "body": [{"value":"This is the body of the node created using curl"}]}'
Sample Create Request
![Page 30: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/30.jpg)
HTTP/1.1 201 CreatedDate: Fri, 24 Oct 2014 10:26:50 GMTServer: Apache/2.2.22 (Debian)X-Powered-By: PHP/5.4.33-1~dotdeb.1Location: http://localhost/drupal8/entity/node/2
Sample Create Response
![Page 31: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/31.jpg)
CRUD
CreateReadUpdateDelete
![Page 32: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/32.jpg)
Sample Read Request
curl-H "Accept:application/hal+json" --request GET http://localhost/drupal8/node/2
![Page 33: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/33.jpg)
Sample Read Response{"_links":{"self":{"href":"http:\/\/localhost\/drupal8\/node\/2"},"type":{"href":"http:\/\/localhost\/drupal8\/rest\/type\/node\/page"},"http:\/\/localhost\/drupal8\/rest\/relation\/node\/page\/uid":[{"href":"http:\/\/localhost\/drupal8\/user\/1","lang":"en"}],"http:\/\/localhost\/drupal8\/rest\/relation\/node\/page\/revision_uid":[{"href":"http:\/\/localhost\/drupal8\/user\/1"}]},"uuid":[{"value":"f89b04b9-b2b2-4230-8e5c-92fb3856213d"}],"type":[{"target_id":"page"}],"langcode":[{"value":"en"}],"title":[{"value":"Node updated using curl","lang":"en"}],"_embedded":{"http:\/\/localhost\/drupal8\/rest\/relation\/node\/page\/uid":[{"_links":{"self":{"href":"http:\/\/localhost\/drupal8\/user\/1"},"type":{"href":"http:\/\/localhost\/drupal8\/rest\/type\/user\/user"}},"uuid":[{"value":"b3e6e828-03fa-4a19-a706-6bba6f47edc4"}],"lang":"en"}],"http:\/\/localhost\/drupal8\/rest\/relation\/node\/page\/revision_uid":[{"_links":{"self":{"href":"http:\/\/localhost\/drupal8\/user\/1"},"type":{"href":"http:\/\/localhost\/drupal8\/rest\/type\/user\/user"}},"uuid":[{"value":"b3e6e828-03fa-4a19-a706-6bba6f47edc4"}]}]},"status":[{"value":"1","lang":"en"}],"created":[{"value":"1414146410","lang":"en"}],"changed":[{"value":"1414147433","lang":"en"}],"promote":[{"value":"0","lang":"en"}],"sticky":[{"value":"0","lang":"en"}],"revision_timestamp":[{"value":"1414146410"}],"body":[{"value":"This is the body of the node updated using curl","format":null,"summary":null,"lang":"en"}]}
![Page 34: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/34.jpg)
CRUD
CreateReadUpdateDelete
![Page 35: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/35.jpg)
Sample Update Request
curl --include --request PATCH--user user:password--header 'Content-type: application/hal+json' http://localhost/drupal8/node/2--data-binary '{"_links":{"type":{"href":"http://localhost/drupal8/rest/type/node/page"}}, "title":[{"value":"Node updated using curl"}], "body": [{"value":"This is the body of the node updated using curl"}]}'
![Page 36: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/36.jpg)
Sample Update Response
HTTP/1.1 204 No ContentDate: Fri, 24 Oct 2014 10:43:53 GMTServer: Apache/2.2.22 (Debian)X-Powered-By: PHP/5.4.33-1~dotdeb.1
![Page 37: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/37.jpg)
CRUD
CreateReadUpdateDelete
![Page 38: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/38.jpg)
Sample Delete Request
curl --include --request DELETE--user user:password--header 'Content-type: application/hal+json' http://localhost/drupal8/node/2
![Page 39: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/39.jpg)
Sample Delete Response
HTTP/1.1 204 No ContentDate: Fri, 24 Oct 2014 10:51:04 GMTServer: Apache/2.2.22 (Debian)X-Powered-By: PHP/5.4.33-1~dotdeb.1
![Page 40: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/40.jpg)
![Page 41: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/41.jpg)
Views are in Core too!!
![Page 42: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/42.jpg)
![Page 44: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/44.jpg)
![Page 45: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/45.jpg)
REST export
![Page 46: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/46.jpg)
REST export settings
![Page 47: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/47.jpg)
curl-H "Accept:application/hal+json" --request GET http://localhost/drupal8/node/rest
Sample Request
![Page 48: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/48.jpg)
Sample Response[{"_links":{"self":{"href":"http:\/\/localhost\/drupal8\/node\/1"},"type":{"href":"http:\/\/localhost\/drupal8\/rest\/type\/node\/article"},"http:\/\/localhost\/drupal8\/rest\/relation\/node\/article\/uid":[{"href":"http:\/\/localhost\/drupal8\/user\/1","lang":"en"}],"http:\/\/localhost\/drupal8\/rest\/relation\/node\/article\/revision_uid":[{"href":"http:\/\/localhost\/drupal8\/user\/1"}]},"uuid":[{"value":"5fad49bf-5c70-475d-9333-51c25caf62d5"}],"type":[{"target_id":"article"}],"langcode":[{"value":"en"}],"title":[{"value":"You don't get sick, I do","lang":"en"}],"_embedded":{"http:\/\/localhost\/drupal8\/rest\/relation\/node\/article\/uid":[{"_links":{"self":{"href":"http:\/\/localhost\/drupal8\/user\/1"},"type":{"href":"http:\/\/localhost\/drupal8\/rest\/type\/user\/user"}},"uuid":[{"value":"b3e6e828-03fa-4a19-a706-6bba6f47edc4"}],"lang":"en"}],"http:\/\/localhost\/drupal8\/rest\/relation\/node\/article\/revision_uid":[{"_links":{"self":{"href":"http:\/\/localhost\/drupal8\/user\/1"},"type":{"href":"http:\/\/localhost\/drupal8\/rest\/type\/user\/user"}},"uuid":[{"value":"b3e6e828-03fa-4a19-a706-6bba6f47edc4"}]}]},"status":[{"value":"1","lang":"en"}],"created":[{"value":"1413657929","lang":"en"}],"changed":[{"value":"1413657957","lang":"en"}],"promote":[{"value":"1","lang":"en"}],"sticky":[{"value":"0","lang":"en"}],"revision_timestamp":[{"value":"1413657929"}],"revision_log":[{"value":"","lang":"en"}],"body":[{"value":"<p>Your bones don't break, mine do. That's clear. Your cells react to bacteria and viruses differently than mine. You don't get sick, I do. That's also clear. But for some reason, you and I react the exact same way to water. We swallow it too fast, we choke. We get some in our lungs, we drown. However unreal it may seem, we are connected, you and I. We're on the same curve, just on opposite ends.<\/p>\r\n","format":"basic_html","summary":"","lang":"en"}],"comment":[{"status":"2","cid":"0","last_comment_timestamp":"1413657957","last_comment_name":null,"last_comment_uid":"1","comment_count":"0","lang":"en"}]}]
![Page 49: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/49.jpg)
![Page 50: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/50.jpg)
vs
HAL JSON
![Page 51: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/51.jpg)
Use HAL if you care about API definition
{ "_links": {
"self": { "href": "/your-api-links" }
},
"_embedded": {
"items": [
Array of serialized items
]
}
}
![Page 52: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/52.jpg)
Use JSON if you only care about the content
[
{ "title": “This is your content title”,
"body": “This is your content body.”
}
]
![Page 53: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/53.jpg)
How to enable JSON requests
![Page 54: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/54.jpg)
Like a Ninja
![Page 55: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/55.jpg)
Like a Ninja
![Page 56: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/56.jpg)
core/modules/rest/config/install/rest.settings.yml
resources: entity:node: GET: supported_formats: - hal_json supported_auth: - basic_auth POST: supported_formats: - hal_json supported_auth: - basic_auth PATCH: supported_formats: - hal_json supported_auth: - basic_auth DELETE: supported_formats: - hal_json supported_auth: - basic_auth
![Page 57: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/57.jpg)
Configuration Management – Single Import
![Page 58: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/58.jpg)
Like a Boss
![Page 59: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/59.jpg)
https://www.drupal.org/project/restui
![Page 60: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/60.jpg)
Sample JSON Request
curl -H "Accept: application/json" --request GET http://localhost/drupal8/node/rest
![Page 61: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/61.jpg)
Sample JSON Response
[{"nid":[{"value":"1"}],"uuid":[{"value":"5fad49bf-5c70-475d-9333-51c25caf62d5"}],"vid":[{"value":"1"}],"type":[{"target_id":"article"}],"langcode":[{"value":"en"}],"title":[{"value":"You don't get sick, I do"}],"uid":[{"target_id":"1"}],"status":[{"value":"1"}],"created":[{"value":"1413657929"}],"changed":[{"value":"1413657957"}],"promote":[{"value":"1"}],"sticky":[{"value":"0"}],"revision_timestamp":[{"value":"1413657929"}],"revision_uid":[{"target_id":"1"}],"revision_log":[{"value":""}],"path":[{"alias":null,"pid":null}],"body":[{"value":"<p>Your bones don't break, mine do. That's clear. Your cells react to bacteria and viruses differently than mine. You don't get sick, I do. That's also clear. But for some reason, you and I react the exact same way to water. We swallow it too fast, we choke. We get some in our lungs, we drown. However unreal it may seem, we are connected, you and I. We're on the same curve, just on opposite ends.<\/p>\r\n","format":"basic_html","summary":""}],"comment":[{"status":"2","cid":"0","last_comment_timestamp":"1413657957","last_comment_name":null,"last_comment_uid":"1","comment_count":"0"}],"field_image":[{"target_id":null,"display":null,"description":null,"alt":null,"title":null,"width":null,"height":null}],"field_tags":[{"target_id":null}]}]
![Page 62: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/62.jpg)
BEWARE!!hal_json is the only format supported
for POST and PATCH methods
https://www.drupal.org/node/1964034
It needs work:
![Page 63: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/63.jpg)
![Page 64: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/64.jpg)
Getting a cleaner JSON response
![Page 65: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/65.jpg)
Use Fields in View
![Page 66: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/66.jpg)
Fields Raw Format
![Page 67: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/67.jpg)
Clean JSON Response
[ { "title":"You don't get sick, I do", "body":"<p>Your bones don't break, mine do. That's clear. Your cells react to bacteria and viruses differently than mine. You don't get sick, I do. That's also clear. But for some reason, you and I react the exact same way to water. We swallow it too fast, we choke. We get some in our lungs, we drown. However unreal it may seem, we are connected, you and I. We're on the same curve, just on opposite ends.<\/p>" }]
![Page 68: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/68.jpg)
Happy now?
![Page 69: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/69.jpg)
DEMO
![Page 70: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/70.jpg)
Want to contribute?
How to POST a comment and other relational entities
Add special handling for collections in REST
Support ConfigEntity via REST
[meta] REST et al
File needs CRUD permissions to make REST work on entity/file/{id}
Serialize file content (base64) to support REST GET/POST/PATCH on file entity
![Page 71: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/71.jpg)
Join the Code Sprints!!
![Page 72: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/72.jpg)
Questions??
![Page 73: Headless Drupal 8](https://reader037.fdocuments.us/reader037/viewer/2022100223/547e85bdb379594e2b8b54a3/html5/thumbnails/73.jpg)
Thanks!