WireArray filterData selector

Recommended Posts

dscONE    1

Found myself needing to filter repeater items based upon start and end date fields.  These fields aren't required, so repeater items with no dates should be included and those with dates (or a just one date -- start or end) should be evaluated for inclusion.

I slowly figured out that I was dealing with in-memory filtering (of a RepeaterPageArray) and not a database-driven operation.  So, that eliminated the possibility of using or-groups in my selector.  I still thought I could use the 'filter' and 'not' functions to get the job done.  Turns out the way selectors are handled for in-memory filtering is quite different from database-driven selectors.

Here is what I want(ed) to do to filter the dates:

// Start date not specified or in the past
// Not when end date is specified and has passed
$repeater->not('end_time>0, end_time<'.$now);

The first one worked exactly as expected.  The second it where I ran into problems.  The 'filter' function (which calls the 'filterData' function) takes a $selector argument.  From everything I read about selectors, I assumed that the entire selector would have to match.  In other words, each comma represented an 'and' scenario.  Turns out that each separate comma-separated-selector in the $selector as a whole gets evaluated independently.  In the case of the 'filterData' function, the elements are removed if any one of those selectors matches.  Essentially, we have an 'or' scenario instead of an 'and' scenario.

In my case above, there was no way for me to filter for a non-empty date AND one that occurs before a given date...at least that I could think of.

So, my main question is if the filter (and related) function should operate on the entire selector passed to the function instead of its individual parts in some sequence.  That is what I would have assumed a selector to do.

In my project, altering the segment of the WireArray::filterData function solved my problem for now.  ...at least till I forget about it and overwrite it when I upgrade next.  Here is the code I changed within the function

// now filter the data according to the selectors that remain
foreach($this->data as $key => $item) {
	$filter_item = true;
	foreach($selectors as $selector) {
		if(is_array($selector->field)) {
			$value = array();
			foreach($selector->field as $field) $value[] = (string) $this->getItemPropertyValue($item, $field);
		} else {
			$value = (string) $this->getItemPropertyValue($item, $selector->field);
		if($not === $selector->matches($value))
		$filter_item = false;
	if($filter_item && isset($this->data[$key])) {
		$this->trackRemove($this->data[$key], $key);

I would love to hear what you all think.  If I have misunderstood something, then I would welcome a different solution.

  • Like 1

Share this post

Link to post
Share on other sites
Robin S    2,487

Hi and welcome @dscONE!

Yes, I do think the behaviour you have discovered is unexpected. Just to reiterate in a simplified way what I think you are saying....

The expected behaviour is that for any selector, the combined results of $pagearray->find($some_selector) and $pagearray->not($some_selector) should equal the entire PageArray. But this is not the case, as can be demonstrated with a PageArray containing three pages titled "red car", "blue car", "green truck"...

$matches = $pagearray->find("title~=car"); // "red car", "blue car"
$not_matches = $pagearray->not("title~=car"); // "green truck"
// So far, so good

$matches = $pagearray->find("title~=car, !title~=red"); // "blue car"
$not_matches = $pagearray->not("title~=car, !title~=red"); // expected "red car", "green truck" but got unexpected empty PageArray

Would you please report this issue over at the PW issues GitHub repo? https://github.com/processwire/processwire-issues/issues



  • Like 1

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By pppws
      hey there,
      i have a collection (parent page) of persons (child pages of 'collection'). each person has several fields. two of them are repeater fields where the person can enter their 'jobs' and 'recidencies'. i'm trying to build a list of entries which looks like:
      Peter Maria Paul … Doctor
      Eva Julia William … for the first 5 persons everything worked smoothly. but now that i've reached about 20 entries the server slows down and i'm wondering if my loop is somehow cluttred up.

      <?php $langname = $user->language->title; //get current user language?> <?php $persons = $pages->get('/collection')->children->filter("lang=$langname") // get all children for current user language ?> <section class="profession"> <h1>professions</h1> <?php foreach ($persons as $child): ?> <?php foreach ($child->professions as $profession): // the repeaterfield is called: professions, the field itself is profession?> <?php $profAll[] = $profession->profession // store all entries; $profUnique = array_unique($profAll) // only unique entries ; sort($profUnique) // sort the entries; ?> <?php endforeach; ?> <?php endforeach; ?> <ul style="column-count: 2;"> <?php foreach ($profUnique as $profLetter): // loop through all professions ?> <li style="font-size: 2rem; list-style-type: none;" class="letter"><?= $profLetter // output one profession e.g. Actor?></li> <?php foreach ($persons->find("professions.profession=$profLetter")->sort('givenname') as $person): // find all persons who have the profession Actor ?> <li><a class="ajax" href="<?= $person->url ?>"><?= $person->givenname // output the name of person who fits the profession ?></a></li> <?php endforeach; ?> <?php endforeach; ?> </ul> </section>  
      is there a way to make this request faster? (i'll have at least two of them on the same page)
    • By pppws
      hey there,
      i'm quite new to processwire but i'm having a great experience using it!
      right now i hit a point where i can't help myself out with google/searchfunction.
      to sketch the basic functions of my page:
      visitors can enter their e-mail in a form. when doing this pw creates a new page, using the e-mail's md5 hash as a name. the url of the page is sent to the user. the user now can edit the newly created page and can fill out fields like: name and year of birth (thanks to the docs those two already work like a charm!) but there are other fields like "residencies", which are repeaterfields containing three fields: city (text), country (text), current (checkbox). at my current version i can populate a new repeater field easily by using this code:
      $location = $page->locations->getNew(); $location->location = Munich; $location->country = Germany; $location->current = 1; $location->save(); $page->locations->add($location); now i want to populate the new reapter fields from input fields (using the simple form api). which leads me to my questions:
      1. can i somehow group/merge input fields to one "repeater input fields" right now i have:
      // create a text input -> locations $field = $modules->get("InputfieldText"); $field->label = __('City'); $field->attr('id+name','location'); $field->required = 1; $form->append($field); // append the field to the form // create a text input -> country $field = $modules->get("InputfieldText"); $field->label = __('Country'); $field->attr('id+name','country'); $field->required = 1; $form->append($field); // append the field to the form // create a checkbox -> locations $field = $this->modules->get('InputfieldCheckbox'); $field->attr('name', 'location_current'); $field->attr('autocheck', 1); $field->attr('uncheckedValue', 0); $field->attr('checkedValue', 1); $field->attr('value', $this->fname); $form->append($field); // append the field to the form 2. my desired layout for the form looks like this:

      by hitting the + button a new line shows. i'm wondering what's the best practice here. can is somehow use the above mentioned code itself as a repeater, or do i have to create several (uniqe) input fields in advance and hide them afterwards (javascript)?
      any help is appreciated!
    • By patricktsg
      Something probably off with my loop, the code below is on my "entries.php" template page, it's outputting the title and description of each entry fine but the repeater fields just repeats the first set of images from entry 1 throughout the loop.
      <?php // Functions $entries = $page->children; $entry_array = array(); $image_array = array(); foreach ($entries as $entry) { // Define the fields wanted $title = $entry->title; $description = $entry->entry_description; $images = $entry->images; foreach ($images as $image) { $imageUrl = $image->url; // make a thumb version $imageDescription = $image->description; // Build array from repeater $image_array[] = array( 'image_url' => $imageUrl, 'image_description' => $imageDescription, ); } $pageUrl = $entry->httpUrl; // Build the array from the fields $entry_array[] = array( 'title' => $title, 'description' => $description, 'images' => $image_array, 'page_url' => $pageUrl, ); } // Json encode the array $entries = json_encode($entry_array, true); // Dump the data // echo $data; ?>  
    • By muco
      Hi guys,
      I created an image field (studio) which can contain up to 30 images. Then I created a repeater field (studioimages) and added the studio field.
      I added some images to the repeater:

      Now I loop through the repeater, but I only got the first image or no image:
      foreach($page->studioImages as $studioImage) { echo "<div class='swiper-slide'> <div class='image-gallery'> <a href='{$studioImage->studio->url}'> <div style='background-image: url({$studioImage->studio->url});'> </div> </a> </div> </div>"; } I also tried "$studioImage->studio->first()->url" and "$studioImage->studio" but nothing works. Can you tell me what is wrong?
    • By nuel
      hi there
      i'm new to processwire, please cut me some slack if the answer is already out there..
      i am "processwiring" a non-cms page that i made, containing many repeating so called "station"-elements with fields such as date, location, miles, optional links, photo upload etc. they were listed and created via php and stored in an xml file until now:
      <stations> <station> <date>17.8.2017</date> <location>Exil, Zurich</location> <miles>1234</miles> ... </station> <station> ... </station> </stations> i already created a working repeater field with the needed fields and successfully created the first two elements... but there are many more!
      what is the easiest and quickest way to batch create new repeater fields with the node element content of my xml file?
      otherwise i would just have to type the whole sh..t again which i am too damn lazy to do
      thanks in advance