D r u p a l i z e Y o u r D a t a : U s e E n t i t i e s ! Drupal Dev Days Barcelona 2012 by Wolfgang Ziegler // fago
Wo l f g a n g Z i e g l e r / / f a g o • from Vienna, Austria • studied at the TU Vienna • Drupal since 2005. wolfgangziegler.net twitter.com/the_real_fago gplus.to/fago
O u t l i n e • Introduction • Interact with entities • Providing a new entity type • Expose non-DB entities • Outlook
D r u p a l 7 : F i e l d s e v e r y wh e r e !
B u n d l e s Entity type ↔ Node Bundle ↔ Node type Entity type ↔ Taxonomy term Bundle ↔ Vocabulary Entity type ↔ User Bundle ↔ {}
Wh i c h e n t i t i e s a r e t h e r e ? • Core • Modules: • Drupal commerce • Organic groups • Profile2 • Heartbeat and Message • File entity
N o d e mo d u l e s E n t i t y mo d u l e s
E n t i t y A P I - Wh a t f o r ? Unified way to access data.
User Search Node Vote Comment Entity Groups Entity Profile reference Product Rules
I n t e r a c t i n g wi t h e n t i t i e s
E n t i t y A P I mo d u l e Assists you with • interacting with entities • and providing new entity types
E n t i t y A P I F u n c t i o n s Entity API module entity_get_property_info() entity_id() Drupal Core entity_view() entity_get_info() entity_save() entity_load() entity_create() entity_delete() entity_access() entity_metadata_wrapper()
Me t a d a t a Wr a p p e r $wrapper = entity_metadata_wrapper('node', $nid); $mail = $wrapper->author->mail->value(); $wrapper->author->mail->set('fago@example.com'); $text = $wrapper->field_text->value(); $wrapper->language('de')->field_text->value(); $terms = $wrapper->field_tags->value(); $wrapper->field_tags[] = $term; $options = $wrapper->field_tags->optionsList(); $label = $wrapper->field_tags[0]->label(); $access = $wrapper->field_tags->access('edit');
E n t i t y p r o p e r t y i n f o $properties['mail'] = array( 'label' => t("Email"), 'type' => 'text', 'description' => t("The email address of ..."), 'setter callback' => 'entity_property_verbatim_set', 'validation callback' => 'valid_email_address', 'required' => TRUE, 'access callback' => 'user_properties_access', 'schema field' => 'mail', );
P r o p e r t y i n f o ? • Unified access to entity data • Validation • Access information
H o w mo d u l e s u s e i t • Drupal Commerce, VBO, OG • Rules, Search API • RestWS, WSClient • Microdata, RDFx • Entity tokens • Entity Views (Search API, EFQ views)
P r o v i d i n g a n e n t i t y t y p e • Implement hook_entity_info() • Specify your ' controller class' • Implement hook_schema()
E n t i t y A P I mo d u l e $profile = entity_create('profile2', array( 'type' => 'main', 'user'=> $account, )); $profile->save(); $profile->delete(); entity_delete_multiple('profile2', array(1, 2, 3));
I n t e g r a t i n g y o u r e n t i t y t y p e Tokens Views Rules XY Property Field API Info CRUD Info Schema controller
E x p o r t a b l e e n t i t i e s similar to CTools exportables, but... • unified CRUD interface • regular CRUD hooks • synced to the DB • Admin UI, Features, i18n, Views, Tokens,...
I n t e g r a t i n g y o u r e x p o r t a b l e e n t i t y Admin Features Views i18n Tokens UI Property Info CRUD Info Schema controller
E x a mp l e : P r o f i l e 2 Profile2: • Entity type Profile2 types: • Entity type • Bundle of Profile2 • Exportable
P r o f i l e 2 t o r n a p a r t Y o u r j o b • CRUD, Field API • Permissions, Access • Profile form, display • Admin UI, I18n • Views, CTools, Rules, Tokens, Features, Search API
h o me wo r k d o n e , l e t ' s e n j o y i t .
• NoSQL, Doctrine, PHPCR • Remote entities • Data integration
N o n - D B e n t i t i e s + F i e l d s ?
N o n - D B e n t i t i e s Views Info Controller Rules Schema Tokens XY CRUD Property Info controller Info
Wh a t d o e s i t b u y u s ? • Classed CRUD API • CRUD Hooks • Tokens • Entity reference • Rules, Rules Links • Search API, Views integration, RestWS • …
D r u p a l i z e y o u r d a t a !
E x a mp l e . . .
G o o g l e P i c a s a e n t i t i e s http://drupal.org/sandbox/fago/1493180
R o o m f o r i mp r o v e me n t s • Generate display • Generate form • Ready-only mode
D r u p a l 8
C l a s s e d o b j e c t s . . . ! $comment = entity_create('comment', array( 'nid' => $node->nid, )); $comment->save(); echo $comment->id(); $comment->delete(); entity_delete_multiple('comment', array(1, 2, 3));
C o mme n t s i n D r u p a l 8 class CommentStorageController extends EntityDatabaseStorageController { … } class Comment extends Entity { … } class Entity implements EntityInterface { … }
E n t i t y P r o p e r t y A P I • Similar to the entity metadata wrappers, but improved and built-in! • Unified API for properties and fields • Property definitions / metadata • D8MI, WSCCI, Content staging, ... • Improved DX!
WS C C I We b S e r v i c e s F o r ma t R e p o r t http://groups.drupal.org/node/237443
Q u e s t i o n s ?
T h a n k y o u !
Recommend
More recommend