Skip to main content
Linked mentioned hook; improved formatting.
Source Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

You could use a programmatically solution using hook_node_access()hook_node_access(), in this example the field_YOURFIELD is a checkbox.

function YOUR_MODULE_node_access(NodeInterface $node, $op, AccountInterface $account) { $access = new AccessResultNeutral(); if ($node->hasField('field_YOURFIELD')) { $field = $node->get('field_YOURFIELD')->getString(); if (empty($field) && $account->id() != $node->getOwnerId()) { $access = new AccessResultForbidden(); } } $access->addCacheableDependency($field); $access->addCacheableDependency($node); return $access; }
/**
 * Implements hook_node_access().
 */
function MYMODULE_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account) {
  $access = new \Drupal\Core\Access\AccessResultNeutral();
  if ($node->hasField('field_YOURFIELD')) {
    $field = $node->get('field_YOURFIELD')->getString();
    if (empty($field) && $account->id() != $node->getOwnerId()) {
      $access = new \Drupal\Core\Access\AccessResultForbidden();
    }
  }
  $access->addCacheableDependency($field);
  $access->addCacheableDependency($node);
  return $access;
}

You could use a programmatically solution using hook_node_access(), in this example the field_YOURFIELD is a checkbox.

function YOUR_MODULE_node_access(NodeInterface $node, $op, AccountInterface $account) { $access = new AccessResultNeutral(); if ($node->hasField('field_YOURFIELD')) { $field = $node->get('field_YOURFIELD')->getString(); if (empty($field) && $account->id() != $node->getOwnerId()) { $access = new AccessResultForbidden(); } } $access->addCacheableDependency($field); $access->addCacheableDependency($node); return $access; }

You could use a programmatically solution using hook_node_access(), in this example the field_YOURFIELD is a checkbox.

/**
 * Implements hook_node_access().
 */
function MYMODULE_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account) {
  $access = new \Drupal\Core\Access\AccessResultNeutral();
  if ($node->hasField('field_YOURFIELD')) {
    $field = $node->get('field_YOURFIELD')->getString();
    if (empty($field) && $account->id() != $node->getOwnerId()) {
      $access = new \Drupal\Core\Access\AccessResultForbidden();
    }
  }
  $access->addCacheableDependency($field);
  $access->addCacheableDependency($node);
  return $access;
}
Source Link

You could use a programmatically solution using hook_node_access(), in this example the field_YOURFIELD is a checkbox.

function YOUR_MODULE_node_access(NodeInterface $node, $op, AccountInterface $account) { $access = new AccessResultNeutral(); if ($node->hasField('field_YOURFIELD')) { $field = $node->get('field_YOURFIELD')->getString(); if (empty($field) && $account->id() != $node->getOwnerId()) { $access = new AccessResultForbidden(); } } $access->addCacheableDependency($field); $access->addCacheableDependency($node); return $access; }