Jump to content

Recommended Posts

Posted

Processwire allows us to define predefined tags for images. What if I want to use a repeater field on the page for the purpose? How can I use values of this repeater field as the list of available tags for image fields?

Thanks.

Posted

For a repeater field named "image_tags" in the "home" template, setting the tags list for an images field named "images"...

// Save repeater titles to images tags list
$pages->addHookAfter('saveReady', function(HookEvent $event) {
    $page = $event->arguments(0);
    // For the home template
    if($page->template == 'home') {
        // Get a string of tags from the repeater item titles
        $tags = $page->image_tags->implode(' ', 'title');
        // Get the images field
        $images_field = $this->fields->images;
        // Set the list of allowed tags
        $images_field->tagsList = $tags;
        // Save the field
        $images_field->save();
    }
});

The titles of the repeater items will need to abide by the rules for image tags, so no spaces allowed.

Quote

Tags may contain letters, digits, underscores or hyphens.

 

  • Like 2

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
×
×
  • Create New...