Field APIPractical usage
PresenterPavel MakhrinskySenior Drupal developerBerlingske Media
Introducing Field API● CCK module successor● The way to store and represent Entities  properties● Utilize Form API mechanism
Some terminologyEntitiesField TypesField StorageField InstancesBundles
Some terminology: structure
Field APIField Types APIField Info APIField CRUD APIField Storage APIField API bulk data deletionField Language API
Practical tips   How to use
Implement formatterCreate a presentation of term_reference as acomma-delimited items1. hook_field_formatter_info()2. hook_...
Implement formatter: infofunction smth_field_formatter_info() {  return array(    taxonomy_comma_links => array(      labe...
Implement formatter: viewfunction smth_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $...
Implement widget: viewhook_field_widget_info()hook_field_widget_form()hook_field_is_empty()
Implement widget: infofunction textf_field_widget_info() {  return array(    textf => array(      label => t(Text field), ...
Implement widget: formfunction textf_field_widget_form(&$form, &$form_state, $field, $instance, $langcode,$items, $delta, ...
Language API
Language handling: MultilingualtypesLocalizedSingle language siteMultilingual siteSite with different content for differen...
Language handling
Language handlingLocale module is disabled1.   $entity->body[und][0][value];2.   $langcode = entity_language(entity_type, ...
Language handlingLocale and Content translation modules enabled 1.   $entity->body[und][0][value]; 2.   $langcode = entity...
Language handlingLocale and Entity translation modules enabledShared fields1. $entity->body[und][0][value];2. $langcode = ...
Getting field datafunction field_get_items($entity_type, $entity, $field_name, $langcode = NULL)function field_view_field(...
Entity API: metadata 1.   // hook_entity_property_info_alter 2.   class InviteMetadataController extends EntityDefaultMeta...
Entity API: metadata 1.   $invite = entity_metadata_wrapper(invite, $entity); 2. 3.   // Get the value of field_name of th...
Update a field without Entity$node = node_load($nid);$node->field_fieldname[LANGUAGE_NONE][0][value] = value;node_save($no...
Add AJAX validation to a specificfieldfunction smth_link_form_alter(&$form, &$form_state, $form_id) {  if (example_node_fo...
Add AJAX validation to a specificfieldfunction kf_link_ajax_callback(&$form, $form_state) {  $values = $form_state[values]...
Useful linkshttp://drupal.org/node/443536http://drupal.org/project/entityhttp://api.drupal.org/api/drupal/modules%21field%...
Questions?Pavel Makhrinskydrupal.org: http://drupal.org/user/773216facebook: https://www.facebook.com/gumanist
Upcoming SlideShare
Loading in …5
×

Drupal Field API. Practical usage

4,699 views

Published on

Practical usage tips for Drupal's Field API

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,699
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
17
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Drupal Field API. Practical usage

  1. 1. Field APIPractical usage
  2. 2. PresenterPavel MakhrinskySenior Drupal developerBerlingske Media
  3. 3. Introducing Field API● CCK module successor● The way to store and represent Entities properties● Utilize Form API mechanism
  4. 4. Some terminologyEntitiesField TypesField StorageField InstancesBundles
  5. 5. Some terminology: structure
  6. 6. Field APIField Types APIField Info APIField CRUD APIField Storage APIField API bulk data deletionField Language API
  7. 7. Practical tips How to use
  8. 8. Implement formatterCreate a presentation of term_reference as acomma-delimited items1. hook_field_formatter_info()2. hook_field_formatter_view()(option)3. hook_field_formatter_prepare_view()
  9. 9. Implement formatter: infofunction smth_field_formatter_info() { return array( taxonomy_comma_links => array( label => t(Comma-links), field types => array(taxonomy_term_reference), ), );}
  10. 10. Implement formatter: viewfunction smth_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display){ $element = array(); switch ($display[type]) { case taxonomy_comma_links: { $links = array(); foreach ($items as $delta => $item) { $term = taxonomy_term_load($item[tid]); $uri = entity_uri(taxonomy_term, $term); $links[] = l($term->name, $uri[path]); } $element[] = array( #markup => implode(, , $links), ); break; } } return $element;}
  11. 11. Implement widget: viewhook_field_widget_info()hook_field_widget_form()hook_field_is_empty()
  12. 12. Implement widget: infofunction textf_field_widget_info() { return array( textf => array( label => t(Text field), field types => array(text), settings => array(size => 60), behaviors => array( multiple values => FIELD_BEHAVIOR_DEFAULT, default value => FIELD_BEHAVIOR_DEFAULT, ), ), );}
  13. 13. Implement widget: formfunction textf_field_widget_form(&$form, &$form_state, $field, $instance, $langcode,$items, $delta, $element) { switch ($instance[widget][type]) { case textf: $element[textf] = array( #type => textfield, #title => $element[#title], #description => $element[#description], #default_value => isset($items[$delta][value]) ? $items[$delta][value] : NULL, #required => $element[#required], #weight => isset($element[#weight]) ? $element[#weight] : 0, #delta => $delta, ); break; } return $element;
  14. 14. Language API
  15. 15. Language handling: MultilingualtypesLocalizedSingle language siteMultilingual siteSite with different content for different languagesMultilingual site with translationSite with translated content
  16. 16. Language handling
  17. 17. Language handlingLocale module is disabled1. $entity->body[und][0][value];2. $langcode = entity_language(entity_type, $entity);3. $langcode == und
  18. 18. Language handlingLocale and Content translation modules enabled 1. $entity->body[und][0][value]; 2. $langcode = entity_language(entity_type, $entity); 3. $langcode == en
  19. 19. Language handlingLocale and Entity translation modules enabledShared fields1. $entity->body[und][0][value];2. $langcode = entity_language(entity_type, $entity);3. $langcode == enTranslatable fields1. $entity->body[en][0][value];2. $entity->body[de][0][value];3. $langcode = entity_language(entity_type, $entity);4. $langcode == en
  20. 20. Getting field datafunction field_get_items($entity_type, $entity, $field_name, $langcode = NULL)function field_view_field($entity_type, $entity, $field_name, $display = array(), $langcode = NULL)function field_view_value($entity_type, $entity, $field_name, $item, $display = array(), $langcode = NULL)
  21. 21. Entity API: metadata 1. // hook_entity_property_info_alter 2. class InviteMetadataController extends EntityDefaultMetadataController { 3. public function entityPropertyInfo() { 4. $info = parent::entityPropertyInfo(); 5. $properties = &$info[$this->type][properties]; 6. 7. $properties[inviter] = array( 8. label => t(Inviter), 9. type => user,10. getter callback => entity_property_getter_method,11. setter callback => entity_property_setter_method,12. schema field => uid,13. required => TRUE,14. );15.16. $properties[invite_accept_link] = array(17. label => t(Invite action link: accept),18. getter callback => invite_metadata_entity_get_properties,19. type => uri,20. computed => TRUE,21. entity views field => TRUE,22. );23.24. return $info;25. }
  22. 22. Entity API: metadata 1. $invite = entity_metadata_wrapper(invite, $entity); 2. 3. // Get the value of field_name of the inviter profile. 4. $invite ->inviter->profile->field_name->value(); 5. $invite ->inviter->profile->field_name->set(New name); 6. 7. // Value of the invite summary in german language. 8. $invite ->language(de)->body->summary->value(); 9.10. // Check whether we can edit inviter email address.11. $invite ->inviter->mail->access(edit) ? TRUE : FALSE;12.13. // Get roles of inviter.14. $invite ->inviter->roles->optionsList();15.16. // Set description of the first file in field field_files.17. $invite ->field_files[0]->description = The first file;18. $invite ->save();19.20. // Get invite object.21. $invite = $invite->value();
  23. 23. Update a field without Entity$node = node_load($nid);$node->field_fieldname[LANGUAGE_NONE][0][value] = value;node_save($node);$node = node_load($nid);$node->field_fieldname[LANGUAGE_NONE][0][value] = value;field_attach_update(node, $node);Note:- be careful with security- be careful with caching
  24. 24. Add AJAX validation to a specificfieldfunction smth_link_form_alter(&$form, &$form_state, $form_id) { if (example_node_form == $form_id) { $form[field_link][$language][0][#process] =array(link_field_process,_smth_link_field_link_process); }}function _smth_link_field_link_process($element, &$form_state, $form) { $element[url][#description] = <div id="example-link"></div>; $element[url][#ajax] = array( callback => smth_link_ajax_callback, wrapper => example-link, ); return $element;}
  25. 25. Add AJAX validation to a specificfieldfunction kf_link_ajax_callback(&$form, $form_state) { $values = $form_state[values]; $field_link = $values[field_link]; $language = $values[language]; $url = $field_link[$language][0][url]; $duplicate_nodes = _kf_link_get_url_nid($url); foreach ($duplicate_nodes as $duplicate_node) { if (isset($duplicate_node->nid) && ($duplicate_node->nid !=$values[nid])) { drupal_set_message(t(This URL already exists in <a href="!url">!title</a>, array(!title => $duplicate_node->title, !url =>"node/{$duplicate_node->nid}")), error); } } $commands = array(); $commands[] = ajax_command_html(NULL, theme(status_messages)); return array( #type => ajax, #commands => $commands, );}
  26. 26. Useful linkshttp://drupal.org/node/443536http://drupal.org/project/entityhttp://api.drupal.org/api/drupal/modules%21field%21field.module/group/field/7http://drupal.org/project/exampleshttp://drupal.org/project/edithttp://drupal.org/project/layout
  27. 27. Questions?Pavel Makhrinskydrupal.org: http://drupal.org/user/773216facebook: https://www.facebook.com/gumanist

×