Quantcast
Channel: Question and Answer » formatters
Viewing all articles
Browse latest Browse all 10

How to theme a field formatter?

$
0
0

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']) && !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->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)) { // && registration_status($entity_type, $entity_id)
        $registration = entity_get_controller('registration')->create(array(
          'entity_type' => $entity_type,
          'entity_id' => $entity_id,
          'type' => $reg_type->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' => 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' => array('element' => 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.


Viewing all articles
Browse latest Browse all 10

Trending Articles