Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
When adding Pseudo fields with hook_field_extra_fields these do not get added to the table, The reason for this is because the formatter is only displaying fields from field_info_instances.
/**
* Implements hook_field_extra_fields().
*/
function mymodule_field_extra_fields() {
$extra = array();
$extra['field_collection_item']['field_invoice_line_item'] = array(
'display' => array(
'invoice_line_item_total' => array(
'label' => t('Total'),
'description' => t('Line Item Subtotal'),
'weight' => 0,
),
),
);
return $extra;
}
/**
* Implements hook_entity_view().
*/
function mymodule_entity_view($entity, $type, $view_mode, $langcode) {
if($type == 'field_collection_item' && $entity->field_name == 'field_invoice_line_item') {
$entity->content['invoice_line_item_total'] = array(
'#title' => t('Total'),
'#markup' => '50.00',
'#weight' => 5,
);
}
}
Proposed resolution
Allow Psudeo fields to be displayed by not relying on the fields themselves, The patch below uses element_children and the #title attribute to create the fields and header arrays, which fixes the problem I was having.
Comment | File | Size | Author |
---|---|---|---|
#2 | field_collection_table-support_for_psuedo_fields-707484-2.patch | 1.89 KB | paulmartin84 |
Comments
Comment #2
paulmartin84 CreditAttribution: paulmartin84 as a volunteer commentedComment #3
gusantor CreditAttribution: gusantor commentedHello, I've similar issue
when I choose "Table of field collection items" as formatter for the field collection item that contains the extra field, the extra field disappear from display
I've applied your path with
patch -p1 < field_collection_table-support_for_psuedo_fields-707484-2.patch
but recive errror:
Hunk #1 FAILED at 37.
1 out of 1 hunk FAILED -- saving rejects to file field_collection_table.module.rej
anyway, I manually did the changes, it works !
thanks
Comment #4
gusantor CreditAttribution: gusantor commentedsorry but patch at #2 couses a 500 error in another place (content type)
so I've changed it a bit, instead of change the foreach loop, I've added another one following the original, lookig for extra fields
and it works :)