Skip to content

Commit

Permalink
Disable export action when no valueContext is available (no item sele…
Browse files Browse the repository at this point in the history
…cted)
  • Loading branch information
eschleb committed Apr 17, 2024
1 parent 837946d commit 4b269db
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.namics.oss.magnolia.appbuilder.MgnlIcon;
import com.namics.oss.magnolia.appbuilder.action.AppActionDefinition;
import com.namics.oss.magnolia.appbuilder.action.AvailabilityDefinitionBuilder;
import com.namics.oss.magnolia.appbuilder.action.rule.HasValueContextRule;
import com.namics.oss.magnolia.appbuilder.action.rule.JcrIsNotDeletedRuleDefinition;
import com.namics.oss.magnolia.appbuilder.action.rule.PermissionRequiredRuleDefinition;

Expand All @@ -21,6 +22,7 @@ public ConfiguredActionDefinition action() {
definition.setIcon(MgnlIcon.EXPORT);
definition.setLabel("actions.export");
definition.setAvailability(new AvailabilityDefinitionBuilder()
.rule(new HasValueContextRule.Definition())
.rule(new JcrIsNotDeletedRuleDefinition())
.rule(new PermissionRequiredRuleDefinition(Permission.READ))
.build()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.namics.oss.magnolia.appbuilder.action.rule;

import info.magnolia.ui.ValueContext;
import info.magnolia.ui.api.availability.AvailabilityRule;
import info.magnolia.ui.api.availability.AvailabilityRuleType;
import info.magnolia.ui.api.availability.ConfiguredAvailabilityRuleDefinition;

import java.util.Collection;

import javax.inject.Inject;

public class HasValueContextRule implements AvailabilityRule {
private final ValueContext<?> valueContext;

@Inject
public HasValueContextRule(final ValueContext<?> valueContext) {
this.valueContext = valueContext;
}

@Override
public boolean isAvailable(final Collection<?> itemIds) {
return valueContext.getSingle().isPresent();
}

@AvailabilityRuleType("hasValueContextRule")
public static class Definition extends ConfiguredAvailabilityRuleDefinition {
public Definition() {
setImplementationClass(HasValueContextRule.class);
}
}
}

0 comments on commit 4b269db

Please sign in to comment.