build your own entity with drupal

Post on 28-Jul-2015

85 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Build your own Entity in Drupal

Marco Vito Moscaritolo

hook_user_info()

return [

‘name’ => ‘Marco Vito Moscaritolo’,

‘username’ => ‘mavimo’,

‘mail’ => ‘mavimo@gmail.com’,

‘twitter’ => ‘@mavimo’,

‘field_tags’ => [

‘drupal’, ‘php’, ‘developer’,

‘associazione drupal italia’,

],

‘field_companies’ => [

‘agavee’, ‘sparkfabrik’, ‘freelance’,

],

];

We can create customnode type… right?

Why entities ?

Elements

• Entity (Node, User, Comment, Taxonomy, …)

• Bundle (Node type, Vocabularies, …)

• Property (Node id, node title, vocabulary name,

…)

• Field (Body, Image field, Term references,

…)

hook_entity_info()

return array(

’entity_name' => array(

'label' => t(’Custom entity'),

'controller class' => ’CustomEntityController',

'base table' => ’custom_entity',

'uri callback' => ’custom_entity_uri',

'fieldable' => TRUE,

'entity keys' => array( /* ... */ )

'bundle keys' => array(

'bundle' => 'machine_name',

),

'bundles' => array( /* ... */ ),

'view modes' => array( /* ... */ ),

),

);

Entity API

hook_entity_info() – extended by Entity API

return array(

’entity_name' => array(

// ...

’entity class' => ’CustomEntity',

// ...

'views controller class' => ’CustomEntityViewsController',

// ...

'exportable’ => TRUE,

// ...

'admin ui' => array(

// ...

'controller class' => ’CustomEntityUIController',

),

),

);

Entity

Bla bla…class CustomEntity extends Entity {

public function __construct(array $values = [], $entity_type = NULL) {}

public function identifier() {}

public function bundle() {}

public function uri() {}

public function hasStatus($status) {}

public function save() {}

public function delete() {}

public function view($view_mode = 'full', $lang = NULL, $page = NULL) {}

public function buildContent($view_mode = 'full', $langcode = NULL) {}

// ...

}

Entity UI management

EntityAPIController

class CustomEntityController extends EntityAPIController {

public function __construct($entityType) {}

public function query($ids, $conditions, $revision_id = FALSE) {}

public function load($ids = array(), $conditions = array()) {}

public function invoke($hook, $entity) {}

public function delete($ids, DatabaseTransaction $trans = NULL) {}

public function save($entity, DatabaseTransaction $trans = NULL) {}

public function create(array $values = array()) {}

public function buildContent($entity, $view_mode = 'full’, $lang = NULL, $content = []) {}

public function view($entities, $view_mode = 'full’, $langcode = NULL, $page = NULL) {}

// ...

}

Automatically create Entity UI

EntityDefaultUIController

Bla bla…class CustomEntityUIController extends EntityDefaultUIController {

public function __construct($entity_type, $entity_info) {}

public function hook_menu() {}

public function hook_forms() {}

public function overviewTable($conditions = array()) {}

public function overviewForm($form, &$form_state) {}

public function operationForm($form, &$form_state, $entity, $op) {}

// and also [overview|operation]Form[Submit|Validate] methods.

public function applyOperation($op, $entity) {}

// ...

}

Automatically create Admin UI

Too much work… why???

• Integration (view, rules, feature, …)

• Code testability (see dedicated session, …)

• Reusability (feature on different projects,

…)

• Remote entity (social network, WS,…)

• Drupal 8 “is coming” ;)

Integration (eg: views)

EntityFieldQuery

$query = new EntityFieldQuery();

$query->entityCondition('entity_type', 'custom_entity')

->entityCondition('bundle', 'my_bundle')

->propertyCondition('uid', $user->uid, '=')

->fieldCondition('field_faculty_tag', 'tid', 10)

->fieldOrderBy('field_faculty_tag', 'tid', 'DESC')

->range(0, 10);

$result = $query->execute();

if (isset($result['custom_entity'])) {

$items = entity_load('custom_entity’, array_keys($result['custom_entity']));

}

EntityMetadataWrapper

$wrapper = entity_metadata_wrapper('custom_entity', $entity);

$wrapper->author->mail->set('demo@sidcamp.it');

$wrapper->description->value('This is a demo!');

$labels = array();

foreach ($wrapper->field_faculty_tag->getIterator() as $delta => $term_wrapper) {

$labels[] = $term_wrapper->label->value();

}

$wrapper->save();

… Drupal 8 ?

Create Custom Entity… Fast! (1)

drush entity-scaffold entity_name sidcampFile sidcamp/entity_name/includes/entity_name.admin.inc created. [ok]

File sidcamp/entity_name/includes/entity_name.class.inc created. [ok]

File sidcamp/entity_name/includes/entity_name.controller.inc created. [ok]

File sidcamp/entity_name/includes/entity_name.type.controller.inc created. [ok]

File sidcamp/entity_name/includes/entity_name_type.admin.inc created. [ok]

File sidcamp/entity_name/entity_name.info created. [ok]

File sidcamp/entity_name/entity_name.install created. [ok]

File sidcamp/entity_name/entity_name.tpl.php created. [ok]

File sidcamp/entity_name/entity_name.module created. [ok]

https://www.drupal.org/project/

entity_scaffold

Create Custom Entity… Fast! (2)

https://www.drupal.org/project/eck

Question?

Thank you!

top related