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

Command icon 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:

Comments

Anna D created an issue. See original summary.

anna d’s picture

Status: Active » Needs review
vitaliyb98’s picture

Status: Needs review » Needs work

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

vitaliyb98’s picture

Hi, this issue could be closed as it was fixed in https://www.drupal.org/project/domain_entity/issues/3014538 (Proposal 2 was used)

bohart’s picture

Version: 8.x-1.x-dev » 2.0.x-dev
Status: Needs work » Fixed
Related issues: +#3014538: Where did the current domain option go for Drupal 8?

@vitaliyb98, thanks for pointing this out and for the testing.
Marked as duplicated.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.