views

Fun with entities and Field UI: Views displays

Drupal 7 introduced entities. It's such a great concept, I'm able to easily port Views displays - part of Display suite - to Drupal 7. It is now part of the main package, bundled in a sub module called Display suite Extras, which also includes the 'region to block' and 'switch view mode on a per node basis' features. More goodies, less code, even more power. But back to Views, entities and overriding templates.

For those who don't know exactly what I'm talking about, a quick summary. In the Drupal 6 version of Views displays, you're able to select a single display of a view and rearrange the template variables so you don't have to override the views-view.tpl.php template file. It uses the drag and drop screen that Display suite comes with, but for D7 we decided to use the manage display screens that Field UI provides. This is great because it supports all entities, so any entity that is fieldable use the same forms in which we simply inject our extra regions. However, Views is not an entity, but in comes hook_entity_info to the rescue!

To make sure we can use the Field UI forms, we declare our own entity called 'ds_views', which is not fieldable and only has one default bundle.

<?php
/**
* Implements hook_entity_info().
*/
function ds_extras_entity_info() {

 
// Register a views entity on behalf of Views.
 
$return = array(
   
'ds_views' => array(
     
'label' => t('Display suite Views'),
     
'bundles' => array(),
    ),
  );
  return
$return;
}
?>

So now that we have an entity, we create our own menu callback function which will ask for the field ui form. Display Suite will call all additional fields and add the layout options too.

<?php
function ds_extras_vd_field_ui() {
 
module_load_include('inc', 'field_ui', 'field_ui.admin');
 
$build = drupal_get_form('field_ui_display_overview_form', 'ds_views', 'views', 'default');
  return
$build;
}
?>

However, when calling the Field UI form, we'll get a message saying this entity does not have not any fields. To solve that problem, we can implement hook_field_extra_fields and return one field. In this case, the title will be overridden later on because we also implement hook_ds_fields() which will return our fields for views. 

<?php
/**
* Implements hook_field_extra_fields().
*/
function ds_extras_field_extra_fields() {

 
// Register a single field so fields for vd be picked
  // up nicely in the display overview form.
 
$extra['ds_views']['views'] = array(
   
'display' => array(
     
'title' => array(
       
'label' => t('Title'),
       
'description' => t('Title'),
       
'weight' => 10,
      ),
    ),
  );

  return
$extra;
}
?>

In the screenshot you see a couple template variables which you can now put into the regions of the chosen layout.

Impressive isn't ? Hacky ? Well .. maybe a little bit. There is even more code, because now the combination of a view and a display is recorded as a bundle within the entity I just created above. I've recorded a short screencast so you can see it in action. If you want to try it yourself, download the second alpha release of Display suite for D7 and have fun!

Introducing the Node displays module

In a previous post, I talked about pimping the CCK ui. In that post a first patch was made available for CCK, however after a long debate, we decided to commit our code as a separate module on d.o, available at http://drupal.org/project/nd. After a very short development cycle, we're proud to release the first public beta version. Features included are:

  • Separated the UI for fields and displays: the order of fields isn't done anymore on the fields overview screen, they are now available on the display screen for every build mode. If you want control over the node edit form, I recommend the excellent nodeform columns module.
  • Node regions: every field can be dropped in its own region.
  • Custom build modes: that's right, create as many build modes you like via the interface or via hook_nd_build_modes().
  • Views row plugin: create lists and use one of the standard or custom build modes.
  • hook_nd_extra_display_fields(): define custom fields. The module itself defines title, author, links, body and taxonomy so far. You can implement your own hook for more fields.
  • Exclude build modes from rendering and use the node template instead.
  • Override core search results page to use node display rendering.

As you can see, lots of fine goodies, but a screencast will explain more than a thousands words. Look at this video to discover the power of this module. We also would like to invite you all to test the beta version and submit bugs, features and/or patches. Happy Node Build Mode building!

Subscribe to RSS - views

You are here