How to theme a field formatter? Context: using the registration module, I am trying to customize the entity registration form depending on the user:


show a 'please login in order to register' to anonymous visitors
show a 'edit registration' link to already registered users
show the registration form, as well as an info message (xx seats left / fully booked butwait list is still open), to not registered yet users.


Current status: I managed to customize the field display using a field template in my custom theme. Unfortunately the field is not displayed anymore when the event is fully booked (without a 'fully booked' message). I changed that by implementing a custom field formatter based on the registration 'form' field formatter.

Problem: In order to have the thing easier to maintain, it sounds like a better idea to have the theming done directly in the module (named 'zss'). Although it is probably easy meat for drupalers, I do not manage to do that so far. The current code reads like:

function zss_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  // we know we should only have a single item
  if (isset($items[0]['registration_type']) &amp;&amp; !empty($items[0]['registration_type'])) {
    $reg_type = registration_type_load($items[0]['registration_type']);
    $settings = $display['settings'];
    $label = !empty($settings['label']) ? $settings['label'] : $reg_type-&gt;label;

    if ($display['type'] == 'zss_registration_default') {
      // Enable registration link if accessible.
      list($entity_id) = entity_extract_ids($entity_type, $entity);
      if (registration_register_page_access($entity_type, $entity)) { // &amp;&amp; registration_status($entity_type, $entity_id)
        $registration = entity_get_controller('registration')-&gt;create(array(
          'entity_type' =&gt; $entity_type,
          'entity_id' =&gt; $entity_id,
          'type' =&gt; $reg_type-&gt;name,
        ));
        $element[0] = drupal_get_form('registration_form', $registration);
//         I tried the following to theme the field:
//         $element[0] = theme('zss_theme_zss_registration_default', drupal_get_form('registration_form', $registration));
      }
    }
  }
  return $element;
}

/**
 * Implements hook_theme().
 */
function zss_theme() {
  return array(
    // The theme function that our formatter uses:
    'zss_theme_zss_registration_default' =&gt; array(
      // Don't forget that all Drupal 7 theme functions have only one argument,
      // so you declare what variables get passed within that argument instead.
      // See http://drupal.org/node/224333#theme_changes
      'variables' =&gt; array('element' =&gt; NULL),
    ),
  );
}

/**
 * Theme function for 'zss_theme_zss_registration_default' field formatter.
 */
function zss_theme_zss_registration_default($element) {
  // Here I would like to have a condition, depending for instance on the user id
  return $element;
}


Thank you in advance for your help.
What I wanted was actually to theme the registration field directly from the module, by implementing zss_field__field_registration($variables).

So, if you have a similar problem, you need to proceed in 2 steps:


create a custom field formatter for your registration fields (described above). This will allow to display the form even when the current user has already registered.
theme the registration field from your module so as to display it according to the current user and available seats.