Jump to content

Selector for Repeater entry


Ksenia
 Share

Recommended Posts

Hello! 

I am still exploring how PW works and I am facing this issue:

I want to create a selector for a field that is PageReference type (AsmSelect) inside the repeater field. It goes like this:

Relation to Document:

Select: (you pick Document/Documents) 

Type: (you pick type/types of relation which are also pages)

1064081253_Screenshot2021-11-25at12_26_08.thumb.png.05ca0c4ff2b4b4ba041ee2e83615a2b0.png

This is how I find the pages that have current page in the Repeater field, it works:

$selector_doc = "template=Document, relation_document_document.relation_document_document_select=$page->name";

But the problem is with how to filter based on repeater entry, meaning if there are multiple entries (its a different repeater, but identical):

2093927960_Screenshot2021-11-25at13_07_56.thumb.png.19e40cba125b3aced43fccdbf4d9861e.png

How do I say "if SELECT == '$page->name', then print it "select and type"?

Because now it just loops through all of the Relation entries, but I want it to show me only the one that has current page in Select:

296447836_Screenshot2021-11-25at13_17_11.thumb.png.dd6ddb59281f523c2d1d51174ea9e7b4.png

My code: 

$techniques = new PageArray();
$selector_tech = "template=Technique, relation_technique_substance.relation_technique_substance_select=$page->name";
$techniques->add($pages->find($selector_tech));
if (sizeof($techniques) > 0) {
    echo "<div class='field-cont'>";
    echo "<div class='field-name'>Linked techniques:</div>";
    echo "<div class='trick'>";
    echo "<div class='field-lists tech'>";
    foreach ($techniques as $tech) {
        foreach ($tech->relation_technique_substance as $relation_technique_substance) {
            $arraySelect_trick = $relation_technique_substance->relation_technique_substance_select;
            $arrayType_trick = $relation_technique_substance->relation_technique_substance_type;
            echo "<div class='field-relation'>";
                ////here needs to be If()///
                echo "<div class='select'>";
                echo "<li><a href='$tech->url'>$tech->title</a>";
                echo "</div>";
                echo "<div class='type'>";
                foreach ($arrayType_trick as $item) {
                    echo "<span>$item->title</span></li> ";
                }
                echo "</div>";
                 /// end if
            echo "</div>";

        }
    }

    echo "</div></div></div>";

}

 

Best,

Ksenia

 

Link to comment
Share on other sites

  • Ksenia changed the title to Selector for Repeater entry

A few suggestions:

1. It's best to match using a page's ID rather than its name, because the ID is globally unique but the name is not. So instead of...

$pages->find("template=Technique, relation_technique_substance.relation_technique_substance_select=$page->name")

...you can do...

$pages->find("template=Technique, relation_technique_substance.relation_technique_substance_select=$page->id")

...and because the string value of a Page object is its ID it's typical to do...

$pages->find("template=Technique, relation_technique_substance.relation_technique_substance_select=$page")

 

2. You don't need to create a PageArray and add pages to it because $pages->find() already returns a PageArray. So this...

$techniques = new PageArray();
$selector_tech = "template=Technique, relation_technique_substance.relation_technique_substance_select=$page->name";
$techniques->add($pages->find($selector_tech));

...can be simplified to...

$techniques = $pages->find("template=Technique, relation_technique_substance.relation_technique_substance_select=$page");

 

3. PageArrays have a count() method/property, so rather than...

if (sizeof($techniques) > 0) {

...try...

if ($techniques->count) {

 

4. To answer your main question, the value of a Repeater field is a (Repeater)PageArray so you can use ->find($selector) to search within it.

foreach ($tech->relation_technique_substance->find("relation_technique_substance_select=$page") as $relation_technique_substance) {

Now any Repeater items where relation_technique_substance_select does not include $page will be excluded.

  • Like 1
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...