Problem/Motivation
Drupal 10.3
Domain 2.0.0-beta1
Domain Access field is visible always doesn't matter chosen behaviour BEHAVIOR_AUTO/BEHAVIOR_USER.
Steps to reproduce
1. Log is as Editor. (any not superuser role)
2. Make sure that editor have permission: "Publish content to any assigned domain" or "Publish to any domain".
3. Create Content type A.
4. Go to /admin/config/domain/entities/node
5. Enable domain entity access. Choose BEHAVIOR_AUTO for the domain access field.
Note: BEHAVIOR_AUTO == Affiliate automatically created entity to a value (no widget on entity creation form, auto-assignation).
6. Open form to create new node of A content type.
Result: domain access field is visible.
Expected Result: domain access field is hidden.
Proposed resolution
Function domain_entity_entity_field_access returns always neutral or allowed access. But never forbidden.
Current implementation:
if ($field_definition->getName() === DomainEntityMapper::FIELD_NAME && $operation == 'edit') {
/** @var \Drupal\field\Entity\FieldConfig $field_definition */
$behavior = $field_definition->getThirdPartySetting('domain_entity', 'behavior', DomainEntityMapper::BEHAVIOR_AUTO);
$access = AccessResult::allowedIfHasPermission($account, 'set domain access status for all entities');
$access = $access->orIf(AccessResult::allowedIf($behavior == DomainEntityMapper::BEHAVIOR_USER));
$access->addCacheableDependency($field_definition);
// @todo Add remaining conditions.
return $access;
}
return AccessResult::neutral();Proposal 1:
if ($field_definition->getName() === DomainEntityMapper::FIELD_NAME && $operation == 'edit') {
/** @var \Drupal\field\Entity\FieldConfig $field_definition */
$behavior = $field_definition->getThirdPartySetting('domain_entity', 'behavior', DomainEntityMapper::BEHAVIOR_AUTO);
$access = AccessResult::allowedIfHasPermission($account, 'set domain access status for all entities');
$access = $access->orIf(AccessResult::allowedIf($behavior == DomainEntityMapper::BEHAVIOR_USER));
// @todo Add remaining conditions.
return AccessResult::forbiddenIf(!$access->isAllowed())->addCacheableDependency($field_definition);
}
return AccessResult::neutral();Proposal 2:
note: not sure about mixing allowedIf and forbiddenIf in one OR condition. But tested and it works.
if ($field_definition->getName() === DomainEntityMapper::FIELD_NAME && $operation == 'edit') {
/** @var \Drupal\field\Entity\FieldConfig $field_definition */
$behavior = $field_definition->getThirdPartySetting('domain_entity', 'behavior', DomainEntityMapper::BEHAVIOR_AUTO);
$access = AccessResult::allowedIfHasPermission($account, 'set domain access status for all entities');
$access = $access->orIf(AccessResult::forbiddenIf($behavior == DomainEntityMapper::BEHAVIOR_AUTO));
$access->addCacheableDependency($field_definition);
// @todo Add remaining conditions.
return $access;
}
return AccessResult::neutral();User interface changes
none
API changes
none
Data model changes
none
Issue fork domain_entity-3466849
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3466849-domain-access-field
changes, plain diff MR !17
Comments
Comment #3
anna d commentedComment #4
vitaliyb98 commentedHi Anna,
I think Proposal 2 is better. There is an issue https://www.drupal.org/project/domain_entity/issues/3014538, which includes the same approach.
Comment #5
vitaliyb98 commentedHi, this issue could be closed as it was fixed in https://www.drupal.org/project/domain_entity/issues/3014538 (Proposal 2 was used)
Comment #6
bohart@vitaliyb98, thanks for pointing this out and for the testing.
Marked as duplicated.