Jump to content

Using context-dependent labels for fields in repeater labels


MarkE
 Share

Recommended Posts

Repeater fields allow you to customise the labels - eg '#n {title}' where title is a field within the repeater field. One of my repeaters includes a (3 position) toggle field 'motif_toggle_position': the base field has just 'Yes', 'No' and 'Other' labels for the three positions, but I have modifield this in the repeater field template context to be 'Left', 'Centre' and 'Right'. It is those context-dependent labels that I want to appear in my repeater field labels. '#n {motif_toggle_position}' gives me the base field labels, not the context-dependent labels; how do I achieve that?

My friendly AI suggests using a hook on ProcessPageEdit::buildForm (hooks with subtle syntactical errors seem to be its solution to most problems ?) but I was hoping there would be a more 'natural' way. In fact, given that the repeater label is within the repeater field template context, I don't understand why the base field labels are given in preference to the context ones anyway.

Link to comment
Share on other sites

Can you try the following ready.php snippet (can't test it myself right now):

<?php

wire()->addHookAfter('FieldtypeRepeater::wakeupValue', function(HookEvent $event) {

  $field = $event->arguments('field');
  $page = $event->arguments('page');
  // The return value of wakeupValue is a RepeaterPageArray or subclass of it
  $repPageArray = $event->return;
  
  if($field->hasContext($page)) {
    $field = $field->getContext($page);
    $repPageArray->setField($field);
  }
  
});

 

  • Like 1
Link to comment
Share on other sites

5 hours ago, BitPoet said:

Can you try the following ready.php snippet

Thanks @BitPoet. I can't get that to work, but this does work:

		$this->wire()->addHookAfter('ProcessPageEdit::buildForm', function(HookEvent $event) {
			$form = $event->return;
			foreach($form->getAll() as $f) {
				// Check if this is the repeater field you want to modify
				if($f instanceof InputfieldRepeater && $f->name == 'motif_image_component') {
					foreach($f->value() as $repeaterItem) {
						$toggleField = $repeaterItem->getField('motif_toggle_position');
						$value = $repeaterItem->motif_toggle_position;
						if($toggleField) {
							$yesLabel = $repeaterItem->fieldgroup->getField($toggleField, true)->yesLabel;
							$noLabel = $repeaterItem->fieldgroup->getField($toggleField, true)->noLabel;
							$otherLabel = $repeaterItem->fieldgroup->getField($toggleField, true)->otherLabel;
							$toggleField->set('yesLabel', $yesLabel);
							$toggleField->set('noLabel', $noLabel);
							$toggleField->set('otherLabel', $otherLabel);
						}
					}
				}
			}
		});

I guess it would be nice to make it generic - which your hook tries to be. I can't see how to do that with a buildForm hook. However, I wonder whether it is worth raising an issue here, since surely all labels for a repeater field should be in the repeater field template context?

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...