I'm updating this question on the suggestion of the initial provided answer.
When trying to render out the content of my custom field, outputting the data into a custom theming function to render on to the page when using the suggested method is resulting in the theming function being ignored, even when attempting to output just static strings of text or using the devel module to test it, nothing happens, the code is updated as below
/**
* Implements hook_field_formatter_view().
*/
function mymodule_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
foreach ($items as $delta => $item) {
if ($file = file_load($item['image'])) {
list($hours, $mins, $secs) = explode(':', $item['time']);
$time = ((3600 * $hours) + (60 * $mins) + $secs);
$element[$delta] = array(
'#theme' => 'mymodule_formatter',
'#image' => (array) $file,
'#time' => $time,
'#comment' => $item['comment'],
);
}
}
return $element;
}
Hook and Theming function
/**
* Implements hook_theme().
*/
function mymodule_theme() {
return array(
'mymodule_formatter' => array(
'variables' => array('variables' => array()),
'file' => 'theme_mymodule.inc',
),
);
}
In the theme_mymodule.inc file
/**
* Returns HTML for a MyModule.
*/
function theme_mymodule_formatter($variables) {
$output = t('test');
return $output;
}
I think I'm a little confused about the naming of the theme hook, when I was copying the image module, the #theme
variable was just declared as 'image_formatter' but the function named was 'theme_image_formatter', I'm wondering if I've declared my theme hook wrong.
--Previous question--
Im trying to write my own custom field in my own module, I've written the field setup, and it's accepting and evaluating data fine, that's all good. I'm struggling to grasp what I need to do when trying to write my own theming output function. As an example to the parts of code I'm working on:
/**
* Implements hook_field_formatter_view().
*/
function mymodule_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
foreach ($items as $delta => $item) {
if ($file = file_load($item['image'])) {
list($hours, $mins, $secs) = explode(':', $item['time']);
$time = ((3600 * $hours) + (60 * $mins) + $secs);
$variables = array(
'image' => (array) $file,
'time' => $time,
'comment' => $item['comment'],
);
$element[$delta] = theme('mymodule_formatter', $variables);
}
}
return $element;
}
And my theming function
/**
* Returns HTML for a MyModule.
*/
function theme_mymodule_formatter($variables) {
$item = $variables['image'];
$image = array(
'path' => $item['uri'],
);
if (isset($item['attributes'])) {
$image['attributes'] = $item['attributes'];
}
if (isset($variables['image_style'])) {
$image['style_name'] = $variables['image_style'];
$output = theme('image_style', $image);
}
else {
$output = theme('image', $image);
}
return $output;
}
Now this returns the correct HTML for my image, so it actually returns a string like
<img typeof="foaf:Image" src="[img_path]" width="120" height="90" alt="" />
However this doesn't actually render on my page, it doesn't come back with an error, it just doesn't show anything.
When I change my $element[$delta]
line in my hook_field_formatter_view()
function to say
$element[$delta] = array('#markup' => theme('mymodule_formatter', $variables));
By putting the element into a '#markup'
array, it does actually render the image, however I don't want to just throw everything into a '#markup'
array if that's not the proper way of doing it.