Entities

Mo., 16/04/2012 - 15:20
Body

Was sind Entities

Entities sind "Dinge", die Information, die Daten mit denen wir in unserer Drupal Anwendung arbeiten. Ein Node, Taxonomy, User, Kommentar sind z.B. alles Entities. An Entities kann man Felder anhängen, d.h. man kan. an alles Felder anhängen. Z.b imagefields an Taxonomy Terms. Es ist nun also möglich eigene Datenstrukturen zu definieren, mit eigener Datenbanktabelle mit einem standardisierten weg Felder anzuhängen. Man muss also nicht mehr nodes in irgendwas ummurksen was sie eigentlich nicht sind (so wie in d6). Entitis sind also Ein Weg Daten zu beschreiben die für den eigenen Anwendungsfall einzigartig sind. Sie haben eine eigenen DB Tabelle. Gleichzeitig kann aber die Drupal Field PAPI genutzt werden um Felder anzuhängen und man kann so andere API Funktionen nutzen und z.B Views nutzen etc. Eine solide Methode ohne eigenes rumgefrickel (und genau deswegen benutzt man ja ein Framework).

Eine Entity programmieren

Einfache Entity mit einer ID(pid) und einem Feld(note).

Schritt 1, Schema beschreiben

Der Code folgende erzeugt die Datenbank Tabelle.
  1.  
  2. function myetity_schema() {
  3.     $schema['myetity'] = array (
  4.       'description' => 'The main store for our entity',
  5.       'fields' => array(
  6.         'pid' => array(
  7.           'description' => 'Primary key for our table of myetity notes',
  8.           'type' => 'serial',
  9.           'unsigned' => TRUE,
  10.           'not null' => TRUE,
  11.         ),
  12.         'note' => array(
  13.           'description' => 'The actual note',
  14.           'type' => 'varchar',
  15.           'length' => '255',
  16.           'not null' => TRUE
  17.         ),
  18.       ),
  19.       'primary key' => array('pid'),
  20.     );
  21.    
  22.     return $schema;
  23.   }

Schritt 2, hook_entity_info()

Tabelle festlegen, Name, Label etc.
  1. function myetity_entity_info(){
  2.   $myetity_info['myetity'] = array(
  3.     'label' => t('myetity Note'),
  4.     'controller class' => 'MyEntityController',
  5.     'base table' => 'myetity',
  6.     'uri callback' => 'myetity_uri',
  7.     'fieldable' => TRUE,
  8.     'entity keys' => array(
  9.       'id' => 'pid',
  10.     ),
  11.     'static cache' => TRUE,
  12.     'bundles' => array(
  13.       'myetity'=> array(
  14.         'label' => 'My Entity',
  15.         'admin' => array(
  16.           'path' => 'admin/structure/myetity/manage',
  17.           'access arguments' => array('administer myetity'),
  18.         ),
  19.       ),
  20.     ),
  21.     'view modes' => array(
  22.       'full' => array(
  23.         'label' => t('Full myetity'),
  24.         'custom settings' =>  FALSE,
  25.       ),
  26.     )
  27.   );
  28.  
  29.   return $myetity_info;
  30. }
Wir legen eine Kontrollklasse fest, MyEntityController. Die Klasse ist eine Unterklasse von DrupalDefaultEntityController. Diese Kontrollklasse schreibt man am besten in ein separates File myentity.controller.inc
  1.   class MyEntityController extends DrupalDefaultEntityController{
  2.  
  3.   /* we use that later*
  4.   public function save($postit) {
  5.     drupal_write_record('postit', $postit);
  6.     field_attach_insert('postit', $postit);
  7.     module_invoke_all('entity_insert', 'postit', $postit);
  8.     return $postit;
  9.   }
  10.  
  11.   }
Nun müssen wir den uri callback myetity_uri definieren:
  1.   function myetity_uri($myentity){
  2.     return array(
  3.       'path' => 'myentity/' . $myentity->id,
  4.     );
  5.   }

Entity Laden

  1.   function myetity_load($pid = NULL, $reset = FALSE){
  2.     $pids = (isset ($pid) ? array($pid) : array());
  3.     $myetity= myetity_load_multiple($pids, $reset);
  4.     return $myetity ? reset ($myetity) : FALSE;
  5.   }
  6.  
  7.   function myetity_load_multiple($pids = array(), $conditions = array(), $reset = FALSE){
  8.     return entity_load('myetity', $pids, $conditions, $reset);
  9.   }

Entrity Menü, Rechte, Info, Felder ..

  1. function myetity_menu(){
  2.   $items['admin/structure/myetity/manage'] = array(
  3.     'title' => 'myetity Admin',
  4.     'description' => 'Manage myetity structure',
  5.     'page callback' => 'myetity_info',
  6.     'access arguments' => array('administer myetity'),
  7.   );
  8.   $items['myetity/%myetity'] = array(
  9.     'title callback' => 'myetity_page_title',
  10.     'title arguments' => array(1),
  11.     'page callback' => 'myetity_page_view',
  12.     'page arguments' => array(1),
  13.     'access arguments' => array('view myetity'),
  14.     'type' => MENU_CALLBACK,
  15.   );
  16.   return $items;
  17. }
  18.  
  19. function myetity_permission(){
  20.     return array(
  21.     'administer myetity' =>  array(
  22.       'title' => t('Administer myetity'),
  23.       'restrict access' => TRUE,
  24.     ),
  25.     'view myetity' => array(
  26.       'title' => t('View myetity'),
  27.     )
  28.   );
  29. }
  30.  
  31.  
  32. function myetity_info() {
  33.   return ('Welcome to the administration page for your myetity!');
  34. }
  35.  
  36. function myentity_page_title($myetity){
  37.   return $myetity->pid;
  38. }
  39.  
  40. function myetity_page_view($myetity, $view_mode = 'full'){
  41.   $myetity->content = array();
  42.  
  43.   // Build fields content.
  44.   field_attach_prepare_view('myetity', array($myetity->pid => $myetity), $view_mode);
  45.   entity_prepare_view('myetity', array($myetity->pid => $myetity));
  46.   $myetity->content += field_attach_view('myetity', $myetity, $view_mode);
  47.  
  48.   return $myetity->content;
  49. }
  50.  
  51.  
  52. function myetity_field_extra_fields() {
  53.   $return = array();
  54.   $return['myetity']['myetity'] = array(
  55.     'form' => array(
  56.       'note' => array(
  57.         'label' => t('Note'),
  58.         'description' => t('myetity Note'),
  59.       ),
  60.     ),
  61.   );
  62.  
  63.   return $return;
  64. }
Bis jetzt gibt es ken wirkliches UI. Nächster Schritt.

UI

  1.   $items['myentity/add'] = array(
  2.       'title' => 'Add myentity!',
  3.       'page callback' => 'myentity_add',
  4.       'access arguments' => array('create myentity'),
  5.     );
  6.  
  7.   /* callback */  
  8.   function myentity_add() {
  9.     $myentity= (object) array (
  10.       'pid' => '',
  11.       'type' => 'myentity',
  12.       'note' => '',
  13.     );
  14.    
  15.     return drupal_get_form('myentity_add_form', $myentity);
  16.   }
  17.  
  18.   function myentity_add_form($form, &$form_state, $myentity) {
  19.     $form['note'] = array(
  20.       '#type' => 'textfield',
  21.       '#title' => t('Note'),
  22.       '#required' => TRUE,
  23.     );
  24.    
  25.     $form['submit'] = array(
  26.       '#type' => 'submit',
  27.       '#value' => t('Save'),
  28.     );
  29.    
  30.     field_attach_form('myentity', $myentity, $form, $form_state);
  31.    
  32.     return $form;
  33.   }
  34.  
  35. /* validierung*/
  36.  
  37. function myentity_add_form_validate($form, &$form_state) {
  38.   $myentity_submisttion = (object) $form_state['values'];
  39.   field_attach_form_validate('myentity', $myentity_submisttion, $form, $form_state);
  40. }
  41.  
  42. function myentity_add_form_submit($form, &$form_state) {
  43.   $myentity_submission = (object) $form_state['values'];
  44.   field_attach_submit('myentity', $myentity_submission, $form, $form_state);
  45.   $myentity= myentity_save($myentity_submission);
  46.   $form_state['redirect'] = "myentity/$myentity->pid";
  47. }
  48.  
  49. /* wir benutzen den myentity controller mit seiner save funktion */
  50. function myentity_save(&$postit) {
  51.   return entity_get_controller('myentity')->save($myentity);
  52. }
Delete, Edit Funktionalität fehlt.

Entity Beispiele

Im großartigen examples modul befindet sich ein Beispiel modul. http://drupal.org/project/examples http://drupal.org/project/field_collection

API, Hilfe, Module

http://drupal.org/project/entity http://drupal.org/project/eck http://drupal.org/project/model http://drupal.org/node/878784 | entity api handbuch

Artikel

http://www.istos.it/en/blog/drupal/drupal-entities-part-1-moving-beyond-nodes | 3 Teile http://www.trellon.com/content/blog/creating-own-entities-entity-api http://de.wetena.com/blog/2011/04/13/entities-in-drupal-7

Videos/Präsentationen

http://bxl2011.drupaldays.org/node/313 | entity api by fargo

Sonstiges

http://en.wikipedia.org/wiki/Entity-relationship_model

Bücher

Drupal 7 Module Development BuchImage removed.
Drupal
Add new comment
The content of this field is kept private and will not be shown publicly.

Plain text

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <drupal-entity data-*>
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.