Drupal Block Entities - Drupalcamp Chicago 2011

1,425
-1

Published on

Published in: Technology, Self Improvement
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,425
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • Beans are Block Entities.\nWhat does BEAN stand for.\n
  • Beans are Block Entities.\nWhat does BEAN stand for.\n
  • Beans are Block Entities.\nWhat does BEAN stand for.\n
  • Before we talk about what beans really are, let’s talk about why we created beans.\n
  • This site was going to have hundreds of blocks, which needed to be created by normal CMS users. How do we allow users to create blocks without granting them administrative access?\n
  • After evaluating the designs, we came up with 2 main types of user generated blocks.\n
  • Here we see 2 “Graphic” blocks. If these were nodes, we would immediately know how to build these. A title, an image field with a link, some body text. Then we style the template, or perhaps if the template isn’t too complicated, we just select the appropriate field formatters for this view mode. But these aren’t nodes, they’re blocks.\n
  • Here is are the listing blocks.\nThat’s what let us to create Beans\nThe “BLOG” bean we will be creating later\n
  • That’s what led us to create Beans\n
  • Ctypes but with Blocks\nBlock types are ctools based plugins\nBlock types are entity bundles\nEntity API module adds views, actions and rules integration\n\n
  • Bean exposes blocks as a new type of fieldable entity.  Creating new block types and adding fields is as easy as creating new node types (screenshot of block type admin).  Use the display settings for the blocks to control the display output, just like with other fieldable entities (screenshot of “display fields” tab).\n\n
  • \n
  • API First\n- Early versions of BEAN had no way of creating block types in the UI\nEvery plugin is a type\nBlock Placement\n- Config if context/block admin\n- Content if block reference/panels\n
  • API First\n- Early versions of BEAN had no way of creating block types in the UI\nEvery plugin is a type\nBlock Placement\n- Config if context/block admin\n- Content if block reference/panels\n
  • API First\n- Early versions of BEAN had no way of creating block types in the UI\nEvery plugin is a type\nBlock Placement\n- Config if context/block admin\n- Content if block reference/panels\n
  • Let’s make a view Block for a listing of blog post. There are 3 different view modes for each node listing. The user needs to be able to select how many of each type of view mode will be used. The user will also select content filter settings.\n
  • All modules just implement hook_bean_types_api_info\nhook_ctools_plugin_direcory is optional but recommended.\n
  • Define a Ctools plugin\nIf you are doing a bean with only fields, you can use bean_default as the “class” with no need for “file” or “path”\n
  • \n
  • \n
  • Notice the groups of settings. These help make logical breaks.\nFilters for Content\nView settings per view mode\nMore link for entire block\n
  • Notice the groups of settings. These help make logical breaks.\nFilters for Content\nView settings per view mode\nMore link for entire block\n
  • Notice the groups of settings. These help make logical breaks.\nFilters for Content\nView settings per view mode\nMore link for entire block\n
  • This is just a sample. Just uses standard Form API\n$bean is fully loaded with defaults so no need to check for existence\nTalk about the $bean->more_link[‘text’].\n
  • Common to use Entity Field Query to create dataset.\n$bean is fully loaded\n$content is the rendered content array up to now. Includes fields\nView mode is not used yet - Oh wait, as of yesterday they are!!\n\n
  • EntityFieldQuery is awesome\nEnergyEntityFieldQuery\n- Defaults Group\n- Published\n- Easily handle topic vocab\n
  • Using the theme function “energy_blog_list”\nnodes\nmore_link\nitems_per_page passed\n
  • Using the theme function “energy_blog_list”\nnodes\nmore_link\nitems_per_page passed\n
  • Using the theme function “energy_blog_list”\nnodes\nmore_link\nitems_per_page passed\n
  • Uses view modes to render the article nodes per the 3 different settings groups\nTalk block placement\n\n
  • Here’s a quick recipe that we used a lot on the site. It is very easy to implement and your users will love it.\n
  • \n
  • Drupal Block Entities - Drupalcamp Chicago 2011

    1. 1. Beans, Beans - They’reGood For Your... EditorsDrupalcamp Chicago 2011December 10th, 2011Neil Hastings
    2. 2. BEANSNeil Hastings‣ Senior Engineering‣ Treehouse Agency‣ @indytechcook Drupal Camp Indy 2011, Neil Hastings
    3. 3. BEANSWhat is a BEAN? Drupacamp Chicago 2011, Neil Hastings
    4. 4. BEANSWhat is a BEAN?‣ Block Entity Drupacamp Chicago 2011, Neil Hastings
    5. 5. BEANSWhat is a BEAN?‣ Block Entity‣ BEAN Entities Aren’t Nodes ‣ BEAN Entities Aren’t Nodes ‣ BEAN Entities Aren’t Nodes ‣ BEAN Entities Aren’t Nodes ‣ BEAN Entities Aren’t Nodes Drupacamp Chicago 2011, Neil Hastings
    6. 6. BEANSWhat is a BEAN?‣ Block Entity‣ BEAN Entities Aren’t Nodes ‣ BEAN Entities Aren’t Nodes ‣ BEAN Entities Aren’t Nodes ‣ BEAN Entities Aren’t Nodes ‣ BEAN Entities Aren’t Nodes‣ A Healthy Way for users to Create Blocks Drupacamp Chicago 2011, Neil Hastings
    7. 7. Blocks on Energy.gov
    8. 8. BEANSBlock Creator !=Administrator Drupacamp Chicago 2011, Neil Hastings
    9. 9. BEANSUser generated Blocks‣ Node-like blocks ‣ Fields ‣ Templates‣ Listing blocks ‣ User-defined filters ‣ Pre-defined styles Drupacamp Chicago 2011, Neil Hastings
    10. 10. screenshot of listingblock example, withhighlights like previousslide
    11. 11. screenshot of listingblock example, withhighlights like previousslide
    12. 12. BEANSBlock Entities‣ Block types‣ Fieldable‣ Entity API‣ Non-admin permissions‣ Data entry is familiar to users‣ Build is familiar to Site Builders Drupacamp Chicago 2011, Neil Hastings
    13. 13. BEANSCoding Concepts Drupacamp Chicago 2011, Neil Hastings
    14. 14. BEANSCoding Concepts‣ API First Drupacamp Chicago 2011, Neil Hastings
    15. 15. BEANSCoding Concepts‣ API First‣ Every Type is a Plugin ‣ Even the UI types Drupacamp Chicago 2011, Neil Hastings
    16. 16. BEANSCoding Concepts‣ API First‣ Every Type is a Plugin ‣ Even the UI types‣ Two levels of configuration storage ‣ “Config” - BEAN Type/Bundle ‣ “Content” - BEAN/Entity ‣ Block Placement - BOTH! Drupacamp Chicago 2011, Neil Hastings
    17. 17. BEANSDrupacamp Chicago 2011, Neil Hastings
    18. 18. BEANSSHOW ME SOME CODE ALREADY Drupacamp Chicago 2011, Neil Hastings
    19. 19. BEANSCode For All‣ META Drupacamp Chicago 2011, Neil Hastings
    20. 20. BEANSShow me the Plugin Drupacamp Chicago 2011, Neil Hastings
    21. 21. BEANSWe all love Classes‣ Extend the base class. ‣ bean_plugin ‣ bean_custom - If bean type will be editable in the UI‣ Implement Methods ‣ Values - Define settings to be saved against the Bean. ‣ Form - Define Form for bean add/edit ‣ View - Render the bean. Drupacamp Chicago 2011, Neil Hastings
    22. 22. BEANSDefine Classclass blog_listing_bean extends bean_plugin {..} Drupacamp Chicago 2011, Neil Hastings
    23. 23. BEANSDefine Valuespublic function values() { return array( filters => array( term => FALSE, topic => FALSE, audience => FALSE, ), items_per_page => array( large_image => 0, small_image => 0, listing => 0, ), more_link => array( text => , path => , ), );} Drupacamp Chicago 2011, Neil Hastings
    24. 24. BEANSDefine Valuespublic function values() { return array( filters => array( term => FALSE, topic => FALSE, audience => FALSE, ), items_per_page => array( large_image => 0, small_image => 0, listing => 0, ), more_link => array( text => , path => , ), );} Drupacamp Chicago 2011, Neil Hastings
    25. 25. BEANSDefine Valuespublic function values() { return array( filters => array( term => FALSE, topic => FALSE, audience => FALSE, ), items_per_page => array( large_image => 0, small_image => 0, listing => 0, ), more_link => array( text => , path => , ), );} Drupacamp Chicago 2011, Neil Hastings
    26. 26. BEANSDefine Valuespublic function values() { return array( filters => array( term => FALSE, topic => FALSE, audience => FALSE, ), items_per_page => array( large_image => 0, small_image => 0, listing => 0, ), more_link => array( text => , path => , ), );} Drupacamp Chicago 2011, Neil Hastings
    27. 27. BEANSForm for Allpublic function form($bean) { $form[more_link][text] = array( #type => textfield, #title => t(Link text), #default_value => $bean->more_link[text], ); $form[more_link][path] = array( #type => textfield, #title => t(Link path), #default_value => $bean->more_link[path], );} Drupacamp Chicago 2011, Neil Hastings
    28. 28. BEANSForm for Allpublic function form($bean) { $form[more_link][text] = array( #type => textfield, #title => t(Link text), #default_value => $bean->more_link[text], ); $form[more_link][path] = array( #type => textfield, #title => t(Link path), #default_value => $bean->more_link[path], );} Drupacamp Chicago 2011, Neil Hastings
    29. 29. BEANSA Bean with a Viewpublic function view($bean, $content, $view_mode =full, $langcode = NULL) {} ‣ Return a Render Array Drupacamp Chicago 2011, Neil Hastings
    30. 30. BEANS $count = $bean->items_per_page[large_image] + $bean->items_per_page[small_image] + $bean->items_per_page[listing]; $query = new EnergyEntityFieldQuery(); $query ->entityCondition(bundle, article) ->clearAudienceConditions() ->setOtherAudienceCondition() ->range(0, $count); if (!empty($bean->filters[term])) { $query->setTermCondition(field_article_type, $bean->filters[term]); } if (!empty($bean->filters[topic])) { $query->setTopicCondition($bean->filters[topic]); } if (!empty($bean->filters[audience])) { $query->setTermCondition(field_audience_term, $bean->filters[audience]); } $result = $query->execute(); Drupacamp Chicago 2011, Neil Hastings
    31. 31. BEANSif (empty($result)) { $content[nodes] = array();}else { $content[nodes] = node_load_multiple(array_keys($result[node]));}$content[#theme] = energy_blog_list;$content[more_link] = array( text => $bean->more_link[text], path => $bean->more_link[path],);$content[items_per_page] = array( large_image => $bean->items_per_page[large_image], small_image => $bean->items_per_page[small_image], listing => $bean->items_per_page[listing],); Drupacamp Chicago 2011, Neil Hastings
    32. 32. BEANSif (empty($result)) { $content[nodes] = array();}else { $content[nodes] = node_load_multiple(array_keys($result[node]));}$content[#theme] = energy_blog_list;$content[more_link] = array( text => $bean->more_link[text], path => $bean->more_link[path],);$content[items_per_page] = array( large_image => $bean->items_per_page[large_image], small_image => $bean->items_per_page[small_image], listing => $bean->items_per_page[listing],); Drupacamp Chicago 2011, Neil Hastings
    33. 33. BEANSif (empty($result)) { $content[nodes] = array();}else { $content[nodes] = node_load_multiple(array_keys($result[node]));}$content[#theme] = energy_blog_list;$content[more_link] = array( text => $bean->more_link[text], path => $bean->more_link[path],);$content[items_per_page] = array( large_image => $bean->items_per_page[large_image], small_image => $bean->items_per_page[small_image], listing => $bean->items_per_page[listing],); Drupacamp Chicago 2011, Neil Hastings
    34. 34. BEANSif (empty($result)) { $content[nodes] = array();}else { $content[nodes] = node_load_multiple(array_keys($result[node]));}$content[#theme] = energy_blog_list;$content[more_link] = array( text => $bean->more_link[text], path => $bean->more_link[path],);$content[items_per_page] = array( large_image => $bean->items_per_page[large_image], small_image => $bean->items_per_page[small_image], listing => $bean->items_per_page[listing],); Drupacamp Chicago 2011, Neil Hastings
    35. 35. screenshot of listingblock example, withhighlights like previousslide
    36. 36. screenshot of listingblock example, withhighlights like previousslide
    37. 37. BEANSEditorial listings‣ Hand-selected listings of nodes‣ Multiple Node Reference field‣ View mode set in the Node Reference field settings‣ Additional fields ‣ More link, Header text, etc. Drupacamp Chicago 2011, Neil Hastings
    38. 38. BEANSMore Fun!‣ http://treehouseagency.com/blog/ neil-hastings/2011/09/21/ building-custom-block-entities- beans‣ http://treehouseagency.com/blog/ neil-hastings/2011/09/06/ building-energygov-without-views‣ http://drupal.org/project/bean Drupacamp Chicago 2011, Neil Hastings

    ×