Jump to content

More dynamic predefined Image tags, coming from Page References for instance?


PascalKonings
 Share

Recommended Posts

Hi,

I'm building a picture gallery, consisting of multiple pages. On each page I would like to add tags to my images, which I can do nicely using the predefined image tags I can set up. However, I was wondering if it is somehow possible to fill the predefined image tags with more dynamic values, like for instance a list of pages (using title or a different field of that page, adhering to the rules for tags). This would make it more flexible for me, as I would not have to have multiple different images fields with different sets of predefined tags.

Link to comment
Share on other sites

@PascalKonings, the suggestion from @Zeka is probably the way to go, but in case you do want to stick with image tags you can set them dynamically in a hook in /site/ready.php:

// Dynamically set the selectable tags for an images field
$wire->addHookAfter('InputfieldImage::renderReadyHook', function(HookEvent $event) {
	/** @var InputfieldImage $inputfield */
	$inputfield = $event->object;
	// The images field
	$field = $inputfield->hasField;
	// The page that the field is on
	$page = $inputfield->hasPage;
	// Only for a specific images field
	if(!$field || $field->name !== 'your_image_field') return;
	$config = $event->wire()->config;
	$js_name = "InputfieldFileTags_$field->name";
	$data = $config->js($js_name);
	// Set the tags however you like, using $page if needed
	$data['tags'] = [
		'carrot',
		'pumpkin',
		'cauliflower',
	];
	$config->js($js_name, $data);
});

 

  • Like 3
Link to comment
Share on other sites

10 hours ago, Robin S said:

@PascalKonings, the suggestion from @Zeka is probably the way to go, but in case you do want to stick with image tags you can set them dynamically in a hook in /site/ready.php:

// Dynamically set the selectable tags for an images field
$wire->addHookAfter('InputfieldImage::renderReadyHook', function(HookEvent $event) {
	/** @var InputfieldImage $inputfield */
	$inputfield = $event->object;
	// The images field
	$field = $inputfield->hasField;
	// The page that the field is on
	$page = $inputfield->hasPage;
	// Only for a specific images field
	if(!$field || $field->name !== 'your_image_field') return;
	$config = $event->wire()->config;
	$js_name = "InputfieldFileTags_$field->name";
	$data = $config->js($js_name);
	// Set the tags however you like, using $page if needed
	$data['tags'] = [
		'carrot',
		'pumpkin',
		'cauliflower',
	];
	$config->js($js_name, $data);
});

 

Thanks!

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