I was trying to make some facets display as select boxes in the fieldset realm, but all of the select boxes were empty. In looking at this code from luceneapi_facet.module:

/**
 * Converts a facet to a FAPI array.
 *
 * @param $facet
 *   An array containing a facet retrieved from hook_luceneapi_facet().
 * @return
 *   A FAPI array.
 * @todo add some sanity checks
 */
function luceneapi_facet_to_fapi_convert($facet) {
  $base_form = array(
    '#type' => $facet['type'],
    '#title' => check_plain($facet['title']),
    '#weight' => $facet['weight'],
    '#description' => filter_xss($facet['description']),
  );
  switch ($facet['type']) {
    case 'checkbox':
    case 'date':
    case 'textarea':
    case 'textfield':
      $form[$facet['element']] = array_merge($base_form, array(
        '#default_value' => (string)luceneapi_facet_value_get($facet['element'], '')
      ));
      break;
    case 'checkboxes':
    case 'radios':
    case 'select':
      $form[$facet['element']] = array_merge($base_form, array(
        '#options' => array(),
        '#default_value' => (array)luceneapi_facet_value_get($facet['element'], array())
      ));
      break;
    case 'markup':
      $form[$facet['element']] = array_merge($base_form, array(
        '#value' => $base_form['#title']
      ));
      break;
    default:
      $form = array();
      break;
  }
  return $form;
}

I can't see how the #options can get set, other than doing a form alter. Is this intended behavior, or am I missing something?

Comments

cpliakas’s picture

Hi ebeyrent.

The way the API is set up, you have to set the options in hook_luceneapi_facet_postrender_alter(). The reason why it has to be done so late is because FAPI arrays are mode difficult to make assumptions for in terms of what data you are trying to populate, so this gives your module the opportunity to set the options without some arbitrary query running beforehand possibly inhibiting performance. Furthermore, some FAPI elements don't have any values to populate. Take a look at luceneapi_node_luceneapi_facet_postrender_alter() in the contrib/luceneapi_node/luceneapi_node.module file for a working example.

Thanks,
Chris

cpliakas’s picture

Status: Active » Closed (fixed)

Marking as closed after a period of inactivity.