background preloader

CCK, entities

Facebook Twitter

Entity metadata wrappers. The contributed Entity API module provides wrapper classes that make dealing with the values of an entity's properties and fields easier. Wrappers make it easier to get and set the values of fields and properties as well as to programmatically retrieve additional information about these elements and iterate over lists of values in a consistent manner. For example consider the following method of accessing the value of a field attached to a node. This is a pattern that we see used often when working with entities in Drupal. However there are a couple of things that make this less than ideal. <? Using metadata wrappers from the entity module we can access this information like so: <? How about an example of making things consistent?

<? // Unified way of getting $node->nid, $user->uid, ... // Unified way of getting $node->type, ... Examples For making use of this information (metadata) the module provides some wrapper classes which ease getting and setting values. <? <? <? Or <? <? <? <? <? <? <? <? <? <? <? <? <? Performance - Which is more performant: entity_metadata_wrapper or field_get_items? The short answer: field_get_items() is more performant than entity_metadata_wrapper(). Check out the code for these functions: Both require that you pass along the entity, which has already been loaded from the database . For example: $node = node_load ( 123 ); $items = field_get_items ( 'node' , $node , 'field_my_field_name' ); print $items [ 0 ][ 'value' ]; or, as you've already suggested: $wrapper = entity_metadata_wrapper ( 'node' , $node ); $field = $wrapper -> field_my_field_name -> value (); print $field [ 'safe_value' ]; Both of these instances kind of bother me because of the silly logic in trying to get a value that's already available to you, but they are certainly useful in lots of cases.

You could just do print $node->field_my_field_name[LANGUAGE_NONE][0]['value']; but that would throw PHP notice errors if the field doesn't have a value, since you're trying to access arrays that may not exist (i.e. Which is a lot cleaner than doing: Entity metadata wrappers. CCK Hook Documentation (Drupal 6.x) Create a CCK formatter - manuee. CCK formatters are pieces of code that allow you to render a CCK field content how you want. In Drupal 6 this basicaly means a theme function. As an example, we will build a formatter for the field type 'nodereference'. This type of field, which is part of the standard CCK package, allows you to "reference" a node inside another.

The formatter that nodereference has by default, prints a standard link to the referenced node. We are going to give the users other options, allowing them choose if they want the link to open in a new window or, if they have the popups module activated, that it opens in a jQuery modal window. Let's call our module 'formattertest'. Step 1: Declare our CCK formatters To do this, the only thing needed is to implement our hook_field_formatter_info() in our module: <? It's important to remember that the array keys you use, in our case 'newwindow' and 'popup', will be used later on to construct our functions hook_theme and theme_. 2. <? 3. <? Conclusion.