Drupal 8 supports Multilingual (i18n) in core. I was wondering, is it possible to restrict users to be able to add/edit content for only one specific language?
3 Answers
Yes, you can use hook_node_access
to control access to add/edit for the node.
For example, you can check the value of:
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
and return the access result accordingly
Working code example:
use Drupal\node\NodeInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
/**
* Implements hook_node_access().
*
* Prevent editor role to edit and/or delete fr/en nodes. Quick and dirty.
*/
function MYMODULE_node_access(NodeInterface $node, $op, AccountInterface $account) {
$roles = $account->getRoles();
$current_language = \Drupal::languageManager()->getCurrentLanguage();
$lang = $current_language->getId();
if (!in_array('administrator', $roles) && !in_array('publisher', $roles)) {
if ($op == 'update' || $op == 'delete') {
if ($lang == 'en' || $lang == 'fr') {
return AccessResult::forbidden();
}
}
}
// No opinion.
return AccessResult::neutral();
}
The Allowed Languages module makes it possible to set restrictions on which content a user can edit based on language.
https://www.drupal.org/project/allowed_languages
Once installed against each user profile you can check which languages the user can edit as shown below.