Problem/Motivation
There is quite a bit of logic implemented in FlagService::flag()
that may through exceptions when flagging an entity:
// Check the entity type corresponds to the flag type.
if ($flag->getFlaggableEntityTypeId() != $entity->getEntityTypeId()) {
throw new \LogicException('The flag does not apply to entities of this type.');
}
// Check the bundle is allowed by the flag.
if (!empty($bundles) && !in_array($entity->bundle(), $bundles)) {
throw new \LogicException('The flag does not apply to the bundle of the entity.');
}
// Check whether there is an existing flagging for the combination of flag,
// entity, and user.
if ($this->getFlagging($flag, $entity, $account)) {
throw new \LogicException('The user has already flagged the entity with the flag.');
}
And in FlagService::unflag()
:
// Check the entity type corresponds to the flag type.
if ($flag->getFlaggableEntityTypeId() != $entity->getEntityTypeId()) {
throw new \LogicException('The flag does not apply to entities of this type.');
}
// Check the bundle is allowed by the flag.
if (!empty($bundles) && !in_array($entity->bundle(), $bundles)) {
throw new \LogicException('The flag does not apply to the bundle of the entity.');
}
$flagging = $this->getFlagging($flag, $entity, $account);
// Check whether there is an existing flagging for the combination of flag,
// entity, and user.
if (!$flagging) {
throw new \LogicException('The entity is not flagged by the user.');
}
Proposed resolution
Add helper methods, either to FlagServiceInterface
to determine if an entity is flaggable and unflaggable.
Remaining tasks
User interface changes
API changes
Data model changes