0

I have an existing module that used node_view to build out html for an entity.
Customer desires access in Drupal to change up the order of the display of the fields and Display Suite seems to allow this.
Now to integrate my old functions into the api. Based on this post I should be able to do this.

Key line from this post is that this general form should work:

$layout_render_array['ds_content']['Foo']['#markup'] = 'Bar';

The thing I'm finding is that to get to ['Foo'] is really tricky and I haven't been able to determine it.
That array changes depending on what layout is in play for a given display_mode and I'm trying many variations of searching for my field_collection name in order to dynamically return the location in this array in order to overwrite the field_collection in the render array to be

'#markup' = mymodule_address_only($vars['nid']);

And that's assuming that #markup will overwrite this part of the Drupal render array for a field_collection.

Other searches say I should create a ds_2-col-stacked--field-collection---.tpl.php.

I've done that, but I'm copying my existing function for flattening a field_collection into a .tpl file which is messy and has to be duplicated for every layout. This needs to be in my module.

function mymodule_ds_pre_render_alter(&$layout_render_array, $context, &$vars) {
  if ($context['bundle'] == 'mycontenttype' && property_exists($context['entity'], 'field_address')) {
    foreach ($layout_render_array['ds_content'] as $key => $value) {
      if (in_array('field_address', $value, TRUE)) {$layout_render_array['ds_content'][$key]['#markup'] = _mymodule_address_only($vars['nid']);
  }
}

How would you flatten a field_collection in a ds_pre_render_alter()?

I'm suspicious that even if I could dynamically find this field_collection in the $layout_render_array that since it is a field_collection that a simple alter would not work. For instance, setting a breakpoint in my module I've set the equivalent of the following to where I know where this field_collection is in this instance.

$layout_render_array['ds_content'][1]['field_title']['#markup'] = "Arbitrary test text here";

Yet, the output html does not reveal this string. I expect because Drupal render is noting it's a field_collection and iterating through each of its elements, picking up each element's #markup. I need to override that in my module so it can pick up my already built up #markup.

Your Answer

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

Browse other questions tagged or ask your own question.