function field_test_field_storage_query

7.x field_test.storage.inc field_test_field_storage_query($field_id, $conditions, $count, &$cursor = NULL, $age)

Implements hook_field_storage_query().

File

drupal-7.x/modules/field/tests/field_test.storage.inc, line 246
Defines a field storage backend.

Code

function field_test_field_storage_query($field_id, $conditions, $count, &$cursor = NULL, $age) {
  $data = _field_test_storage_data();

  $load_current = $age == FIELD_LOAD_CURRENT;

  $field = field_info_field_by_id($field_id);
  $field_columns = array_keys($field['columns']);

  $field_data = $data[$field['id']];
  $sub_table = $load_current ? 'current' : 'revisions';
  // We need to sort records by entity type and entity id.
  usort($field_data[$sub_table], '_field_test_field_storage_query_sort_helper');

  // Initialize results array.
  $return = array();
  $entity_count = 0;
  $rows_count = 0;
  $rows_total = count($field_data[$sub_table]);
  $skip = $cursor;
  $skipped = 0;

  foreach ($field_data[$sub_table] as $row) {
    if ($count != FIELD_QUERY_NO_LIMIT && $entity_count >= $count) {
      break;
    }

    if ($row->field_id == $field['id']) {
      $match = TRUE;
      $condition_deleted = FALSE;
      // Add conditions.
      foreach ($conditions as $condition) {
        @list($column, $value, $operator) = $condition;
        if (empty($operator)) {
          $operator = is_array($value) ? 'IN' : '=';
        }
        switch ($operator) {
          case '=':
            $match = $match && $row->{$column} == $value;
            break;
          case '<>':
          case '<':
          case '<=':
          case '>':
          case '>=':
            eval('$match = $match && ' . $row->{$column} . ' ' . $operator . ' ' . $value);
            break;
          case 'IN':
            $match = $match && in_array($row->{$column}, $value);
            break;
          case 'NOT IN':
            $match = $match && !in_array($row->{$column}, $value);
            break;
          case 'BETWEEN':
            $match = $match && $row->{$column} >= $value[0] && $row->{$column} <= $value[1];
            break;
          case 'STARTS_WITH':
          case 'ENDS_WITH':
          case 'CONTAINS':
            // Not supported.
            $match = FALSE;
            break;
        }
        // Track condition on 'deleted'.
        if ($column == 'deleted') {
          $condition_deleted = TRUE;
        }
      }

      // Exclude deleted data unless we have a condition on it.
      if (!$condition_deleted && $row->deleted) {
        $match = FALSE;
      }

      if ($match) {
        if (!isset($skip) || $skipped >= $skip) {
          $cursor++;
          // If querying all revisions and the entity type has revisions, we need
          // to key the results by revision_ids.
          $entity_type = entity_get_info($row->type);
          $id = ($load_current || empty($entity_type['entity keys']['revision'])) ? $row->entity_id : $row->revision_id;

          if (!isset($return[$row->type][$id])) {
            $return[$row->type][$id] = entity_create_stub_entity($row->type, array($row->entity_id, $row->revision_id, $row->bundle));
            $entity_count++;
          }
        }
        else {
          $skipped++;
        }
      }
    }
    $rows_count++;

    // The query is complete if we walked the whole array.
    if ($count != FIELD_QUERY_NO_LIMIT && $rows_count >= $rows_total) {
      $cursor = FIELD_QUERY_COMPLETE;
    }
  }

  return $return;
}