Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Entity API DrupalCAMP 2013

Lecture about entity API
by

Teun van Veggel

on 25 October 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Entity API DrupalCAMP 2013

Entity API
How to use
to enhance your
custom application

Why
this presentation?
What
is an entity?
When
use the entity API?
How
to build your own entity
Node
User
Taxonomy term
Anything
Every entity type has its own table in the database.
Entity type
Bundle
Other properties
Unique identifier
Node
Content type/
node type/
type
Vid
Language
Title
Uid
Status
Created
Nid
Changed
Comment
Promote
Sticky
tnid
translate
Taxonomy
term
Vocabulary (vid)
Name
Description
Format
Weight
tid
properties
User
User
User name
Password
Mail
Theme
signature
signature_format
created
uid
Every entity belongs to an
entity type
and a
bundle
.
Fields are defined per bundle.
Course
Unit
Exercise
Question
Elearning module
Fieldable entities
Course access
Course result
Unit result
Exercise result
Question result
Non Fieldable entities
Can you do it with node?
Do you need a page? (node/%)?
Do you need comments?
Do you need 'promoted' or 'sticky'?
Do you want it 'clean'?
Vocabulary
Vocabulary
name
machine_name
description
hierarchy
module
weight
vid
1. Create the table
2. Declare the entity type
3. Add the admin
Why Entity?
Why Entity API?
Do you want to control access to your entity?
Entity API (D7) extends de core Entity and adds:
EntityAPIController
CRUD (create/read/update/delete)
entity_metadata_wrapper
rules controller class
views controller class
features controller class
i18n controller class
automatic fields administration
4. Complete the property metadata
Elearning module
The e-learning module creates a framework for online teaching and learning.
Course
Course Access
course/%course/access
Unit
entity
reference
entity
reference
Exercise
Question
entity
reference
Unit result
Testing
Practice
Open Question
Multiple choice
Exercise result
User
Question result
+ Feedback
Put in order
Question
Teun van Veggel
@nuezweb
hook_entity_property_info_alter
function exercise_entity_property_info_alter(&$info) {

$properties = array(
'uid' => array(
'type' => 'user',
'label' => t('Author'),
'description' => t('The author of this exercise'),
),
);

$info['exercise']['properties'] = array_replace_recursive($info['exercise']['properties'], $properties);

}
dpm() in myentity_entity_property_info_alter

label
description
type
text
token
integer
decimal
date
duration
boolean
uri
entity
struct
list
getter callback
setter callback
more ...

Metadata of a property
array_replace_recursive
4. Use your entity
array_replace_recursive
Example
http://prezi.com/h3ncpgzx7gur
entity_metadata_wrapper
$referenced_id = $my_entity->field_entity_reference['und'][0]['target_id'];

$referenced_id = $my_entity->field_entity_reference[LANGUAGE_NONE][0]['target_id'];

$referenced_object = entity_load('referenced_entity_type', $referenced_id);

Wolfgang Ziegler // Fago
http://drupalcamp2013.nuezweb.com

https://drupal.org/sandbox/nuez/2054439
course/%
course/%/unit/%
course/%/unit/%/exercise/%
course/%/unit/%/exercise/%/question/%
hook_schema
in yourmodule.install
return $schema['my_entity_type'] = array(...);
Use blob type for flexible properties.
Foreign keys are for documentation purposes only
hook_entity_info
In D7 the hook_entity_info is extended by the EntityAPI
defines
entity class (defaults to Entity)
entity controller class (defaults to EntityAPIController)
base table ('my_entity_type')
and more...
EntityAPIController
Defines CRUD methods
Create
Read
Update
Delete
And
load
view
export
import
etc..
MyCustomController
MyCustomController extends EntityAPIController{

public function load($ids = array(), $conditions = array()) {
$entities = parent::load($ids, $conditions);

// do stuff

return $entities;

}
}
1. hook_menu
Page for adding entity
Page for editing entity
Page for administering the entities
2. The form
property = form element
field_attach_form
hook_field_extra_fields
function my_entity_form($form, &$form_state, $entity){

$form['property_A'] = array(...);
$form['property_B'] = array(...);
field_attach_form(..);
return $form;

}
entity_create('my_entity_type', array('type'=> $type);
mymodule.views_defaults.inc
3. Submitting the form
entity_form_submit_build_entity

function my_entity_form_submit($form, &$form_state) {
$entity = $form_state['entity'];
entity_form_submit_build_entity('my_entity_type', $entity, $form, $form_state);
entity_save('my_entity_type- , $entity);
}
$wrapper = entity_metadata_wrapper ('my_entity_type', $my_entity);

$referenced_object = $wrapper->field_entity_reference->value();

$mail_of_author = $wrapper->uid->mail->value();

$wrapper->uid->mail->set('nuezweb@exampe.com');
setter callback required
entity_create
entity_delete
entity_save
entity_access
Full transcript