There are potentially many entity types that do not use $variables['content'] as their renderable element. The core user module is one, and that is special-cased in field_group module's entity handling as $variables['user_profile'].

Unfortunately these two content variable keys are hard-coded. It would be useful to provide a hook to allow contrib modules to add their (or others) content key to an array, and use that when manipulating the group structure (or any other task as needed). That would allow entity modules to easily implement field group support, or allow any contrib module to add fieldgroup support for any entity module.

Proposed patch to come.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

John Pitcairn created an issue. See original summary.

John Pitcairn’s picture

Status: Active » Needs review
FileSize
4.65 KB

Here is a first stab at it. This patch adds a new function:

/**
 * Provides the content element key for a display context.
 *
 * This allows entity modules to specify their content element for field group
 * support, or other modules to add entity module support.
 *
 * @param $context
 *   The display context (entity type, form or view).
 */
function field_group_get_content_element_key($context = 'default')

And uses that to get the default content variable keys - 'content' or 'user_profile' - in field_group_build_entity_groups() and field_group_fields_nest(). The latter adds a third optional parameter, $context, to allow passing in the display context. Wherever $context is mentioned in existing code comments, those are updated to indicate $context may be the entity type, and it is this that is used to provide a corresponding content element key in field_group_get_content_element_key().

Finally, hook_field_group_content_element_keys_alter() provides a way for other modules to add entity-type to content-element-key mappings. This means that entity modules can easily add fieldgroup support themselves, or crucially, any other module or theme may add fieldgroup support on behalf of any entity module.

For example, to add fieldgroup support for commerce product variation entities:

function MYMODULE_field_group_content_element_keys_alter(&$keys) {
  if (!isset($keys['commerce_product_variation'])) {
    $keys['commerce_product_variation'] = 'product_variation';
  }
}
John Pitcairn’s picture

Title: Allow contrib to supply the variable content key for any entity type » Allow contrib to add support for any entity type
John Pitcairn’s picture

Issue summary: View changes
John Pitcairn’s picture

We could modify this to allow modules to specify the $element variable as well.

John Pitcairn’s picture

I also think the $variables['user_profile'] key is no longer correct for user module in Drupal 8. That would be a followup issue.

John Pitcairn’s picture

Assigned: John Pitcairn » Unassigned
nils.destoop’s picture

Status: Needs review » Fixed

Good addon. I committed it to dev.

nils.destoop’s picture

The user_profile is idd not correct anymore. I removed it.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

kmajzlik’s picture

This should be re-opened and ported to 8.x-3.x because it does not work with Commerce (products, variations etc.)

John Pitcairn’s picture

Component: Miscellaneous » Code

@karlos007: message the maintainer directly and ask him to reopen this and assign to 8.x-3.x. It's unlikely @zuuperman pays attention to closed/fixed issues.

John Pitcairn’s picture

@karlos007: This patch is already in 8.x-3.x. You need to implement the appropriate hook to specify the correct Commerce element keys, as per the example in #2.

Ideally, Commerce itself would implement this hook. The discussion is #2906502: Compatibility with Field group module

PFEnriquez’s picture

Hi,

I tried to implement the hook this way (changing obviously "hook" with my module


function hook_field_group_content_element_keys_alter(&$keys) {
  if (!isset($keys['commerce_product_variation'])) {
    $keys['commerce_product_variation'] = 'product_variation';
  }

  if (!isset($keys['commerce_product'])) {
    $keys['commerce_product'] = 'product';
  }
}

but the field groups are still not rendered on Commerce products. The error seems to be on field_group side (commerce feedbacks) and I wonder it should be solved as it is a very important features for contrib modules in general, including Commerce.

PFEnriquez’s picture

Version: 8.x-1.x-dev » 8.x-3.x-dev