You are here

function _farm_ui_entityreference_view_widget_views_arguments_alter in farmOS 7

Helper function for altering entityreference_view_widget arguments.

1 call to _farm_ui_entityreference_view_widget_views_arguments_alter()
farm_ui_entityreference_view_widget_views_arguments_alter in modules/farm/farm_ui/farm_ui.module
Implements hook_entityreference_view_widget_views_arguments_alter().

File

modules/farm/farm_ui/farm_ui.views.inc, line 78

Code

function _farm_ui_entityreference_view_widget_views_arguments_alter(&$arguments, $form_state, $view) {

  // The farm_log module provides a View that is used for searching and
  // selecting assets for the field_farm_asset entity reference field, using
  // the Entity Reference View Widget module.
  //
  // In some cases, we want to limit what shows up in the View of assets, based
  // on the log type that the field is on. For instance, a seeding should only
  // be able to reference planting assets, but it still uses the general
  // field_farm_asset field, which technically can reference any asset type.
  //
  // The Entity Reference View Widget module provides this hook to allow the
  // View arguments to be altered. We implement this hook, and use information
  // provided by hook_farm_ui_entities() to see if we should filter the View
  // results to a specific asset type, given the current log type.
  //
  // The View that we use is farm_asset_entityreference_view, which has two
  // contextual arguments: asset id (to filter out already-selected assets), and
  // asset type. The code below only tries to set the asset type argument, and
  // maintains any asset id argument that is already set.

  /**
   * @todo
   * Note that this does NOT alter the "Asset type" exposed filter. So users
   * will still see that filter, and it will still default to "- Any -". If
   * the contextual filter is activated here, however, then selecting any other
   * asset type with the exposed filter will return an empty result set. That's
   * confusing, so perhaps we should find a way to hide that exposed filter if
   * this code successfully sets a contextual filter argument.
   */

  // Only apply this to the View: farm_asset_entityreference_view
  if ($view->name != 'farm_asset_entityreference_view') {
    return;
  }

  // Figure out what the log type is.
  if (!empty($form_state['build_info']['args'][0]->type)) {
    $log_type = $form_state['build_info']['args'][0]->type;
  }
  else {
    return;
  }

  // Load entity UI info for this log type.
  $ui_info = farm_ui_entities('log', $log_type);

  // If the asset type is empty, or 'all', or 'none', then we will stop here
  // because we don't want to apply any filtering.
  if (empty($ui_info['farm_asset']) || in_array($ui_info['farm_asset'], array(
    'all',
    'none',
  ))) {
    return;
  }

  // Take the asset type specified so we can use it as a filter.
  $type = $ui_info['farm_asset'];

  // We need to ensure that the first argument is reserved for asset id(s). So
  // if it's not set, set it to 'all'.
  if (empty($arguments)) {
    $arguments[0] = 'all';
  }

  // Add the asset type as the second argument.
  $arguments[1] = $type;
}