4

I've created a field by implementing hook_field_info, I've created the widget which works perfectly and I've hit a road block with the formatter.

I've implemented hook_field_formatter_settings_form to add some extra output options but I can't work out how to get those values passed to the theme function when the field is rendered.

Here is a simpified version of my code, it's not all the hooks just the ones related to the formatter and the field's theme:

function test_field_formatter_info() {
    return array(
        'test' => array(
            'label' => t('Test'),
            'field types' => array('test'),
        ),
    );
}

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

  foreach ($items as $delta => $item) {

    $element[$delta] = array(
      '#theme' => 'test',
      '#item' => $item,
      '#myextravalue' => $display['settings']['myextravalue'],
    );
  }

  return $element;  
}

function test_theme() {
  return array(
    'test' => array (
      'variables' => array('item' => NULL, 'myextravalue' => NULL),
    ),
  );
}

function theme_test($variables) {   
    $item = $variables['item'];
    ...
    return $output;
}

The widget, field and formatter are working fine, I just can't work out how to get the formatter settings that were defined by hook_field_formatter_settings_form to the test theme function.

When I $variables variable I only see an array for item. myextravalue is not there!

There is very little information on how to create fields/widgets/formatters for Drupal 7 and I'm pulling my hair out with this.

  • Theme formatter settings are fairly new and got into D7 quite late in the day. The documentation should catch up soon but it is tricky without it. I had cracked it in the past but don't have access to the code I used any more. – Jeremy French May 10 '11 at 9:50
4

Your formatter_view implementation looks ok, very similar to image_field_formatter_view().

However, you're info hook seems to be missing default values for your settings, I'm not sure if they are required or not, but try to add them, again see image_field_formatter_info()

Does the field_formatter_settings form work correctly? Does it show correctly in the summary and keeps the values when you edit it again?

Without more information, the only advice I can give you is install devel.module if you haven't already and make extensive use of dpm() to see where you get which information. For example, contains $display the necessary information in your formatter_view() function?

If yes, it could simply be a outdated theme registry that does not yet know about your additional theme argument (if you added later on for example).

  • I'll check the theme registry, though I did have the Devel module installed and I think the rebuild registry on every request was enabled. Thanks for the suggestions! – Camsoft May 10 '11 at 8:02
  • It turns out it was the theme registry! Even though I had the rebuild on every request option turned on in the Devel module! Thanks so much. – Camsoft May 10 '11 at 9:57
  • This just worked for me! My mistake was using 'myextravalue' field in my formatter_view hook but not declaring the same in my hook_theme() x-( /me is stupid sometimes > and even after fixing it, I had to clear all cache from drush for it to work (having theme rebuilt turned ON in zen did not work). Thanks everybody :-) – Sumeet Pareek Jan 28 '13 at 7:08
4

Like any other theme functions, you need to define those array indexes as variables used by the theme function.

For example, you can use the function theme_table() using the following code (the code is present in node_admin_nodes()):

$form['nodes'] = array(
  '#theme' => 'table', 
  '#header' => $header, 
  '#rows' => $options, 
  '#empty' => t('No content available.'),
);

theme_table() is defined in drupal_common_theme() as:

'table' => array(
  'variables' => array('header' => NULL, 'rows' => NULL, 'attributes' => array(), 'caption' => NULL, 'colgroups' => array(), 'sticky' => TRUE, 'empty' => ''),
),

If you want to use "#myextravalue," then you need to define "myextravalue" as one of the variables used by your theme function.

  • myextravalue is defined in test_theme(), the only thing I could think of regarding that is that the theme registry hasn't been updated after it was added. – Berdir May 9 '11 at 22:48
  • It's probably that the reason; if it works for theme_table(), then it should work for any theme function. – kiamlaluno May 9 '11 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.