46

Drupalize your data use entities

  • Upload
    -

  • View
    327

  • Download
    1

Embed Size (px)

DESCRIPTION

Drupalize your data use entities Druapl 7

Citation preview

Page 1: Drupalize your data use entities
Page 2: Drupalize your data use entities

Coding and development

Drupalize Your Data: Use Entities!

Presented by Wolfgang Ziegler // fago

Page 3: Drupalize your data use entities

Wolfgang Ziegler // fago

• from Vienna, Austria

• studied at the TU Vienna

• Drupal since 2005.

wolfgangziegler.nettwitter.com/the_real_fagogplus.to/fago

Page 4: Drupalize your data use entities

Outline

• Introduction

• Interact with entities

• Providing a new entity type

• Expose non-DB entities

• Outlook

Page 5: Drupalize your data use entities

Drupal 7: Fields everywhere!

Page 6: Drupalize your data use entities

Bundles

Entity type ↔ NodeBundle ↔ Node type

Entity type ↔ Taxonomy termBundle ↔ Vocabulary

Entity type ↔ UserBundle ↔ {}

Page 7: Drupalize your data use entities

Which entities are there?

• Core

• Modules:

• Drupal commerce

• Organic groups

• Profile2

• Heartbeat and Message

• File entity

Page 8: Drupalize your data use entities

Node modules

Entity modules

Page 9: Drupalize your data use entities

Entity API - What for?

Unified way to access data.

Page 10: Drupalize your data use entities

User

Node

Comment

Profile

Product

Search

Vote

Groups

Entityreference

Rules

Entity

Page 11: Drupalize your data use entities

Interacting with entities

Page 12: Drupalize your data use entities

Entity API module

Assists you with

• interacting with entities

• and providing new entity types

Page 13: Drupalize your data use entities

Entity API Functions

Drupal Core

Entity API module

entity_save()entity_load()

entity_get_info()

entity_metadata_wrapper()

entity_view()

entity_access()

entity_create()

entity_id()

entity_get_property_info()

entity_delete()

Page 14: Drupalize your data use entities

Metadata Wrapper

$wrapper = entity_metadata_wrapper('node', $nid);

$mail = $wrapper­>author­>mail­>value();

$wrapper­>author­>mail­>set('[email protected]');

$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');

Page 15: Drupalize your data use entities

Metadata Wrapper

$wrapper = entity_metadata_wrapper('node', $nid);

$mail = $wrapper­>author­>mail­>value();

$wrapper­>author­>mail­>set('[email protected]');

$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');

Page 16: Drupalize your data use entities

Entity property info

$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',

);

Page 17: Drupalize your data use entities

Property info?

• Unified access to entity data

• Validation

• Access information

Page 18: Drupalize your data use entities

How modules use it

• Drupal Commerce, VBO, OG

• Rules, Search API

• Microdata

• RestWS, WSClient

• Entity tokens

• Entity Views

Page 19: Drupalize your data use entities

Providing an entity type

• Implement hook_entity_info()

• Specify your 'controller class'

• Implement hook_schema()

Page 20: Drupalize your data use entities

Entity API module

$profile = entity_create('profile2', array(

  'type' => 'main',

  'user'=> $account,

));

$profile­>save();

$profile­>delete();

entity_delete_multiple('profile2', array(1, 2, 3));

Page 21: Drupalize your data use entities

Integrating your entity type

TokensViews Rules

PropertyInfo

Field API

SchemaCRUD

controllerInfo

XY

Page 22: Drupalize your data use entities

Exportable entities

similar to CTools exportables, but...

• unified CRUD interface

• synced to the DB

• regular CRUD hooks

• Views, Tokens, Features, i18n, ...

Page 23: Drupalize your data use entities

Integrating your exportable entity

SchemaCRUD

controllerInfo

Tokens

PropertyInfo

Views i18nFeaturesAdmin

UI

Page 24: Drupalize your data use entities

Apocalypse now?

• Provide an entity

• get a bullshit of not fitting stuff

• Find a good metafa, or cite for it.

• is it an elephant or moskito?

Page 25: Drupalize your data use entities

Module developers: Think

• what makes it applicable?

• label, URI?

• field?

• custom on,off

• example: no display,no page → no metadata tags

Page 26: Drupalize your data use entities
Page 27: Drupalize your data use entities

Example: Profile2

Profile2:

• Entity type

Profile2 types:

• Entity type

• Bundle of Profile2

• Exportable

Page 28: Drupalize your data use entities

Profile2 torn apart

• CRUD, Field API

• Permissions, Access

• Profile form, display

• Admin UI, I18n

• Views, CTools, Rules, Tokens, Features, Search API

Your job

Page 29: Drupalize your data use entities

homework done,let's enjoy it.

Page 30: Drupalize your data use entities

• NoSQL, Doctrine, PHPCR

• Remote entities

• Data integration

Page 31: Drupalize your data use entities

Non-DB entities+

Fields?

Page 32: Drupalize your data use entities

Non-DB entities

Field storage

Info

PropertyInfo

Field API

SchemaControllerInfo TokensRulesViews XY

CRUDcontroller

Page 33: Drupalize your data use entities

Non-DB entities

CRUDcontroller

InfoProperty

Info

SchemaControllerInfo TokensRulesViews XY

Page 34: Drupalize your data use entities

What does it buy us?

• Classed CRUD API

• CRUD Hooks

• Tokens

• Entity reference

• Rules, Rules Links

• Search API, Views integration, RestWS

• …

Page 35: Drupalize your data use entities

Drupalize your data!

Page 36: Drupalize your data use entities

howto slide?

Page 37: Drupalize your data use entities

Example...

Page 38: Drupalize your data use entities

Google Picasa entities

http://drupal.org/sandbox/fago/1493180

Page 39: Drupalize your data use entities
Page 40: Drupalize your data use entities
Page 41: Drupalize your data use entities

Room for improvements

• EFQ Views

• Ready-only mode

• Generated display

• Generated form

Page 42: Drupalize your data use entities

Drupal 8

Page 43: Drupalize your data use entities

Comments in Drupal 8

$comment = entity_create('comment', array(

  'nid' => $node­>nid,

));

$comment­>save();

echo $comment­>id();

$comment­>delete();

entity_delete_multiple('comment', array(1, 2, 3));

Page 44: Drupalize your data use entities

Comments in Drupal 8

class CommentStorageController extends 

EntityDatabaseStorageController {

}

class Comment extends Entity {

 …

}

class Entity implements EntityInterface {

 …

}

Page 45: Drupalize your data use entities

Questions?

Page 46: Drupalize your data use entities

What did you think?

Locate this session on theDrupalCon Denver website

http://denver2012.drupal.org/program

Click the “Take the Survey” link.

Thank You!