Jump to content
pwFoo

Extend and search "Selectors" object

Recommended Posts

I played with the Selectors object and would like to use it for a special use case...

That is a custom Selectors object with dummy data converted to an php array to see the structure (Selectors object is a WireArray with "fields" added)

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [field] => seg1
                    [value] => val1
                    [not] => 
                    [group] => 
                    [quote] => 
                    [forceMatch] => 
                )
            [1] => Array
                (
                    [field] => seg2
                    [value] => val2
                    [not] => 
                    [group] => 
                    [quote] => 
                    [forceMatch] => 
                )
        )
)

 

But instead of "seg1" the field name is "field" with value "seg1" (= my field name).

 

So I can't search the Selectors WireArray (= custom WireArray with added Selectors objects) with PW "find('seg1=val1')", Is there a way to search with "find()" or build a simple wrapper to make the elements searchable / filterable with find()?

Share this post


Link to post
Share on other sites
52 minutes ago, pwFoo said:

custom Selectors object with dummy data

To clearify. It is a PW Selectors object (not a custom class!) with dummy data.

Because I need a selector (object) and some custom additional properties. Would be nice to just use the Selectors object with added properties searchable with find.

 

And a missing part here.

I tried to add custom properties to Selectors object, but get an error message:

  • $this->property = 'value'; // ignored
  • $this->set('property', 'value'); // error see below
  • $this->addHookProperty(...); // error see below
Error: Exception: Item 'selector' set to ProcessWire\ApiRoute is not an allowed type (in C:\xampp\htdocs\pw\wire\core\WireArray.php line 450)

 

Share this post


Link to post
Share on other sites

Is there a way to redirect properties / fields to make them searchable by find($selector)? For example redirect a call to "$obj->prop<X>" to a function and return the needed value? Would that help to get find('prop1=myValue') work?

 

I tried addHookProperty, but isn't usable for find().

$wa = new WireArray();
$obj1 = new Selectors('seg1=val1,seg2=val2');
$obj1->addhookProperty('seg1', function($event){
    $event->return = $event->object->getSelectorByField('seg1');
});
echo $obj1->seg1; // "seg1=val1"
$wa->add($obj1);
$output = $wa->find('seg1=val1');   // object with items "count 0"

 

Share this post


Link to post
Share on other sites

Looks like I should use a custom not Selectors object / class to build that. No working solution found / known.

Share this post


Link to post
Share on other sites
On 1/2/2019 at 11:46 PM, pwFoo said:

So I can't search the Selectors WireArray (= custom WireArray with added Selectors objects) with PW "find('seg1=val1')", Is there a way to search with "find()" or build a simple wrapper to make the elements searchable / filterable with find()?

It's not fully clear to me what you're asking, but you can use find() to get individual Selector objects from a Selectors object by both field and value.

2019-01-05_191030.thumb.png.fc410bfcbb74f7aa239a732c88da54f3.png

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 Anders
      I want to allow full text search on my site. There is a very nice solution that comes right out of the box:
      $selector = "title|body~=$q, limit=50"; This works, but to make it even better I would want to give higher weight to pages where the search term occurs in the title, than if it just occurs in the body. After all, a page with the title "Wine from France" is probably the best match for the search "france wine". How do I accomplish this in ProcessWire?
      I can see three possible paths, but I am not very fond of any of them:
      Do a direct SQL query, circumventing the API, along these lines. But I would prefer to abstract away the database layout if at all possible. Use something like ElasticSearch, but to be honest that would be to complicated to set up and maintain in the long run. Make multiple lookups, first for matches in the title, then for matches in the body, and merge and sort in PHP. My suspicion is that this would get complicated quite quickly. For instance, how do you deal with a page that has two of the three search terms in the title and the third in the body? Is there a magic option four I should look into? Or are any of the above options better than the others? Any input is welcome!
    • By Roberts R
      I have simple page structure:
      category subcategory simple-product simple-product simple-product simple-product subcategory .... category2 so Im at category page and running
      $pages->find("template=simple-product, has_parent=$page, limit=8"); and it returns 0 pages
       
      but this returns 4 pages
      $pages->find("template=simple-product, has_parent=$page"); Can someone explain why limit does not work here?
      EDIT:
      So I did some tests and it seems that any limit=n where n is >= actual page count that is possible ... selector return 0 results.
    • By schwarzdesign
      ProcessWire added the numReferences property in 3.0.107, which returns the count of all pages having a reference to the page object. However, apparently it's not possible to use this property inside a selector. I'm trying to find all pages that are referenced at least once:
      $pages->find('template=service, numReferences>0'); This throws an error: "Field does not exist: numReferences".
      Is there another way to filter by the number of references? Of course, I could manually filter the results of the find query, but that feels overly complicated. Ideally, I would also like to filter the number of references from a specific field; that is, find all pages that are referenced at least once in one specific page reference field.
      Is there a way to do this? I guess supporting numReferences in selectors would be a feature request - if so, is it feasible?
      Thanks!
       
    • By jds43
      Hello, I have a Page Reference by template radio button field to promote a certain page (only two options), but I'd like to target the page that isn't selected. This would be used dynamically throughout the site.
      Does anyone know how I could accomplish this? Would I use something like remove() or not()?
       
    • By iipa
      Hi!
      I have two Page Reference fields: Category and Subcategory. Category is parent of Subcategory, and can have 0 to n Subcategories. Fields are selected by dropdown selection.
      I'm trying to achieve following logic:
      1. Select Category (obviously works)
      2. Change Subcategory options based on selected Category (this works)
      3. Hide the Subcategory field if Category has no Subcategories (to prevent weird dropdown with nothing to select)
      I have tried to investigate two possible alternatives to achieve step 3:
      A) Make dynamic condition into Subcategory selection's "Only visible if..." field
      B) Make a hook that fires when Category changes, then hides Subcategory field if Category has no children
      So far I haven't been successful in either. Conditions I've tried always lead to hidden field, and I haven't been able to find set up a hook that fires on field change.
      So here's the question (finally): Is there a way to alter field visibility in API, for example in ready.php or inside a hook? In API I could loop through categories, find the ones that have children and then make a selector based on their IDs.
×
×
  • Create New...