I'm using field_group with my own UI, so I need to be able to drive it programmatically. At the moment, there's no function to load a field group - The closest is field_group_menu_load(), but this means feeding in some extra params.

I'd suggest adding a function called field_group_load(), which does nothing but load a field group, move the loading code out of field_group_menu_load() into there and call it. The new function would match up nicely with the existing field_group_save().

Will make a patch for this if I get the time, but if anyone else would like to do it in the meantime that's cool :)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Hydra’s picture

Status: Active » Needs review
FileSize
1.08 KB

I agree, the function of field_group_menu_load should be used in field_group_load. Hope this is waht you mentioned.

Stalski’s picture

This patch will not do and I think @rupertj meant that field_group_menu_load should delegate into a more APi function, like field_group_load.

Just renaming the function won't do the job. For instance the last parameter is a map argument of "load arguments" in the menu registry. We can't expect people to call that function with all those required parameters.

So I think the best we can do is take out a portion of the function, like:

function field_group_load($group_name, $entity_type, $bundle_name) {
  ctools_include('export');
  $objects = ctools_export_load_object('field_group', 'conditions', array(
    'group_name' => $group_name,
    'entity_type' => $entity_type,
    'bundle' => $bundle_name,
    'mode' => $mode
  ));
  $object = array_shift($objects);
  return $object;
}
function field_group_menu_load($group_name, $entity_type, $bundle_name, $bundle_pos, $map) {

  if ($bundle_pos > 0) {
    $bundle = $map[$bundle_pos];
    $bundle_name = field_extract_bundle($entity_type, $bundle);
  }

  $args = func_get_args();
  $args_pop = array_pop($args);
  $mode = array_pop($args_pop);

  $object = field_group_load($group_name, $entity_type, $bundle_name);
  if ($object && isset($object->data)) {
    return field_group_unpack($object);
  }
  return $object;

}
rupertj’s picture

Stalski's got it spot on there. Although, I'd be tempted to move the data unpacking inside the load function, as you'll need to do it to get a properly loaded object.

$mode needs adding as a param too.

The attached patch does all this, and seems to work for me.

Stalski’s picture

yeah, that seems correct indeed. Thx for the patch. I'll look at it asap.

nils.destoop’s picture

Status: Needs review » Fixed

Patch has been tested and committed to dev. Thx :)

casey’s picture

Status: Fixed » Needs work

Uhhh this causes a namespace issue with og.module and field.module in DrupalDefaultEntityController::attachLoad().

  • OG defines an entity type "group".
  • DrupalDefaultEntityController::attachLoad() invokes hook_ENTITYTYPE_LOAD (actually a callback, but this default is set in entity_get_info())
  • field_group_load is seen as an implementation of this hook implemented by field.module
  • Result:

    Warning: Missing argument 2 for field_group_load() in field_group_load() (regel 125 van drupal\sites\all\modules\field_group\field_group.module).
    Warning: Missing argument 3 for field_group_load() in field_group_load() (regel 125 van drupal\sites\all\modules\field_group\field_group.module).
    Warning: Missing argument 4 for field_group_load() in field_group_load() (regel 125 van drupal\sites\all\modules\field_group\field_group.module).
    Notice: Undefined variable: entity_type in field_group_load() (regel 130 van drupal\sites\all\modules\field_group\field_group.module).
    Notice: Undefined variable: bundle_name in field_group_load() (regel 131 van drupal\sites\all\modules\field_group\field_group.module).
    Notice: Undefined variable: mode in field_group_load() (regel 132 van drupal\sites\all\modules\field_group\field_group.module).
    Recoverable fatal error: Object of class OgGroup could not be converted to string in DatabaseStatementBase->execute() (regel 2137 van drupal\includes\database\database.inc).

rupertj’s picture

Ah, that's not so great... How about we rename field_group's field_group_load() to field_group_group_load()? Would be nice if the save function was renamed too to keep it consistent, and also avoid another potential clash.

Stalski’s picture

I guess we will have too.

nils.destoop’s picture

Status: Needs work » Fixed

i changed the load, delete and save function.

casey’s picture

Status: Fixed » Needs work

Yike this is so nasty. Now the field_group module itself is considered to implement hook_ENTITYTYPE_LOAD for entity type group. Maybe we should ask the maintainers of og.module to rename its entity type to og_group.

casey’s picture

Stalski’s picture

Hmm, This is nasty indeed. It seems waiting on #1295566: Rename entity type "group" to "og_group" will be the issue.
Wouldn't it be better to just call the function _field_group_load() for now? I can't think of any other function names that would help this issue. Maybe field_group_api_load ...

Stalski’s picture

There is also a duplicate issue at #1297858: Please rename function again where a patch is posted for "field_group_load_field_group", which is acceptable too.

Stalski’s picture

Status: Needs work » Fixed

This is pushed to git with the solution of "field_group_load_field_group" as name of the API function. I hope everyone can live with this (temporary solution).

Status: Fixed » Closed (fixed)

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