Marc Ingram Entity Field Query API Examiner.com http://drupal.org/user/77320
The need for a query builder Where we are now Drupal 6 has no concept of a query builder http://buytaert.net/starting-to-work-on-drupal-7 Feb 2008 Every query must be hand crafted for a specific database
View offers the ability to create custom queries on the fly View 3 allows for plugable backends
Views based queries are not always optimal D7 changes this
Views in core Why for developers? No GUI
Lacks the structured tpl structure that views provides
Need to be able to write code Why is this possible? Fields in core
Plugable storage
Fields in Core What does fields in core mean? Bundles/Entities This is the basic building block Taxonomy terms, Taxonomy vocabularies, Users, Nodes, Files You can build your own Fields Allows you define fields that can be used by entities Instances Associates fields with given entities and bundles
Entities and bundles hook_entity_info() Allows you to create new entities
Example of a node entity
hook_entity_info_alter() Bundle is a sub class of an entity CCK content type
Taxonomy terms are classified by vocabularies
Bundles can have there own properties
Fields All entities are now fieldable This means the concept of cck is no longer restricted to nodes Core comes with predefined field types Text, Options, Number, List CCK minus user and node reference these are still contrib

Entity Query API

  • 1.
    Marc Ingram EntityField Query API Examiner.com http://drupal.org/user/77320
  • 2.
    The need fora query builder Where we are now Drupal 6 has no concept of a query builder http://buytaert.net/starting-to-work-on-drupal-7 Feb 2008 Every query must be hand crafted for a specific database
  • 3.
    View offers theability to create custom queries on the fly View 3 allows for plugable backends
  • 4.
    Views based queriesare not always optimal D7 changes this
  • 5.
    Views in coreWhy for developers? No GUI
  • 6.
    Lacks the structuredtpl structure that views provides
  • 7.
    Need to beable to write code Why is this possible? Fields in core
  • 8.
  • 9.
    Fields in CoreWhat does fields in core mean? Bundles/Entities This is the basic building block Taxonomy terms, Taxonomy vocabularies, Users, Nodes, Files You can build your own Fields Allows you define fields that can be used by entities Instances Associates fields with given entities and bundles
  • 10.
    Entities and bundleshook_entity_info() Allows you to create new entities
  • 11.
    Example of anode entity
  • 12.
    hook_entity_info_alter() Bundle isa sub class of an entity CCK content type
  • 13.
    Taxonomy terms areclassified by vocabularies
  • 14.
    Bundles can havethere own properties
  • 15.
    Fields All entitiesare now fieldable This means the concept of cck is no longer restricted to nodes Core comes with predefined field types Text, Options, Number, List CCK minus user and node reference these are still contrib
  • 16.
  • 17.
    Creating a field$vocabulary = (object) array('name' => 'Newsworthy subject', 'machine_name' => 'newsworthy_subject'); taxonomy_vocabulary_save($vocabulary); $vocabularies = taxonomy_vocabulary_get_names(); $field = array( 'field_name' => 'newsworthy_subject', 'type' => 'taxonomy_term_reference', 'settings' => array( 'required' => FALSE, 'allowed_values' => array( array( 'vid' => $vocabularies['newsworthy_subject']->vid, 'parent' => 0, ), ), ), ); field_create_field($field);
  • 18.
    Bundles A bundleis a collection of fields associated with an entity Sub types can have different bundles – story vs business
  • 19.
    Widgets can bedefined here along with display modes Each field can have a different widget on a different bundle field_update_instance
  • 20.
  • 21.
    Updating a fieldforeach (ex_taxonomy_get_content_types() as $content_type) { $content_type = node_type_set_defaults($content_type); node_type_save($content_type); // just for good measure - it won't do anything if the field is // already present node_add_body_field($content_type); // set the weight of the body instances. $body_instance = field_info_instance('node', 'body', $content_type->type); $body_instance['required'] = TRUE; $body_instance['widget']['weight'] = 1.2; $body_instance['display']['full'] = array( 'label' => 'hidden', 'type' => 'text_default', ); field_update_instance($body_instance); }
  • 22.
    Entity field apiEntity query api The ability to create queries that are agnostic with regards field storage engines
  • 23.
    What does thatmean? What is field storage?
  • 24.
    Drupal 7 wedon't need to store fields in just mysql Main choices at the moment sql_storage (core)
  • 25.
    Mongodb and activelybeing used by examiner.com Will allow for sites to select the most appropriate storage mechanism but for scalability nosql is likely to become the weapon of choice The same query will work over multiple storage engines
  • 26.
    Entity query apiWhat does a entity field query look like
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
    Examples How toroll your own hook_field_storage_query() Now for some code An example query Benefits of entity_cache and memcache Native queries Mysql
  • 32.
  • 33.
    No Kittens wereharmed Didn't we mention portable queries..... A demo and code walkthrough in which no kittens are harmed!!! Mysql vs mongodb spot the difference
  • 34.
    Some basic mongocli queries
  • 35.
    The joy oftheming!! Why D7 has multiple options for storage Not all sites have the same need
  • 36.
    The nature ofd7 storage in mysql is problematic for scalability
  • 37.
    New api allowsfor backends to swap out with little rework in the drupal layer
  • 38.
    Views in core??Entity query api Negatives Lacks the flexibility of views
  • 39.
    Lack the templatingstructure of view
  • 40.
    Has no conceptof relationships But.... Database agnostic
  • 41.
    Supports all queriesin views that do need relationships
  • 42.
  • 43.
    Renderable arrays maketheming a joy anyway!!!
  • 44.
    Thank you Anyquestions?