Jump to content

Getting the page title a repeater field is on from a pageField


a-ok
 Share

Recommended Posts

Hi folks,

I have a repeater set up on a 'fabrics' page. When you add a fabric, you then add an image and a colour (per row on a repeater); the colour field is a pageField which lists out all the colours from the Colours section. See screenshot for an example. post-2553-0-76415800-1466168632_thumb.pn

Across the site, where 'related fabrics' come into play, the client will choose, via a pageField, all the relevant fabrics and it'll take the first row of the repeater (image and assigning colour) and display it. See second screenshot as an example. post-2553-0-51105000-1466168676_thumb.pn This is all good and works BUT there is one section of the site 'In-Situ' where the client would have to choose a specific colour from a fabric (and not just the fabric).

As the image/colour combos are set up as a repeater (which are added as pages in the CMS under 'Admin > Repeaters' (which is amazing) I can simply tell a pageField, using a custom selector, to show each 'row' from across the site (which would return all the images and colour options for each fabric).

template=repeater_collections_detail_images, include=hidden, sort=name

This works a treat. I can then use the custom page label format which would return the parent title of the page it's on and then the title of the colour chosen.

{parent.title} – {collections_detail_image_colour.title}

I have one issue, however. The parent.title isn't user-friendly and is rather the canonical title of the page, not the actual title of the page, so looks more like a name than a title. See the third and fourth screenshot as an example. post-2553-0-27627800-1466168718_thumb.pnpost-2553-0-01989400-1466168719_thumb.pn

The only thing I can think of is that these repeaters and their rows are on these specific fabric pages, right? So is there a way for the custom page label format to return the title of THAT page and not the repeater page? See my final screenshot for what I mean. post-2553-0-87329500-1466168795_thumb.pn The repeater has a path, for example, .../for-page-1080/ and the page 1080 that it is referring to is the page of the title I am trying to get.

Any ideas? I realise this is a little long-winded but any help or advice would be appreciated.

Link to comment
Share on other sites

I think what I could do is include a hidden field in the repeater and write a addHookAfter to fill this hidden field with the page title on save, then just use that field? Seems a bit odd? But maybe not?

Link to comment
Share on other sites

Only thing I'm struggling with is trying to add a hook to a repeater set the value of a field per repeater row.

I thought this would've worked

$pages->addHookAfter('Pages::saveReady', function($event) {
    $page = $event->arguments('page');
    $repeater = $page->collections_detail_images;
    foreach ($repeater as $row) {
        //$page->message($row);
        $row->collections_detail_image_page_title = 'TEST';
    }
});
Link to comment
Share on other sites

$pages->addHookAfter('Pages::saveReady', function($event) {
    $page = $event->arguments(0);
    $repeater = $page->collections_detail_images;
    foreach ($repeater as $row) {
        $row->of(false);
         $row->collections_detail_image_page_title = $page->title;
        $row->save();
    }
});

This is what worked.

  • Like 1
Link to comment
Share on other sites

  • 4 years later...

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

×
×
  • Create New...