You are here

content_language_access.module in Content Language Access 8

Same filename and directory in other branches
  1. 6 content_language_access.module
  2. 7 content_language_access.module

This module provides access checking of the current language of the site.

File

content_language_access.module
View source
<?php

/**
 * @file
 * This module provides access checking of the current language of the site.
 */
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Url;

/**
 * Implements hook_node_access().
 */
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();
}

/**
 * Implements hook_help().
 */
function content_language_access_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the content_language_access module.
    case 'help.page.content_language_access':
      return '<p>' . t('Content Language Access Module restricts the access of only contents with language (except neutral language) that are equal of the actual Drupal language being accessed or others that were previous configured in the <a href=":content_language_access">admin page</a>.', [
        ':content_language_access' => Url::fromRoute('content_language_access.admin_form')
          ->toString(),
      ]) . '</p>';

    // Help for admin page for the content_language_access module.
    case 'content_language_access.admin_form':
      return '<p>' . t('This page provides an interface for configuring more languages that can be accessed from a Drupal language.') . '</p>' . '<p>' . t('Language of the content is detected based on the settings defined on <a href=":language_detection">Language detection and selection page</a>.', [
        ':language_detection' => Url::fromRoute('language.negotiation')
          ->toString(),
      ]) . '</p>';
  }
}