I'm developing against a service that's returning >3,000 items. For dev purposes, I'd like to limit the number of items returned. I've added a setting for item count.

In sourceForm()

    $form['querypath']['count'] = array(
      '#type' => 'textfield',
      '#title' => t('Count'),
      '#description' => t('How many items to return, 0 for all items'),
      '#default_value' => isset($source_config['count']) ? $source_config['count'] : '0',
    );

and

  public function configDefaults() {
    return array(
      'context' => '',
      'count' => 0, // jh added
      'sources' => array(),
      'debug' => array(),
      'attrs' => array(),
      'rawXML' => array(),
    );
  }

In parse(), set $count =0 before the foreach ($context as $item) { loop, and before the end of the loop:

      $count++;
      if (!empty($this->source_config['count']) && $count >= $this->source_config['count']) {
        break; // Abandon loop if we have sufficient items
      }

Comments

jonathan_hunt’s picture

Diff for above

diff --git a/modules/feeds_querypath_parser/FeedsQueryPathParser.inc b/modules/feeds_querypath_parser/FeedsQueryPathParser.inc
index fabd73d..80be2c6 100644
--- a/modules/feeds_querypath_parser/FeedsQueryPathParser.inc
+++ b/modules/feeds_querypath_parser/FeedsQueryPathParser.inc
@@ -53,6 +53,7 @@ class FeedsQueryPathParser extends FeedsParser {
     $context = qp($doc, $this->source_config['context'], $opts);
     $this->debug($context, 'context');
 
+    $count = 0;
     foreach ($context as $item) {
       $parsed_item = $variables = array();
       foreach ($this->source_config['sources'] as $source => $query) {
@@ -70,6 +71,10 @@ class FeedsQueryPathParser extends FeedsParser {
         }
       }
       $result->items[] = $parsed_item;
+      $count++;
+      if ($this->source_config['count'] != 0 && $count >= $this->source_config['count']) {
+        break; // Abandon loop if we have sufficient items
+      }
     }
     return $result;
   }
@@ -149,6 +154,14 @@ class FeedsQueryPathParser extends FeedsParser {
       '#default_value' => isset($source_config['context']) ? $source_config['context'] : '',
       '#maxlength' => 1024,
     );
+
+    $form['querypath']['count'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Count'),
+      '#description' => t('How many items to return, 0 for all items'),
+      '#default_value' => isset($source_config['count']) ? $source_config['count'] : 0,
+    );
+
     $form['querypath']['sources'] = array(
       '#type' => 'fieldset',
     );
@@ -236,6 +249,7 @@ class FeedsQueryPathParser extends FeedsParser {
   public function configDefaults() {
     return array(
       'context' => '',
+      'count' => 0,
       'sources' => array(),
       'debug' => array(),
       'attrs' => array(),