Forum

Thread tagged as: Question, Problem

Pull specific item from a shared region using a dataselect field

Hi there,

I have a shared region called Testimonials. My client wants to pull out specific testimonial from that shared region on certain inner pages. Rather than making them enter duplicates in a new region, I wanted to know if it's possible to populate a dataselect and give them control to select which one they'd like.

I have a template for my new region called Testimonials Specific. Which contains:

<perch:content type="dataselect" id="testimonials" label="Testimonials" region="Testimonials" order="1" options="name" required="true" page="*" />

This works in that it populates the dataselect with the name of the client from each testimonial for the user to select. The next part I'm stuck on, is how - using this - I can use the ID of the item to pull out the entire contents of said item. I am guessing it'd be perch_content_custom. Any pointing in the right direction would be appreciated if anyone has done this before!

Mathew Doidge

Mathew Doidge 2 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You'll need to get the region back with skip-template to get the id, then query for the content, pass that back into the template and redisplay the region.

Or use relationships in Runway.

Hi Drew,

I've been working on this trying to use what I think you were saying I needed to do. Can I get a hot or cold on the following:

perch_content_custom('Testimonials', array(
    'template' => 'testimonials.html',
    'each' => function($item) {
        $item['testimonials'] = perch_content_custom('Testimonials Specific', array(
        'page' => '*',
        'filter' => 'tm_name',
        'match' => 'eq',
        'value' => $item['slug'],
        ), true);
        return $item;
    },
));

So - in my head at least - I am calling the testimonials region and filtering through each trying to match the one that relates to the selection in the dataselect from the Testimonials Specific region. My Testimonials region contains the actual testimonials themselves and the fields and the Testimonials Specific just contains the dataselect pulling in the name of each testimonial giver. Not sure if I am wildly off here.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Is that working?

Drew McLellan said:

Is that working?

Seems to just pulling everything out. Doesn't seem to be filtering for some reason

Drew McLellan

Drew McLellan 2638 points
Perch Support

The inner query isn't filtering?

Doesn't seem to be. I assume that was the way in which I could query against the selection made on the dataselect.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you tested your assumptions within the inner part?

Drew, the value passed into $item['testimonials'] = - What should this value be? I noticed with <perch:showall /> that testimonials shows up in the table blank. But I can see it pulling in things like _id and slug.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Is $item['slug'] what you're expecting it to be?

If I run it like

perch_content_custom('Testimonials', array(
    'template' => 'testimonials.html',
    'each' => function($item) {
        $item['slug'] = perch_content_custom('Testimonials Specific', array(
        'page' => '*',
        'filter' => 'tm_name',
        'match' => 'eq',
        'value' => $item['slug'],
        ), true);

        return $item;
    },
));

I can see the output doesn't change, i.e. all still being pulled. But in the show all table, slug goes blank when slug gets used.

$item['slug'] = perch_content_custom('Testimonials Specific', array(

Can't understand why it's clearing the value

Duncan Revell

Duncan Revell 78 points
Registered Developer

$item['slug'] = perch_content_custom('Testimonials Specific', array(...), true);

is setting $item['slug'] to be nothing, because the 'Testimonials Specific' query is returning nothing.

You need to isolate that query and get it returning something - run this all by itself:

$test = perch_content_custom('Testimonials Specific', array( 
'page' => '*', 
'filter' => 'tm_name', 
'match' => 'eq', 
'value' => 'put a value in here you would expect work', 
), true);
print_r($test);

I'm guessing that your 'filter' => field is wrong - is 'tm_name' used as an id in your Testimonials Specific template? Get that query working by itself, then you can use it in your bigger query.

Although, I don't think you need to be using 'each' for this...