Jump to content
Zeka

Set matrix/repeater collapsed state via API

Recommended Posts

Hi. 

Is it possible to set matrix/repeater item collapsed/uncollapsed state via API? 

Currently I set 'Repeater item visibility in editor' setting to 'Items always open (disables dynamic loading for existing items)' , but it more like a workaround than a solution.

Thanks, Eugene.

Share this post


Link to post
Share on other sites
18 hours ago, Zeka said:

Is it possible to set matrix/repeater item collapsed/uncollapsed state via API?

Sort of. This does the job but is somewhat hacky:

$wire->addHookBefore('InputfieldRepeater::render', function(HookEvent $event) {
	$inputfield = $event->object;
	$session = $event->wire('session');
	// Existing open IDs from core (e.g. items that contain errors)
	$existing_open_ids = $session->getFor('InputfieldRepeater', 'openIDs') ?: [];
	// Find the IDs of items you want to render in open state
	$open_ids = $inputfield->value->find("images.count>1")->explode('id');
	// Merge IDs
	$open_ids = array_unique(array_merge($existing_open_ids, $open_ids));
	// Set open IDs to session
	$session->setFor('InputfieldRepeater', 'openIDs', $open_ids);
});

$wire->addHookBefore('InputfieldRepeater::processInput', function(HookEvent $event) {
	// Clear open IDs from session
	$event->wire('session')->removeFor('InputfieldRepeater', 'openIDs');
});

Would be nice to have a proper API method to do this.

Edited by Robin S
Code tweak
  • Like 1

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...