Top Banner
Build your own Entity in Drupal Marco Vito Moscaritolo
22

Build your own entity with Drupal

Jul 28, 2015

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Build your own entity with Drupal

Build your own Entity in Drupal

Marco Vito Moscaritolo

Page 2: Build your own entity with Drupal

hook_user_info()

return [

‘name’ => ‘Marco Vito Moscaritolo’,

‘username’ => ‘mavimo’,

‘mail’ => ‘[email protected]’,

‘twitter’ => ‘@mavimo’,

‘field_tags’ => [

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

‘associazione drupal italia’,

],

‘field_companies’ => [

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

],

];

Page 3: Build your own entity with Drupal

We can create customnode type… right?

Why entities ?

Page 4: Build your own entity with Drupal

Elements

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

• Bundle (Node type, Vocabularies, …)

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

…)

• Field (Body, Image field, Term references,

…)

Page 5: Build your own entity with Drupal

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( /* ... */ ),

),

);

Page 6: Build your own entity with Drupal

Entity API

Page 7: Build your own entity with Drupal

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

),

),

);

Page 8: Build your own entity with Drupal

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) {}

// ...

}

Page 9: Build your own entity with Drupal

Entity UI management

Page 10: Build your own entity with Drupal

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) {}

// ...

}

Page 11: Build your own entity with Drupal

Automatically create Entity UI

Page 12: Build your own entity with Drupal

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) {}

// ...

}

Page 13: Build your own entity with Drupal

Automatically create Admin UI

Page 14: Build your own entity with Drupal

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” ;)

Page 15: Build your own entity with Drupal

Integration (eg: views)

Page 16: Build your own entity with Drupal

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

}

Page 17: Build your own entity with Drupal

EntityMetadataWrapper

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

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

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

Page 18: Build your own entity with Drupal

… Drupal 8 ?

Page 19: Build your own entity with Drupal

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

Page 20: Build your own entity with Drupal

Create Custom Entity… Fast! (2)

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

Page 21: Build your own entity with Drupal

Question?

Thank you!