your entity, your code
TRANSCRIPT
![Page 1: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/1.jpg)
![Page 3: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/3.jpg)
me.yml
users:mavimo:
name: Marco Vito Moscaritoloemail: mavimo-at-gmail.comtwitter: @mavimotechnologies: [ php, drupal, symfony ]role: [ developer, teacher, architect ]work: freelancefounder: @sparkfabrik
![Page 4: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/4.jpg)
Whyentities?
We cancreatecustomnode type…
![Page 5: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/5.jpg)
…right?
![Page 6: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/6.jpg)
• D6 & 5 $node = new stdClass();
// Sad L
• D7 $node = new stdClass();
// DrupalEntityControllerInterface// EntityFieldQuery, …
• D8 $node = new Drupal\node\Entity\Node();
// Yah yahhhh!!! J
Abitofhistory
![Page 7: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/7.jpg)
• Entity (Node,User,Comment,Taxonomy,…)
• Bundle (Nodetype,Vocabularies,…)
• Property (Nodeid,nodetitle,vocabularyname,…)
• Field (Body,Imagefield,Termreferences,…)
Coreelements
![Page 8: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/8.jpg)
D7 - 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 9: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/9.jpg)
D7 - Entity API
![Page 10: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/10.jpg)
D7 - 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 11: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/11.jpg)
D7 - 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 12: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/12.jpg)
D7 - EntityAPIControllerclass 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 13: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/13.jpg)
D7 - 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 14: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/14.jpg)
D7 – Entity(AdminUI)
![Page 15: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/15.jpg)
D7 - EntityUI(admin)
![Page 16: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/16.jpg)
D7 - EntityUI(edit)
![Page 17: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/17.jpg)
Toomuchwork…why???
Integrationview, rules, features
Code testabilityeasy to isolate
Reusabilitycode once, use more
Remote entity social network, webservices
Drupal 8 ;-)
![Page 18: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/18.jpg)
Integration(eg:views)
![Page 19: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/19.jpg)
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 20: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/20.jpg)
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 21: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/21.jpg)
D7 - CreateCustomEntity…Fast! (1)
drush entity-scaffold entity_name ddayFile dday/entity_name/includes/entity_name.admin.inc created. [ok]
File dday/entity_name/includes/entity_name.class.inc created. [ok]
File dday/entity_name/includes/entity_name.controller.inc created. [ok]
File dday/entity_name/includes/entity_name.type.controller.inc created. [ok]
File dday/entity_name/includes/entity_name_type.admin.inc created. [ok]
File dday/entity_name/entity_name.info created. [ok]
File dday/entity_name/entity_name.install created. [ok]
File dday/entity_name/entity_name.tpl.php created. [ok]
File dday/entity_name/entity_name.module created. [ok]
https://www.drupal.org/project/entity_scaffold
![Page 22: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/22.jpg)
D7 - CreateCustomEntity…Fast! (2)
https://www.drupal.org/project/eck
![Page 23: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/23.jpg)
…Drupal8?
![Page 24: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/24.jpg)
Thanks!
![Page 25: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/25.jpg)
ContentEntityTypeInterface(Node,User,Comment,Taxonomy,…)
ConfigEntityTypeInterface(Configurationinformation)
…(eg:Bundle)
D8 - Entitytype
![Page 26: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/26.jpg)
D8 - Entity TypeModule namespacedDrupal\my_module\Entity\MyCustomeEntity()
Defined folder in modulesrc/Entity/MyCustomEntity.php
INFORMATION MANAGED BY ANNOTATION/*** @XyzEntityType( … )*/
Can be “code only”config/install/my_custom_entity.sample_1.yml
Unit testableclass MyCustomEntityTest extends Drupal\Tests\UnitTestCase {}
![Page 27: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/27.jpg)
D8 - ContentEntityType
Require database definitionhook_schema()
Can be fildable/REVISIONABLEbaseFieldDefinition()
CAN HAVE CUSTOM BEHAVIORredefine [pre|post][Save|Load|Delete] methods
CAN PROVIDE CUSTOM ACCESS RULEScheckAccess() and checkCreateAccess()
CAN PROVIDE CUSTOM HANDLERSstrorage, view_builder, route_provider, form, translation, config_export...
![Page 28: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/28.jpg)
D8 - ContentEntityType/*** @ContentEntityType(* id = "my_content",* label = @Translation("My Content"),* bundle_label = @Translation("My Content type"),* handlers = { ... },* base_table = "my_entity",* data_table = "my_entity_field_data",* revision_table = "my_entity_revision",* revision_data_table = "my_entity_field_revision",* translatable = TRUE, * entity_keys = { ... },* bundle_entity_type = "my_entity_type",* field_ui_base_route = "entity.my_entity_type.edit_form",* common_reference_target = TRUE,* permission_granularity = "bundle",* links = { ... }* )*/class MyContentEntity extends ContentEntityBase implementsMyContentEntityInterface { }
![Page 29: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/29.jpg)
D8 - ConfigEntityType
DifferencecomparedtoContentEntityType:
• Schema file definitionInsteadofdatabaseschemadefinition,using:config/schema/my_entity.schema.yml
• INTEGRATED with CMI APIfor exportabilitySeededicatedtalk.
• No field
![Page 30: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/30.jpg)
D8 - ConfigEntityType/*** @ConfigEntityType(* id = "my_config",* label = @Translation("My config"),* handlers = {* "list_builder" = "Drupal\my_module\Controller\MyConfigListBuilder",* "form" = { ... }* },* config_prefix = "my_config",* admin_permission = "administer site configuration",* entity_keys = { ... },* links = { ... }* )*/class MyConfigEntity extends ConfigEntityBase implementsMyConfigEntityInterface { ... }
![Page 31: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/31.jpg)
my_module/config/schema/my_config.schema.yml
my_module.my_config.*:type: config_entitylabel: My config’mapping:
id:type: stringlabel: 'ID’
label:type: labellabel: 'Label'
![Page 32: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/32.jpg)
D8 - CreateCustomEntity…Fast!
drupal generate:entity:content \--module=dday \--entity-name=my_content_entity \--entity-class=MyContentEntity \--label="My Content Entity”
Generated or updated files Site path: ~/dday1 - modules/custom/dday/dday.routing.yml2 - modules/custom/dday/dday.permissions.yml3 - modules/custom/dday/dday.links.menu.yml4 - modules/custom/dday/dday.links.task.yml. . . 13 - modules/custom/dday/src/Entity/Form/MyContentEntityDeleteForm.php14 - modules/custom/dday/my_content_entity.page.inc15 - modules/custom/dday/templates/My Content Entity.html.twig
https://www.drupal.org/project/console
![Page 33: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/33.jpg)
Question?
Thank you!
![Page 34: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/34.jpg)
milan2016.drupaldays.o
rg
![Page 35: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/35.jpg)
References
http://joshaust.in/wp-content/uploads/2012/06/Entities-and-Bundles-in-Drupal-7.pdf
https://www.drupal.org/project/entity
https://www.drupal.org/project/eck
https://www.drupal.org/project/entity_scaffold
http://www.bluespark.com/blog/drupal-entities-part-1-moving-beyond-nodes
http://www.sitepoint.com/build-custom-entities-drupal-setup/
https://www.drupal.org/developing/api/entity
https://hechoendrupal.gitbooks.io/drupal-console/content/en/index.html
http://www.wunderkraut.com/blog/configuration-entities-in-drupal-8/2014-07-14
http://nuvole.org/blog/drupal-8
http://blog.oskoui-oskoui.com/?p=8218
https://codedrop.com.au/blog/creating-custom-config-entities-drupal-8
![Page 36: Your Entity, Your Code](https://reader031.vdocument.in/reader031/viewer/2022022200/58a9a0391a28abc2518b641b/html5/thumbnails/36.jpg)