Custom widget not rendered within field group I wrote a custom module that adds a widget. This widget consists of multiple textfields and an ajax button that sends that information to an external service and gets in return and identifier that is stored locally.

However, it is not possible to render this widget within a group. (Using the field group module)
When adding it to a content type it will never be rendered within a group (as a fieldset, a tab, div or whatever), but instead before or after it.

My custom fields within the widget are bundled together by:

$form['fieldset']['field_name1']
$form['fieldset']['field_name2']


But even when they are not put together as a fieldset, it doesn't work.

How come?

Here is a code snipped of the definition of the fieldset:

$form['b2share_fieldset'] = array(
  '#type'  =&gt; 'fieldset',
  '#title'  =&gt; t('Publish data on B2Share'),
  '#collapsible'  =&gt; TRUE,
  '#collapsed' =&gt; FALSE,
  '#tree'  =&gt; TRUE,
);

$form['b2share_fieldset']['database_field_name'] = array(
   '#type'  =&gt; 'hidden',
   '#default_value'  =&gt; $b2share_fieldname,
);


The complete module can be found here.

I want to emphasize that my widget works fine. It's just always displayed outside the fieldgroup even when it is placed within the fieldgroup. Declaring it as a container as described here doesn't work either. 

I also tried the patch provided here, but without success.

Also when I remove the fieldgroup and just leave the fields in there, they are always rendered outside the parent element.There aren't any status or error messages either.

I actually want to publish this as a contributed module, so I'd appreciate any help as I want to get rid of this bug, before I do that.

The problem is also described here.
OK. I finally found the problem based on the example code here.
The problem was that I constantly used "form" instead of element. Once I had replaced that the rendering was correct.

$form['b2share_fieldset']

instead of 
$element['b2share_fieldset']

However, the huge downside is that now I don't know how to access the field information as $form_state isn't working anymore.

EDIT: After applying some minor changes in the code I can confirm that this approach works. The fully functional code can be seen on github.