You are here

public function AllowedLanguagesController::overview in Allowed Languages 8

Same name and namespace in other branches
  1. 2.x src/Controller/AllowedLanguagesController.php \Drupal\allowed_languages\Controller\AllowedLanguagesController::overview()

Override overview method defined in ContentTranslationController.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match.

string $entity_type_id: (optional) The entity type ID.

Return value

array Array of page elements to render.

Overrides ContentTranslationController::overview

File

src/Controller/AllowedLanguagesController.php, line 25

Class

AllowedLanguagesController
Base class for entity translation controllers.

Namespace

Drupal\allowed_languages\Controller

Code

public function overview(RouteMatchInterface $route_match, $entity_type_id = NULL) {
  $build = parent::overview($route_match, $entity_type_id);
  $user = $this
    ->currentUser();
  if (!$user
    ->hasPermission('translate all languages') && !empty($build['content_translation_overview']['#rows'])) {
    $rows =& $build['content_translation_overview']['#rows'];
    $languages = $this
      ->languageManager()
      ->getLanguages();
    $user_entity = User::load($user
      ->id());
    $allowed_languages = allowed_languages_get_allowed_languages_for_user($user_entity);

    // Index of a row with the language in the parent output.
    $i = 0;

    // Parent overview() method does the same loop through available languages.
    foreach ($languages as $language) {

      // If the user is not allowed to manage entities in this language.
      if (!in_array($language
        ->getId(), $allowed_languages)) {
        $target_row = $rows[$i];

        // Row with operations will always be the last. See parent method.
        end($target_row);
        $operations_key = key($target_row);

        // Unset operations element in case if user can't edit entities in this language.
        unset($rows[$i][$operations_key]['data']);
      }

      // Increment the language index.
      $i++;
    }
  }
  return $build;
}