0

I've a Drupal 8 site using 8 languages. The site is not a multi-domain.

The Admin DE role should have only the permission to create nodes or translate nodes in German, but can view all node in all languages.

I don't find a module that can implement this functionality.
Do you have any ideas?

1 Answer 1

0

Create a custom module for this. I'm including some code that may help get you started. The idea behind this code (below) is preventing a user without the role 'admin_de' from editing content that has a different language than what the 'admin_de' user has set for their account. So there are some obvious differences, but this should be a good start.

In MY_MODULE.info.yml:

name: MY_MODULE
type: module
description: Allows user with particular role to edit content of particular 
language.
core: 8.x

In a file called MY_MODULE.permissions.yml, include:

edit same language:
  title: 'Edit Same Language Only'
  description: 'Allow user to edit only nodes matching their account language'

In a file called MY_MODULE.module, include:

<?php

use \Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityForm;

/**
 * Implements hook_form_alter().
 */
function MY_MODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $currentUser = Drupal::currentUser();
  $roles = $currentUser->getRoles();

  if (in_array('admin_de', $roles)) {
    switch ($form_id) {
      case 'node_CONTENT_TYPE_edit_form':
        $currentUserLanguages = getUserLanguage($currentUser->id());
        $submissionLanguage = getSubmissionLanguage($form_state);
        $status = (Drupal::currentUser()
        ->hasPermission('edit same language') && ($submissionLanguage == $currentUserLanguages));
        if (!$status) {
          throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException();
        }
        break;
    }
  }
}

/**
 * @param $userId
 * logged in user id
 *
 * @return string of language id logged in user is associated with.
 */
function getUserLanguage($userId) {
  $user = \Drupal\user\Entity\User::load($userId);
  $languageId = $user->language()->getId();

  return $languageId;
}

/**
 * @param FormStateInterface $form_state
 *
 * @return string language id of page
 */
function getSubmissionLanguage(FormStateInterface $form_state) {
  $language = '';
  $form_object = $form_state->getFormObject();
  if ($form_object instanceof EntityForm) {
    $entity = $form_object->getEntity();
    $language = $entity->langcode->value;
  }

  return $language;
}

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.