When body_field is inside a fieldset, it is not found.
To solve this:


function &get_array_containing_key($key, &$array)
{
    $result = isset($array[$key]);
    if ($result) return $array;

    foreach ($array as $vk=>$v) {
        if (is_array($v)) {
            $result = &get_array_containing_key($key, $array[$vk]);
        }
        if ($result) return $result;
    }

    return $result;
}

function better_formats_set_node_format(&$form) {
  // Set core body field.

  $bodyform = get_array_containing_key('body_field', $form);
  if ($bodyform) {
    // Get default for new entries.
    $default = better_formats_get_default_format('node', $form['type']['#value']);

    if (empty($form['nid']['#value'])) {
      // Set format to default for new entries.
      $format = $default;
    }
    else {
      // Get existing format for core body field.
      $format = better_formats_get_current_format($bodyform['body_field']['format']);
    }

    // Overwrite the filter form with our own.
    $bodyform['body_field']['format'] = better_formats_filter_form($format, $default, 'node', $form['type']['#value']);
  }
}

Hope it helps.

Comments

savioret’s picture

sorry the correct call is


$bodyform = &get_array_containing_key('body_field', $form);

dragonwize’s picture

Status: Active » Postponed (maintainer needs more info)

How are you putting the body field in a fieldset? That is not done by normal means so it sounds like you are creating a custom solution.

Performance is a factor that I am concerned with and have not used the "loop through every level of all FAPI arrays to find fields" method. I am very hesitant to put such a large performance hit for such a rare case.

If you are doing a custom job on your forms you can easily use the BF functions to get the default and set it for your case.

savioret’s picture

Hi dragonwise,

I think that is a common task putting fields inside collapsible fieldsets. It is because drupal gives you the possibility to do it.
When users create their own node-types they can re-arrange the fields in different ways, and you can't be sure whether the body_field is in the form root.
Maybe this reaarrange is done by the oser, or by other module installed by the user.
The problem is that when someone changes the fields order, they will notice that BF doesn't work, and they'll probably won't know why because they shouldn't know that BF is not handling the field if its position is changed.

In my case all my module forms are inside collapsible fields. I know the function is a bit low performance, but this fact doesn't mean that this issue should not be taken into account, because many people can be in the same situation.

Is the solution you are telling me to implement my own hook_alter and rewrite my own better_formats_set_node_format function ?
Should anybody using form fieldsets implement their own implementation?

regards.

dragonwize’s picture

My point is this:

1. Only the body field is affected because cck fields are handled prior to form_alter at the cck field level.

2. Order also has absolutely nothing to do with it.

3. The cck fieldgroup module does NOT allow you to put the body field or any other non-cck field in a field set. So you must be using a custom or little used method to put the body field into a field set.

4. Because of the previous points it would not be worth a huge performance hit for 99.99% of users to help a rare customized solution.

Those are my thoughts. I am not perfect though and could be wrong. I am giving you another chance to prove to me in some way that your case is a widespread case and not a custom one.

savioret’s picture

Hmmm, I can't say anything because I'm not CCK user and I didn't know anything about that, sorry.
When I said "order" I meant the disposition/level inside the form array hierarchy.

So, the my case is:
- body_field must be in the form root.
- I need body_field to be inside a fieldset.

I have no idea of how to solve this. Any suggestion would be appreciated.
Thanks in advance.

dragonwize’s picture

Title: bug when body_field is inside a fieldset » Work with custom body_field inside a fieldset
Category: bug » feature
Status: Postponed (maintainer needs more info) » Closed (won't fix)

You are correct in your above assumptions. I can not program for every possible customizable case without a huge performance hit. What about changing the name of the body field or completely custom fields that I have absolutely no data on. While the loop through method might catch many of those "as long as you haven't customized the format area", with the current state of the API around formats in Drupal it is not possible without a large performance hit.

If you want to use BF with your custom fields that are different than normal you can set a form_alter like below:


function example_form_alter(&$form, $form_state, $form_id) {
  switch ($form['#id']) {
    // USE YOUR FORM ID.
    case 'node-form':
      example_set_node_format($form);
      break;
  }
}

function example_set_node_format(&$form) {
  // Set core body field.
  if (isset($form['body_field'])) {
    // Get default for new entries.
    $default = better_formats_get_default_format('node', $form['type']['#value']);

    if (empty($form['nid']['#value'])) {
      // Set format to default for new entries.
      $format = $default;
    }
    else {
      // Get existing format for core body field.
      // HERE YOU WILL NEED TO DETERMINE THE CURRENT FORMAT SIMILAR TO:
      // better_formats_get_current_format($form['body_field']['format'])
      $format = $form['body_field']['format'];
    }

    // Overwrite the filter form with our own.
    $form['body_field']['format'] = better_formats_filter_form($format, $default, 'node', $form['type']['#value']);
  }
}


savioret’s picture

dragonwize,

Thanks for your advise, I solved the problem.