SlideShare uma empresa Scribd logo
1 de 18
Baixar para ler offline
Drupal 7:
von Objekten
zu Entities
Drupal Meetup Stuttgart 3.11.2011 - bb
Drupal 6: Objekte mit proprietären APIs

Objekte sind z.B.

- Nodes
- Terms
- User
- Blocks
- Files

Felder -> CCK ! (nur für Nodes)
Drupal 6: Objekte mit proprietären APIs

Beispiele:
node_load( $param = array(), $revision = NULL, $reset = NULL)
user_save($account, $array = array(), $category = 'account')
file_delete( $path)
taxonomy_get_term_by_name($name)
Drupal 6: Objekte mit proprietären APIs

Beispiele:
node_load( $param = array(), $revision = NULL, $reset = NULL)
user_save($account, $array = array(), $category = 'account')
file_delete( $path)
taxonomy_get_term_by_name($name)



Problem:
- uneinheitliche Behandlung durch spezifische APIs
- Felder nur über contributed modules (CCK, Content Profile, ...)
Ziel: Vereinheitlichung - Objekt & CRUD-API

Ein Objekt kann


- erzeugt werden     (Create)
- gelesen werden     (Read)
- verändert werden   (Update)
- gelöscht werden    (Delete)
Ziel: Vereinheitlichung - Objekt & Field-API

Zu JEDEM Objekt können beliebige Felder angelegt
werden

- Text
- Zahlen
- Links
- Referenzen
- Dateien
- ...

Auch diese können gelesen, verändert, gelöscht werden.
Drupal 7: Voilá, Entities !
Konzepte:                  Beispiele:

- Entity Types             - node, user, vocabulary

- Entity Bundles           - page, article, blog

- Entity Properties        - author, created, status

- Fields                   - field_xyz



& Entity API / Field API
Codevergleich Drupal 6 <-> Drupal 7
Drupal 6 (Database API + Node API)

$query = "SELECT nid FROM {node}
           WHERE (type = 'article' AND status = '1')
           ORDER BY created DESC";
$result = db_query($query);
while ($nid = db_result($result)) {
    $articles[] = node_load($res);
}


Drupal 7 (Entity API):
$query = new EntityFieldQuery();
$result = $query->entityCondition     ('entity_type', 'node')
                ->entityCondition     ('bundle'), 'article')
                ->propertyCondition   ('status', '1');
                ->propertyOrderBy     ('created', 'DESC');
                ->execute();
$articles = entity_load ('node', array_keys($result['node']);
Abstraktionsebene: Entity Controller
    Drupal 6:           Drupal 7:
       Modul                Modul




        API            Entity Controller




       Daten                  API




                            Daten
Bsp: Drupal Variablen als Entities
/**                                                         class VariableController extends
 * Implements hook_entity_info()                                                     DrupalDefaultEntityController {
 */                                                         }
function variables_entity_info() {                          function variable_load($variable_id = NULL, $reset =
                                                                                   FALSE) {
    $info['variable'] = array(
                                                              $variable_ids = (isset($variable_id) ?
      'label' => t('Drupal Variable'),                                         array($variable_id) : array());
                                                              $variable = variable_load_multiple($variable_ids,
      'controller class' => 'VariableController',
                                                                                                 $reset);
      'base table' => 'variable',                             return $variable ? reset($variable) : FALSE;
      'uri callback' => 'variable_uri',                     }

      'fieldable' => FALSE,                                 function variable_load_multiple($variable_ids = array(),
                                                                           $conditions = array(), $reset = FALSE) {
      'entity keys' => array('id' => 'name'),                 return entity_load('variable', $variable_ids,
      'static cache' => TRUE,                                                     $conditions, $reset);
                                                            }
      'bundles' => array(
                                                            function variable_uri($variable) {




                Entity Controller
          'variable' => array('label' => 'Variable'),         return array(
      ),                                                         'path' => 'variable/' . $variable->name,
                                                              );
      'view modes' => array(                                }
          'full' => array('label' => t('Full'),             function variables_field_extra_fields() {
                         'custom settings' =>     FALSE),     $fields = array();
                                                              $fields['variable']['value'] = array(
      )                                                          'form' => array(
    );                                                              'name' => array(
                                                                       'label' => t('value'),
    return $info;                                                      'description' => t('Variable Value'),
}                                                                   ),
                                                                 ),
                                                              );
                                                              return $fields;
                                                            }
Bsp: Drupal Variablen als Entities
/**
 * Implements hook_menu()
 */                                                 function variable_list() {
function mymodule_menu() {                            $query = new EntityFieldQuery();
    $items['variable/%variable'] = array(             $result = $query->entityCondition
       'title callback' => 'variable_page_title',                       ('entity_type', 'variable')
       'title arguments' => array(1),                                 ->propertyOrderBy('name','DESC')
       'page callback' => 'variable_page_view',                       ->execute();
       'page arguments' => array(1),                  $variables = entity_load('variable',
       'type' => MENU_CALLBACK,                                    array_keys($result['variable']));
       'access callback' => TRUE,
    );                                                  $output = '';
    $items['variablelist'] = array(
                                                        foreach($variables as $var){



             Custom Module
       'title' => 'List of Variables',
       'page callback' => 'variable_list',                $output .= l($var->name,'variable/' .
       'type' => MENU_CALLBACK,                                      $var->name) . '<br />';
       'access callback' => TRUE,                       }
    );                                                  return $output;
                                                    }
    return $items;
}
function variable_page_title($variable) {
  return 'Variable: ' . $variable->name;
}
function variable_page_view($variable) {
  return '&lt;' . $variable->value . '&gt;';
}
Ausgabe des Variablen-Beispiels
/variablelist:    /variable/user_picture_dimensions:
Drupal 7: Vieles ist schon da, aber...
Beispiele aus dem D7 Core API:
function node_load($nid = NULL, $vid = NULL, $reset = FALSE) {
  $nids = (isset($nid) ? array($nid) : array());
  $conditions = (isset($vid) ? array('vid' => $vid) : array());
  $node = node_load_multiple($nids, $conditions, $reset);
  return $node ? reset($node) : FALSE;
}
function node_load_multiple($nids = array(), $conditions = array(), $reset = FALSE) {
  return entity_load('node', $nids, $conditions, $reset);
}
Drupal 7: Vieles ist schon da, aber...
Beispiele aus dem D7 Core API:
function node_load($nid = NULL, $vid = NULL, $reset = FALSE) {
  $nids = (isset($nid) ? array($nid) : array());
  $conditions = (isset($vid) ? array('vid' => $vid) : array());
  $node = node_load_multiple($nids, $conditions, $reset);
  return $node ? reset($node) : FALSE;
}
function node_load_multiple($nids = array(), $conditions = array(), $reset = FALSE) {
  return entity_load('node', $nids, $conditions, $reset);
}


function taxonomy_get_term_by_name($name) {
  return taxonomy_term_load_multiple(array(), array('name' => trim($name)));
}
function taxonomy_term_load_multiple($tids = array(), $conditions = array()) {
  return entity_load('taxonomy_term', $tids, $conditions);
}
Drupal 7: Vieles ist schon da, aber...
Vieles ist noch unvollständig:

- Remember CRUD? Im Core gibt es nur das "R" !
- Es gibt noch kein User Interface zu Entities
- Integration z.B. in Views, Panels, Rules, Features, Pathauto, Display Suite, ...


Manches ist nur vorläufig:
function taxonomy_get_term_by_name($name) {
  return taxonomy_term_load_multiple(array(), array('name' => trim($name)));
}
function taxonomy_term_load_multiple($tids = array(), $conditions = array()) {
  return entity_load('taxonomy_term', $tids, $conditions);
}

Die Verwendung von $conditions ist deprecated, und wird in D8 wahrscheinlich
wieder verschwinden!
Abhilfe: Contributed Modules
Entity API - Ergänzt das Core API, z.B. mit vollständigem CRUD
http://drupal.org/project/entity

File Entity - Macht Files fieldable
http://drupal.org/project/file_entity

Entity Construction Kit (ECK) - User Interface zur Verwaltung von Entities
http://drupal.org/project/eck

Entity Reference - Referenzen auf Entities, ähnlich node / user reference
http://drupal.org/project/entityreference

Manches wird mit D8 im Core landen, anderes in der Versenkung
verschwinden - schwer zu beurteilen!
Demo: ECK, Views, DB
- Erzeugen von Entity Types, Bundles, Entities

- Listen in Views

- Repräsentation in der Datenbank
Fragen / Diskussion
Zum Weiterlesen:

http://www.davetech.com/blog/introduction_drupal_7_entities_and_fields
http://www.istos.it/category/blog-tags/drupal-entities
http://de.wetena.com/blog/2011/04/13/entities-in-drupal-7
http://www.slideshare.net/alexshr/entity-api




Diese Folien als PDF:

http://www.slideshare.net/drubb/drupal-entities

Mais conteúdo relacionado

Mais de drubb

Barrierefreie Webseiten
Barrierefreie WebseitenBarrierefreie Webseiten
Barrierefreie Webseitendrubb
 
Drupal 9 Entity Bundle Classes
Drupal 9 Entity Bundle ClassesDrupal 9 Entity Bundle Classes
Drupal 9 Entity Bundle Classesdrubb
 
Drupal 8 Dependency Injection Using Traits
Drupal 8 Dependency Injection Using TraitsDrupal 8 Dependency Injection Using Traits
Drupal 8 Dependency Injection Using Traitsdrubb
 
Closing the Drupal Hosting Gap - A Review of Wodby
Closing the Drupal Hosting Gap - A Review of WodbyClosing the Drupal Hosting Gap - A Review of Wodby
Closing the Drupal Hosting Gap - A Review of Wodbydrubb
 
Composer & Drupal
Composer & DrupalComposer & Drupal
Composer & Drupaldrubb
 
Drupal 8: Theming
Drupal 8: ThemingDrupal 8: Theming
Drupal 8: Themingdrubb
 
Drupal 8: Entities
Drupal 8: EntitiesDrupal 8: Entities
Drupal 8: Entitiesdrubb
 
Drupal 8: Forms
Drupal 8: FormsDrupal 8: Forms
Drupal 8: Formsdrubb
 
Drupal 8: Routing & More
Drupal 8: Routing & MoreDrupal 8: Routing & More
Drupal 8: Routing & Moredrubb
 
Drupal 8 Sample Module
Drupal 8 Sample ModuleDrupal 8 Sample Module
Drupal 8 Sample Moduledrubb
 
Headless Drupal
Headless DrupalHeadless Drupal
Headless Drupaldrubb
 
Spamschutzverfahren für Drupal
Spamschutzverfahren für DrupalSpamschutzverfahren für Drupal
Spamschutzverfahren für Drupaldrubb
 
Drupal 8: TWIG Template Engine
Drupal 8:  TWIG Template EngineDrupal 8:  TWIG Template Engine
Drupal 8: TWIG Template Enginedrubb
 
Drupal 8: Neuerungen im Überblick
Drupal 8:  Neuerungen im ÜberblickDrupal 8:  Neuerungen im Überblick
Drupal 8: Neuerungen im Überblickdrubb
 

Mais de drubb (14)

Barrierefreie Webseiten
Barrierefreie WebseitenBarrierefreie Webseiten
Barrierefreie Webseiten
 
Drupal 9 Entity Bundle Classes
Drupal 9 Entity Bundle ClassesDrupal 9 Entity Bundle Classes
Drupal 9 Entity Bundle Classes
 
Drupal 8 Dependency Injection Using Traits
Drupal 8 Dependency Injection Using TraitsDrupal 8 Dependency Injection Using Traits
Drupal 8 Dependency Injection Using Traits
 
Closing the Drupal Hosting Gap - A Review of Wodby
Closing the Drupal Hosting Gap - A Review of WodbyClosing the Drupal Hosting Gap - A Review of Wodby
Closing the Drupal Hosting Gap - A Review of Wodby
 
Composer & Drupal
Composer & DrupalComposer & Drupal
Composer & Drupal
 
Drupal 8: Theming
Drupal 8: ThemingDrupal 8: Theming
Drupal 8: Theming
 
Drupal 8: Entities
Drupal 8: EntitiesDrupal 8: Entities
Drupal 8: Entities
 
Drupal 8: Forms
Drupal 8: FormsDrupal 8: Forms
Drupal 8: Forms
 
Drupal 8: Routing & More
Drupal 8: Routing & MoreDrupal 8: Routing & More
Drupal 8: Routing & More
 
Drupal 8 Sample Module
Drupal 8 Sample ModuleDrupal 8 Sample Module
Drupal 8 Sample Module
 
Headless Drupal
Headless DrupalHeadless Drupal
Headless Drupal
 
Spamschutzverfahren für Drupal
Spamschutzverfahren für DrupalSpamschutzverfahren für Drupal
Spamschutzverfahren für Drupal
 
Drupal 8: TWIG Template Engine
Drupal 8:  TWIG Template EngineDrupal 8:  TWIG Template Engine
Drupal 8: TWIG Template Engine
 
Drupal 8: Neuerungen im Überblick
Drupal 8:  Neuerungen im ÜberblickDrupal 8:  Neuerungen im Überblick
Drupal 8: Neuerungen im Überblick
 

Drupal Entities

  • 1. Drupal 7: von Objekten zu Entities Drupal Meetup Stuttgart 3.11.2011 - bb
  • 2. Drupal 6: Objekte mit proprietären APIs Objekte sind z.B. - Nodes - Terms - User - Blocks - Files Felder -> CCK ! (nur für Nodes)
  • 3. Drupal 6: Objekte mit proprietären APIs Beispiele: node_load( $param = array(), $revision = NULL, $reset = NULL) user_save($account, $array = array(), $category = 'account') file_delete( $path) taxonomy_get_term_by_name($name)
  • 4. Drupal 6: Objekte mit proprietären APIs Beispiele: node_load( $param = array(), $revision = NULL, $reset = NULL) user_save($account, $array = array(), $category = 'account') file_delete( $path) taxonomy_get_term_by_name($name) Problem: - uneinheitliche Behandlung durch spezifische APIs - Felder nur über contributed modules (CCK, Content Profile, ...)
  • 5. Ziel: Vereinheitlichung - Objekt & CRUD-API Ein Objekt kann - erzeugt werden (Create) - gelesen werden (Read) - verändert werden (Update) - gelöscht werden (Delete)
  • 6. Ziel: Vereinheitlichung - Objekt & Field-API Zu JEDEM Objekt können beliebige Felder angelegt werden - Text - Zahlen - Links - Referenzen - Dateien - ... Auch diese können gelesen, verändert, gelöscht werden.
  • 7. Drupal 7: Voilá, Entities ! Konzepte: Beispiele: - Entity Types - node, user, vocabulary - Entity Bundles - page, article, blog - Entity Properties - author, created, status - Fields - field_xyz & Entity API / Field API
  • 8. Codevergleich Drupal 6 <-> Drupal 7 Drupal 6 (Database API + Node API) $query = "SELECT nid FROM {node} WHERE (type = 'article' AND status = '1') ORDER BY created DESC"; $result = db_query($query); while ($nid = db_result($result)) { $articles[] = node_load($res); } Drupal 7 (Entity API): $query = new EntityFieldQuery(); $result = $query->entityCondition ('entity_type', 'node') ->entityCondition ('bundle'), 'article') ->propertyCondition ('status', '1'); ->propertyOrderBy ('created', 'DESC'); ->execute(); $articles = entity_load ('node', array_keys($result['node']);
  • 9. Abstraktionsebene: Entity Controller Drupal 6: Drupal 7: Modul Modul API Entity Controller Daten API Daten
  • 10. Bsp: Drupal Variablen als Entities /** class VariableController extends * Implements hook_entity_info() DrupalDefaultEntityController { */ } function variables_entity_info() { function variable_load($variable_id = NULL, $reset = FALSE) { $info['variable'] = array( $variable_ids = (isset($variable_id) ? 'label' => t('Drupal Variable'), array($variable_id) : array()); $variable = variable_load_multiple($variable_ids, 'controller class' => 'VariableController', $reset); 'base table' => 'variable', return $variable ? reset($variable) : FALSE; 'uri callback' => 'variable_uri', } 'fieldable' => FALSE, function variable_load_multiple($variable_ids = array(), $conditions = array(), $reset = FALSE) { 'entity keys' => array('id' => 'name'), return entity_load('variable', $variable_ids, 'static cache' => TRUE, $conditions, $reset); } 'bundles' => array( function variable_uri($variable) { Entity Controller 'variable' => array('label' => 'Variable'), return array( ), 'path' => 'variable/' . $variable->name, ); 'view modes' => array( } 'full' => array('label' => t('Full'), function variables_field_extra_fields() { 'custom settings' => FALSE), $fields = array(); $fields['variable']['value'] = array( ) 'form' => array( ); 'name' => array( 'label' => t('value'), return $info; 'description' => t('Variable Value'), } ), ), ); return $fields; }
  • 11. Bsp: Drupal Variablen als Entities /** * Implements hook_menu() */ function variable_list() { function mymodule_menu() { $query = new EntityFieldQuery(); $items['variable/%variable'] = array( $result = $query->entityCondition 'title callback' => 'variable_page_title', ('entity_type', 'variable') 'title arguments' => array(1), ->propertyOrderBy('name','DESC') 'page callback' => 'variable_page_view', ->execute(); 'page arguments' => array(1), $variables = entity_load('variable', 'type' => MENU_CALLBACK, array_keys($result['variable'])); 'access callback' => TRUE, ); $output = ''; $items['variablelist'] = array( foreach($variables as $var){ Custom Module 'title' => 'List of Variables', 'page callback' => 'variable_list', $output .= l($var->name,'variable/' . 'type' => MENU_CALLBACK, $var->name) . '<br />'; 'access callback' => TRUE, } ); return $output; } return $items; } function variable_page_title($variable) { return 'Variable: ' . $variable->name; } function variable_page_view($variable) { return '&lt;' . $variable->value . '&gt;'; }
  • 12. Ausgabe des Variablen-Beispiels /variablelist: /variable/user_picture_dimensions:
  • 13. Drupal 7: Vieles ist schon da, aber... Beispiele aus dem D7 Core API: function node_load($nid = NULL, $vid = NULL, $reset = FALSE) { $nids = (isset($nid) ? array($nid) : array()); $conditions = (isset($vid) ? array('vid' => $vid) : array()); $node = node_load_multiple($nids, $conditions, $reset); return $node ? reset($node) : FALSE; } function node_load_multiple($nids = array(), $conditions = array(), $reset = FALSE) { return entity_load('node', $nids, $conditions, $reset); }
  • 14. Drupal 7: Vieles ist schon da, aber... Beispiele aus dem D7 Core API: function node_load($nid = NULL, $vid = NULL, $reset = FALSE) { $nids = (isset($nid) ? array($nid) : array()); $conditions = (isset($vid) ? array('vid' => $vid) : array()); $node = node_load_multiple($nids, $conditions, $reset); return $node ? reset($node) : FALSE; } function node_load_multiple($nids = array(), $conditions = array(), $reset = FALSE) { return entity_load('node', $nids, $conditions, $reset); } function taxonomy_get_term_by_name($name) { return taxonomy_term_load_multiple(array(), array('name' => trim($name))); } function taxonomy_term_load_multiple($tids = array(), $conditions = array()) { return entity_load('taxonomy_term', $tids, $conditions); }
  • 15. Drupal 7: Vieles ist schon da, aber... Vieles ist noch unvollständig: - Remember CRUD? Im Core gibt es nur das "R" ! - Es gibt noch kein User Interface zu Entities - Integration z.B. in Views, Panels, Rules, Features, Pathauto, Display Suite, ... Manches ist nur vorläufig: function taxonomy_get_term_by_name($name) { return taxonomy_term_load_multiple(array(), array('name' => trim($name))); } function taxonomy_term_load_multiple($tids = array(), $conditions = array()) { return entity_load('taxonomy_term', $tids, $conditions); } Die Verwendung von $conditions ist deprecated, und wird in D8 wahrscheinlich wieder verschwinden!
  • 16. Abhilfe: Contributed Modules Entity API - Ergänzt das Core API, z.B. mit vollständigem CRUD http://drupal.org/project/entity File Entity - Macht Files fieldable http://drupal.org/project/file_entity Entity Construction Kit (ECK) - User Interface zur Verwaltung von Entities http://drupal.org/project/eck Entity Reference - Referenzen auf Entities, ähnlich node / user reference http://drupal.org/project/entityreference Manches wird mit D8 im Core landen, anderes in der Versenkung verschwinden - schwer zu beurteilen!
  • 17. Demo: ECK, Views, DB - Erzeugen von Entity Types, Bundles, Entities - Listen in Views - Repräsentation in der Datenbank
  • 18. Fragen / Diskussion Zum Weiterlesen: http://www.davetech.com/blog/introduction_drupal_7_entities_and_fields http://www.istos.it/category/blog-tags/drupal-entities http://de.wetena.com/blog/2011/04/13/entities-in-drupal-7 http://www.slideshare.net/alexshr/entity-api Diese Folien als PDF: http://www.slideshare.net/drubb/drupal-entities