1

In a Commerce project, I added some custom fields to the customer profile, and I’m using Addressbook. The customer profile is now displayed in the address book tab of each user page.

I need to change the display of this address book. Until now :

  • I changed basic display settings of the fields in the "Manage Display" form, and this had a surprising effect : setting a label as hidden removed completely the default theming of all the view.
  • I tried to use hook_field_formatter_info() and hook_field_formatter_view() as per that Clive answer : this is great but it alters only the native AddressFields, not the custom fields of the customer profile.
  • In the Commerce Addressbook Defaults view, source of the display, there's only one field in the view : Commerce Customer Profile: Rendered Commerce Customer profile.

enter image description here

I could call the fields one by one in the view, but what if I want to alter directly the Rendered Commerce Customer profile field ? Where is it built, and how can I theme or alter it?

  • 1
    That Views field just renders an entity in a particular view mode. It's provided by entity.module (I think the actual code is entity_views_handler_field_entity::render_single_value()). So I think manage display should work as normal. Could you provide some screenshots of what goes wrong? – Andy Jul 30 '14 at 16:59
  • Tks Andy. indeed, manage display works fine, like reordering fields or hide/display labels... What I mentioned is while I used manage display, the default markup which was an awful <h3> for labels, disappeared, and now, remained only a few <span>. So I was wondering why, but I'm especially searching how I can change this render and put mine... – Kojo Jul 30 '14 at 17:05
  • So you'd like to know how to theme field output if I understand correctly? – Andy Jul 30 '14 at 17:52
  • @Andy, I'd like to theme that "Rendered Commerce Customer profile", which appears as a field in that view, but contains the whole customer profile, address fields + custom fields plus edit/delete links ;-) I would be glad to learn that – Kojo Jul 30 '14 at 18:07
  • Sorry I was a bit ambiguous - I didn't mean Views field, just normal core fields. I'll post an answer describing the overall field render process. – Andy Jul 30 '14 at 18:13
1

Generally speaking you can theme field output like other Drupal output. If you use devel_themer* you can normally see the template used and suggestions available. By default fields use theme('field') which by default has a number of suggestions:

// Add specific suggestions that can override the default implementation.
$variables['theme_hook_suggestions'] = array(
  'field__' . $element['#field_type'],
  'field__' . $element['#field_name'],
  'field__' . $element['#bundle'],
  'field__' . $element['#field_name'] . '__' . $element['#bundle'],
);

For example, the first suggestion allows you to override the template/function used for a particular type of field. In this case we could create MYTHEME_field__addressfield() and it would be used for addressfields only. You can add your own suggestions as well.

There are some field API routes as well though. You can use contrib or custom formatters (some of which in turn provide their own theme functions that you can override). Formatters can have settings that might give the results you want. You can add extra settings with a contrib module if you want to extend a formatter. There are also some field-specific hooks that might be useful:

* As you say devel_themer is heavy. It massively changes the markup, and often messes up your JS. I only ever switch it on when I need it and then switch if off immediately afterwards, YMMV!

  • well tks. I installed devel_themer (huge but heavy). Now I can see that the view is rendered by views-view-fields.tpl.php. pb is when I dsm into the view, there is stil the same lone field, or I have to deep far into, to get such variables : dsm($view); $result = $view->result[0]; $entity = $result->_entity_properties[entity object]; $tel_value = $entity->field_user_tel['und'][0]['value']; and so on... but it seems quite heavy just to theme the address book ? and I can't find some pieces like delete and edit links... – Kojo Jul 31 '14 at 11:01
  • last, sorry I'm not sure I understand well the $variables['theme_hook_suggestions'] = array()... – Kojo Jul 31 '14 at 11:05
  • 1
    I've updated the Q a bit regarding devel_themer and suggestions. devel_themer isn't perfect, but I was expecting you'd be able to select inside the Views field to the Drupal fields attached to the customer entity. (I'd expect it to show something like theme_field() or field.tpl.php if you've selected the Drupal field.) – Andy Jul 31 '14 at 12:04
  • Indeed, fields are themed by Fences .tpl & by theme_addressfield_container() ... I'll have to deep into the different solutions you purposed. Unfortunately at the moment I can't find specifically how the Rendered Commerce Customer profile field was rendered, but I also realised, blind I am, that each field is there in the view UI, so I can do the usual way... anyway I learned a lot today. Many thanks for your efforts to help & make this clear for my poor mind ;) – Kojo Jul 31 '14 at 13: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.