Access callback: Checks a user's permission for performing a node operation.
$op: The operation to be performed on the node. Possible values are:
Drupal\Core\Entity\EntityInterface|string|stdClass $node: The node entity on which the operation is to be performed, or the node type object, or node type string (e.g., 'forum') for the 'create' operation.
$account: (optional) A user object representing the user for whom the operation is to be performed. Determines access for a user other than the current user. Defaults to NULL.
$langcode: (optional) Language code for the variant of the node. Different language variants might have different permissions associated. If NULL, the original langcode of the node is used. Defaults to NULL.
TRUE if the operation may be performed, FALSE otherwise.
function node_access($op, $node, $account = NULL, $langcode = NULL) {
if (!$node instanceof EntityInterface) {
$type = is_object($node) ? $node->type : $node;
$node = entity_create('node', array(
'type' => $type,
));
}
// If no language code was provided, default to the node's langcode.
if (empty($langcode)) {
$langcode = $node->langcode;
// If the Language module is enabled, try to use the language from content
// negotiation.
if (module_exists('language')) {
// Load languages the node exists in.
$node_translations = $node
->getTranslationLanguages();
// Load the language from content negotiation.
$content_negotiation_langcode = language(Language::TYPE_CONTENT)->langcode;
// If there is a translation available, use it.
if (isset($node_translations[$content_negotiation_langcode])) {
$langcode = $content_negotiation_langcode;
}
}
}
// Make sure that if an account is passed, that it is a fully loaded user
// object.
if ($account && !$account instanceof UserInterface) {
$account = user_load($account->uid);
}
return entity_access_controller('node')
->access($node, $op, $langcode, $account);
}