summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authorAngie Byron <webchick@24967.no-reply.drupal.org>2010-11-14 22:07:57 +0000
committerAngie Byron <webchick@24967.no-reply.drupal.org>2010-11-14 22:07:57 +0000
commit0c33e8638845d4e8617d92776e95eac86c3e2e2a (patch)
tree74caee22e14bde6f53d84f9ca11b889a19e1206d /includes
parent4575c982a2348cc2f9f5c6f6ee5531fe5a40f644 (diff)
downloadbrdo-0c33e8638845d4e8617d92776e95eac86c3e2e2a.tar.gz
brdo-0c33e8638845d4e8617d92776e95eac86c3e2e2a.tar.bz2
#885014 by bojanz, chx: Fixed add missing pager and tablesort query support to EntityFieldQuery.
Diffstat (limited to 'includes')
-rw-r--r--includes/entity.inc104
-rw-r--r--includes/pager.inc2
2 files changed, 104 insertions, 2 deletions
diff --git a/includes/entity.inc b/includes/entity.inc
index 7f9a78da7..1c3e7494e 100644
--- a/includes/entity.inc
+++ b/includes/entity.inc
@@ -419,6 +419,16 @@ class EntityFieldQuery {
const RETURN_ALL = NULL;
/**
+ * TRUE if the query has already been altered, FALSE if it hasn't.
+ *
+ * Used in alter hooks to check for cloned queries that have already been
+ * altered prior to the clone (for example, the pager count query).
+ *
+ * @var boolean
+ */
+ public $altered = FALSE;
+
+ /**
* Associative array of entity-generic metadata conditions.
*
* @var array
@@ -462,6 +472,15 @@ class EntityFieldQuery {
public $range = array();
/**
+ * The query pager data.
+ *
+ * @var array
+ *
+ * @see EntityFieldQuery::pager()
+ */
+ public $pager = array();
+
+ /**
* Query behavior for deleted data.
*
* TRUE to return only deleted data, FALSE to return only non-deleted data,
@@ -799,6 +818,68 @@ class EntityFieldQuery {
}
/**
+ * Enable a pager for the query.
+ *
+ * @param $limit
+ * An integer specifying the number of elements per page. If passed a false
+ * value (FALSE, 0, NULL), the pager is disabled.
+ * @param $element
+ * An optional integer to distinguish between multiple pagers on one page.
+ * If not provided, one is automatically calculated.
+ *
+ * @return EntityFieldQuery
+ * The called object.
+ */
+ public function pager($limit = 10, $element = NULL) {
+ if (!isset($element)) {
+ $element = PagerDefault::$maxElement++;
+ }
+ elseif ($element >= PagerDefault::$maxElement) {
+ PagerDefault::$maxElement = $element + 1;
+ }
+
+ $this->pager = array(
+ 'limit' => $limit,
+ 'element' => $element,
+ );
+ return $this;
+ }
+
+ /**
+ * Enable sortable tables for this query.
+ *
+ * @param $headers
+ * An EFQ Header array based on which the order clause is added to the query.
+ *
+ * @return EntityFieldQuery
+ * The called object.
+ */
+ public function tableSort(&$headers) {
+ // If 'field' is not initialized, the header columns aren't clickable
+ foreach ($headers as $key =>$header) {
+ if (is_array($header) && isset($header['specifier'])) {
+ $headers[$key]['field'] = '';
+ }
+ }
+
+ $order = tablesort_get_order($headers);
+ $direction = tablesort_get_sort($headers);
+ foreach ($headers as $header) {
+ if (is_array($header) && ($header['data'] == $order['name'])) {
+ if ($header['type'] == 'field') {
+ $this->fieldOrderBy($header['specifier']['field'], $header['specifier']['column'], $direction);
+ }
+ else {
+ $header['direction'] = $direction;
+ $this->order[] = $header;
+ }
+ }
+ }
+
+ return $this;
+ }
+
+ /**
* Filters on the data being deleted.
*
* @param $deleted
@@ -911,6 +992,10 @@ class EntityFieldQuery {
public function execute() {
// Give a chance to other modules to alter the query.
drupal_alter('entity_query', $this);
+ $this->altered = TRUE;
+
+ // Initialize the pager.
+ $this->initializePager();
// Execute the query using the correct callback.
$result = call_user_func($this->queryCallback(), $this);
@@ -966,7 +1051,6 @@ class EntityFieldQuery {
throw new EntityFieldQueryException(t('For this query an entity type must be specified.'));
}
$entity_type = $this->entityConditions['entity_type']['value'];
- unset($this->entityConditions['entity_type']);
$entity_info = entity_get_info($entity_type);
if (empty($entity_info['base table'])) {
throw new EntityFieldQueryException(t('Entity %entity has no base table.', array('%entity' => $entity_type)));
@@ -1039,6 +1123,24 @@ class EntityFieldQuery {
}
/**
+ * Get the total number of results and initialize a pager for the query.
+ *
+ * This query can be detected by checking for ($this->pager && $this->count),
+ * which allows a driver to return 0 from the count query and disable
+ * the pager.
+ */
+ function initializePager() {
+ if ($this->pager && !$this->count) {
+ $page = pager_find_page($this->pager['element']);
+ $count_query = clone $this;
+ $this->pager['total'] = $count_query->count()->execute();
+ $this->pager['start'] = $page * $this->pager['limit'];
+ pager_default_initialize($this->pager['total'], $this->pager['limit'], $this->pager['element']);
+ $this->range($this->pager['start'], $this->pager['limit']);
+ }
+ }
+
+ /**
* Finishes the query.
*
* Adds tags, metaData, range and returns the requested list or count.
diff --git a/includes/pager.inc b/includes/pager.inc
index 0e417f656..69c2759b3 100644
--- a/includes/pager.inc
+++ b/includes/pager.inc
@@ -20,7 +20,7 @@ class PagerDefault extends SelectQueryExtender {
*
* @var int
*/
- static protected $maxElement = 0;
+ static $maxElement = 0;
/**
* The number of elements per page to allow.