You are here

function content_language_access_node_access in Content Language Access 8

Same name and namespace in other branches
  1. 7 content_language_access.module \content_language_access_node_access()

Implements hook_node_access().

File

./content_language_access.module, line 19
This module provides access checking of the current language of the site.

Code

function content_language_access_node_access(NodeInterface $node, $op, AccountInterface $account) {

  // Ignore verification when the user has bypass permission.
  if ($account
    ->hasPermission('bypass content_language_access')) {
    return AccessResult::neutral();
  }

  // Checking if Content language detection is enabled.
  if (in_array(LanguageInterface::TYPE_CONTENT, \Drupal::languageManager()
    ->getLanguageTypes())) {
    $language = \Drupal::languageManager()
      ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
  }
  else {
    $language = \Drupal::languageManager()
      ->getCurrentLanguage();
  }

  // Only checks for view permission.
  if (is_object($node) && $op == 'view') {
    $node_language = $node
      ->language()
      ->getId();

    // Ignoring the language is neutral and not applicable.
    if ($node_language != Language::LANGCODE_NOT_SPECIFIED && $node_language != Language::LANGCODE_NOT_APPLICABLE) {

      // Verifies if the current language is not the same of the content.
      if ($node_language != $language
        ->getId()) {

        // Checks the configuration defined in admin page.
        $config = Drupal::config('content_language_access.settings');
        $actual_language_permission = (bool) $config
          ->get($language
          ->getId() . '_' . $node_language);
        if (!$actual_language_permission) {

          // Be neutral while translating content
          if (\Drupal::routeMatch()
            ->getRouteName() == 'entity.node.content_translation_add') {
            return AccessResult::neutral();
          }
          return AccessResult::forbidden();
        }
      }
    }
  }
  return AccessResult::neutral();
}