Spend More Time Surfing: Custom Entities & Entity Tools

Spend More Time Surfing: Custom Entities & Entity Tools

06d59b7a09c0085d85d0a42638e0db01?s=128

Mark Dorison

March 28, 2015
Tweet

Transcript

  1. Spend More Time Surfing Custom Entities and Entity Tools

  2. Mark Dorison @markdorison

  3. Spend More Time SURFING

  4. Spend More Time READING

  5. Spend More Time WRITING

  6. Spend More Time SLEEPING

  7. None
  8. None
  9. A long time ago…

  10. Drupal 6: “Stuff”

  11. nodecomment https://www.drupal.org/project/nodecomment

  12. profile

  13. Drupal 7 Entities

  14. Custom Entities

  15. Why Bother?

  16. OOP

  17. None
  18. More Art Then Science

  19. Reservations

  20. hook_entity_info()

  21. /** * Implements hook_entity_info(). */ function gc_reservation_entity_info() { return array(

    'gc_reservation' => array( 'label' => t('Reservation'), 'plural label' => t('Reservations'), 'entity class' => 'GCReservation', 'controller class' => 'GCReservationController', 'base table' => 'gc_reservation', 'entity cache' => TRUE, 'entity keys' => array( 'id' => 'id', 'label' => 'id', ), 'label callback' => 'entity_class_label', 'uri callback' => 'gc_reservation_reservation_uri', 'fieldable' => TRUE, 'bundles' => array( 'gc_reservation' => array( 'label' => t('Reservation'), 'admin' => array( 'path' => 'admin/structure/reservations', 'access arguments' => array('administer reservation settings'), ), ), ), 'access callback' => 'gc_reservation_reservation_access', 'module' => 'gc_reservation', 'admin ui' => array( 'path' => 'admin/content/reservations', 'file' => 'gc_reservation.admin.inc', 'controller class' => 'GCReservationUIController', ), ), ); }
  22. hook_entity_info() label access_callback admin_ui bundles base_table entity_class label_callback

  23. Add Methods

  24. class GCReservation extends Entity { }

  25. class GCReservation extends GCChargeableEntity { }

  26. class GCReservation extends GCChargeableEntity implements GCCreditable { }

  27. Performance

  28. base_table

  29. Entity Tools

  30. EntityFieldQuery

  31. db_query

  32. $ids = db_query("SELECT id FROM {gc_reservation} WHERE type = 'booking'")

    ->fetchCol();
  33. db_select

  34. $query = db_select('gc_reservation', 'gcr') ->fields('gcr', array('id')) ->condition('gcr.created', $created_limit, '>') ->condition('gcr.status',

    1, '='); $query->innerJoin('field_reservation_type', 'rt', 'gcr.id = frt.entity_id'); $query->condition('frt.field_reservation_type_value', 'booking', '='); $result = $query->execute();
  35. EntityFieldQuery

  36. $query = new EntityFieldQuery(); $results = $query->entityCondition('entity_type', 'gc_reservation') ->fieldCondition('field_reservation_type', 'value',

    'booking') ->propertyCondition('created', $created_limit, '<') ->propertyCondition('status', 1) ->addMetaData('account', user_load(1)); ->execute();
  37. $query = new EntityFieldQuery(); $results = $query->entityCondition('entity_type', 'gc_reservation') ->fieldCondition('field_reservation_type', 'value',

    'booking') ->propertyCondition('created', $created_limit, '<') ->propertyCondition('status', 1) ->addMetaData('account', user_load(1)); ->execute();
  38. $query = new EntityFieldQuery(); $results = $query->entityCondition('entity_type', 'gc_reservation') ->fieldCondition('field_reservation_type', 'value',

    'booking') ->propertyCondition('created', $created_limit, '<') ->propertyCondition('status', 1) ->addMetaData('account', user_load(1)); ->execute();
  39. $query = new EntityFieldQuery(); $results = $query->entityCondition('entity_type', 'gc_reservation') ->fieldCondition('field_reservation_type', 'value',

    'booking') ->propertyCondition('created', $created_limit, '<') ->propertyCondition('status', 1) ->execute();
  40. EntityMetaDataWrapper

  41. EMW

  42. EMW Querying

  43. if (!empty($node->field_event_photo_ref[$node->language][0]['target_id'])) { if ($imgrefnode = node_load($node->field_event_photo_ref[$node->language][0]['target_id'])) { // DO

    SOME STUFF. } }
  44. $recipe_wrapper = entity_metadata_wrapper('node', $node); if ($image_node = $recipe_wrapper->field_ref_image->value()) { //

    DO SOME STUFF! }
  45. $entity = $entity_wrapper->value();

  46. $ref_entity = $entity_wrapper->field_ref->value();

  47. $ref_wrapper = $entity_wrapper->field_ref();

  48. $id = $entity_wrapper->getIdentifier();

  49. $label = $entity_wrapper->label();

  50. EMW Querying getIdentifier() getBundle() label() entityKey() value()

  51. EMW Mutation

  52. $entity_wrapper->status->set(0);

  53. $entity_wrapper->field_collected->set(TRUE);

  54. $entity_wrapper->field_img_ref->set($image_node);

  55. Performance

  56. Developer Experience (DX)

  57. Hardware is Cheap

  58. Recap

  59. None
  60. Mark Dorison @markdorison mark@chromatic.is