Entity JS allows for Javascript layer access to some commonly used entity-related functions such as EntityFieldQuery.
This is achieved through Drupal menu callbacks and jQuery $.ajax calls.
Note: jQuery's jqXHR Object uses the .done()
method that requires later versions of jQuery (1.5+) than what currently ships with Drupal 7. In order to use these examples, we recommend installing jQuery Update. For more on .done()
, review the .promise() method jQuery API docs.
entity_create
Create an entity by providing a type and an array of arguments. Shown printing response below.
values = {
"name": "username",
"mail": "username@example.com",
}
entity_create('user', values).done(function(data) { console.log(data); });
Uses a $.post callback URL: /entity_create/[entity_type]
entity_render_view
Return a rendered entity using a view mode. Shown printing HTML to console below.
entity_render_view('node', 1, 'default').done(function(data) { console.log(data); });
Uses a $.get callback URL: /entity_js_drupal_render_entity_view/[entity_type]/[entity_id]/[view_mode]
entity_load_json
Return an entity as JSON. Shown printing the object to browser console in snippet below.
entity_load_json('user', 1).done(function(data) { console.log(data); });
Uses a $.get callback URL: /entity_js_load_single_json/[entity_type]/[entity_id]
entity_field_query_json
Execute an EntityFieldQuery and return the results as JSON. Shown printed to console below.
conditions = {
"entityCondition": [
[ "entity_type", "node" ],
[ "bundle", "article" ]
],
"propertyCondition": [
[ "status", 1 ]
],
"fieldOrderBy": [
[ "field_date", "value", "ASC"]
],
"fieldCondition": [
[ "field_weight", "value", [ 0, 1 ], "BETWEEN" ]
]
}
entity_field_query_json(conditions).done(function(data) { console.log(data); });
Above uses a $.post callback URL: /entity_js_efq_json
Below is an EntityFieldQuery that returns a similar result.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'article')
->propertyCondition('status', 1)
->fieldOrderBy('field_date', 'value', 'ASC')
->fieldCondition('field_weight', 'value', array('0', '1'), 'BETWEEN');
return $query->execute();
One can also return a list of fully loaded objects instead of the default EFQ output:
conditions = {
"entityCondition": [
[ "entity_type", "node" ],
[ "bundle", "article" ]
],
"propertyCondition": [
[ "status", 1 ]
]
}
entity_load = true;
entity_field_query_json(conditions, entity_load).done(function(data) { console.log(data); });
This requires an additional boolean (true/false) argument, shown as 'entity_load' above.
entity_update
Update an entity via type, unique id and an array of arguments. Shown printing response below.
values = {
"name": "admin",
"mail": "admin@example.com",
}
entity_update('user',1, values).done(function(data) { console.log(data); });
Uses a $.post callback URL: /entity_js_update/[entity_type]/[entity_id]
entity_delete
Delete an existing entity. Shown printing response in snippet below.
entity_delete('node', 1).done(function(data) { console.log(data); });
Uses a $.get callback URL: /entity_js_delete/[entity_type]/[entity_id]