You are here

function _views_bulk_operations_modify_action_get_bundles in Views Bulk Operations (VBO) 7.3

Returns all bundles for which field widgets should be displayed.

If the admin decided to limit the modify form to certain properties / fields (through the action settings) then only bundles that have at least one field selected are returned.

Parameters

string $entity_type: The entity type whose bundles will be fetched.

array $context: The VBO context variable.

1 call to _views_bulk_operations_modify_action_get_bundles()
views_bulk_operations_modify_action_form in actions/modify.action.inc
Action form function.

File

actions/modify.action.inc, line 526
VBO action to modify entity values (properties and fields).

Code

function _views_bulk_operations_modify_action_get_bundles($entity_type, $context) {
  $bundles = array();
  $view = $context['view'];
  $vbo = _views_bulk_operations_get_field($view);
  $display_values = $context['settings']['display_values'];
  $info = entity_get_info($entity_type);
  $bundle_key = $info['entity keys']['bundle'];

  // Check if this View has a filter on the bundle key and assemble a list
  // of allowed bundles according to the filter.
  $filtered_bundles = array_keys($info['bundles']);

  // Go over all the filters and find any relevant ones.
  foreach ($view->filter as $key => $filter) {

    // Check it's the right field on the right table.
    if ($filter->table == $vbo->table && $filter->field == $bundle_key) {

      // Exposed filters may have no bundles, so check that there is a value.
      if (empty($filter->value)) {
        continue;
      }
      $operator = $filter->operator;
      if ($operator == 'in') {
        $filtered_bundles = array_intersect($filtered_bundles, $filter->value);
      }
      elseif ($operator == 'not in') {
        $filtered_bundles = array_diff($filtered_bundles, $filter->value);
      }
    }
  }
  foreach ($info['bundles'] as $bundle_name => $bundle) {

    // The view is limited to specific bundles, but this bundle isn't one of
    // them. Ignore it.
    if (!in_array($bundle_name, $filtered_bundles)) {
      continue;
    }
    $instances = field_info_instances($entity_type, $bundle_name);

    // Ignore bundles that don't have any field instances attached.
    if (empty($instances)) {
      continue;
    }
    $has_enabled_fields = FALSE;
    foreach ($display_values as $key) {
      if (strpos($key, $bundle_name . '::') === 0) {
        $has_enabled_fields = TRUE;
      }
    }

    // The admin has either specified that all values should be modifiable, or
    // selected at least one field belonging to this bundle.
    if (!empty($display_values[VBO_MODIFY_ACTION_ALL]) || $has_enabled_fields) {
      $bundles[$bundle_name] = $bundle;
    }
  }
  return $bundles;
}