diff --git a/facetapi.admin.inc b/facetapi.admin.inc index b5254fa..0dd5bca 100644 --- a/facetapi.admin.inc +++ b/facetapi.admin.inc @@ -217,6 +217,85 @@ function facetapi_get_settings_path($searcher, $realm_name, $facet_name, $op) { } /** + * Returns array of widget definitions available to the facet. + * + * @param array $realm + * The realm definition. + * @param array $facet + * The facet definition. + * + * @return array + * An array of available widget plugins. + */ +function facetapi_get_widgets(array $realm, array $facet) { + $plugins = array(); + + // Iterates over all defined plugins. + foreach (ctools_get_plugins('facetapi', 'widgets') as $id => $plugin) { + $append = TRUE; + + // Initializes the requirements if empty. + $plugin['handler'] += array( + 'requirements' => array( + 'facetapi_requirement_realm_property' => array('element type' => 'links') + ), + ); + + // Requirements fail if at least one requirements callback returns FALSE. + foreach ($plugin['handler']['requirements'] as $callback => $options) { + if (!call_user_func($callback, $realm, $facet, $options)) { + $append = FALSE; + break; + } + } + + // Appends plugin if no requiremetns failed. + if ($append) { + $plugins[$id] = $plugin; + } + } + + return $plugins; +} + +/** + * Checks realm property requirement. + */ +function facetapi_requirement_realm_property(array $realm, array $facet, array $options) { + return facetapi_requirement_property($realm, $options); +} + +/** + * Checks facet property requirement. + */ +function facetapi_requirement_facet_property(array $realm, array $facet, array $options) { + return facetapi_requirement_property($facet, $options); +} + +/** + * Checks the equality of a property. + */ +function facetapi_requirement_property(array $definition, array $options) { + $passed = TRUE; + foreach ($options as $key => $requirement) { + $condition = $definition[$key]; + if (is_array($requirement)) { + if (array_intersect_key($condition, $requirement) != $requirement) { + $passed = FALSE; + break; + } + } + else { + if ($requirement != $condition) { + $passed = FALSE; + break; + } + } + } + return $passed; +} + +/** * Facet display settings form. * * @param $form @@ -272,7 +351,7 @@ function facetapi_facet_settings_form($form, &$form_state, FacetapiAdapter $adap // Builds select options for widgets, allows widgets to add settings. $widget_options = array(); - foreach (ctools_get_plugins('facetapi', 'widgets') as $id => $plugin) { + foreach (facetapi_get_widgets($realm, $facet) as $id => $plugin) { $widget_options[$id] = $plugin['handler']['label']; $class = $plugin['handler']['class']; $plugin = new $class($id, $realm, $adapter->getFacet($facet), $facet_settings); diff --git a/facetapi.module b/facetapi.module index 8ae511b..37282d5 100644 --- a/facetapi.module +++ b/facetapi.module @@ -373,8 +373,7 @@ function facetapi_get_realm_info() { 'description' => '', 'default widget' => '', 'settings callback' => FALSE, - 'widget requirements' => array(), - 'weight' => 0, + 'element type' => 'links', 'sortable' => TRUE, ); } @@ -422,14 +421,6 @@ function facetapi_get_facet_info($searcher) { array('display', SORT_ASC), ), ); - - // Checks whether facet is flat or hierarchical, adds to requirements. - if (!$facet_info[$facet_name]['hierarchy callback']) { - $facet_info[$facet_name]['widget requirements'][] = 'flat'; - } - else { - $facet_info[$facet_name]['widget requirements'][] = 'hierarchical'; - } } // Invokes alter hook, sorts and returns. @@ -518,19 +509,24 @@ function facetapi_facetapi_realm_info() { 'weight' => -10, 'sortable' => FALSE, 'default widget' => 'facetapi_links', + 'element type' => 'links', 'description' => t( 'The Blocks realm displays each facet in a separate block. Users are able to refine their searches in a drill-down fashion.', array('@block-page' => url('admin/structure/block', array('query' => array('destination' => $_GET['q'])))) ), ); -/* // @todo - implement this. http://drupal.org/node/1132744 + + /* + // @todo - implement this. http://drupal.org/node/1132744 $realms['fieldset'] = array( 'label' => t('Fieldset'), 'weight' => -5, 'default widget' => 'facetapi_textfield', + 'element type' => 'form elements', 'description' => t('The Fieldset realm displays facets as form elements in a fieldset below the search form that is similar in appearance to the core Search module\'s Advanced search fieldset.'), ); -*/ + */ + return $realms; }