Jump to content

Repeater matrix – how to insert content from another field


Tyssen
 Share

Recommended Posts

I have two fields on a page, one a normal repeater and the other a repeater matrix. What I want to be able to do is allow the content editors to choose to insert the content from the normal repeater field in between other content in the repeater matrix field.

I thought I could insert some text into the repeater matrix and then do a conditional in my templates to check for that string and insert the other field's output there.

My template looks like this at the moment:

home.php

$inserted = 'Data from the normal repeater field created with a foreach loop';

$page->render('repeater_matrix_field');

repeater_matrix_field.php

foreach($value as $item) :
  echo $item->render();
endforeach;

matrix_body.php

if($page->heading=='Replace this') :
  echo $inserted;
else:
  echo '<h2>'.$page->heading.'</h2>';
endif;

I don't get any output I guess because the $inserted variable doesn't get passed from home.php to the other templates.

So is there a way to do that, or is there a better way to achieve what I'm trying to do?

Link to comment
Share on other sites

You could create a Page Reference field that has the Repeater items as selectable pages. Hook for selectable items in /site/ready.php:

$wire->addHookAfter('InputfieldPage::getSelectablePages', function(HookEvent $event) {
	$page = $event->arguments(0);
	if($event->object->hasField == 'select_repeater_item') {
		$event->return = $page->repeater;
	}
});

Then create a matrix type that has this Page Reference field.

2019-05-15_164055.png.68f781b96968b797cb26dc4df830e00a.png

But if the repeater items are to be output among the matrix items then I'm not sure why the repeater exists as a separate field. Wouldn't it be better to just have a matrix field alone and replace the repeater with a matrix type?

  • Like 1
Link to comment
Share on other sites

51 minutes ago, Robin S said:

But if the repeater items are to be output among the matrix items then I'm not sure why the repeater exists as a separate field. Wouldn't it be better to just have a matrix field alone and replace the repeater with a matrix type?

Every page is going to use the repeater matrix but only the home page is going to need to have the repeater as well so I was trying to find a solution that would keep the repeater option out of the matrix field, but I guess it's not that big a deal really, so I'll go with that.

Link to comment
Share on other sites

1 minute ago, Tyssen said:

I was trying to find a solution that would keep the repeater option out of the matrix field

You might find this module useful for that: https://modules.processwire.com/modules/restrict-repeater-matrix/

Quote

Example hooks

Prevent the matrix type "images_block" from being added to "my_matrix_field" in a page with the "basic-page" template:


$wire->addHookAfter('RestrictRepeaterMatrix::checkRestrictions', function(HookEvent $event) {
    $field = $event->arguments('field');
    $page = $event->arguments('page');
    $type_restrictions = $event->return;
    if($field->name === 'my_matrix_field' && $page->template->name === 'basic-page') {
        $type_restrictions['images_block']['limit'] = 0;
    }
    $event->return = $type_restrictions;
});

 

  • Like 2
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...