You are here

public function ConfigTranslationController::itemPage in Language Hierarchy 2.x

Same name and namespace in other branches
  1. 8 src/Controller/ConfigTranslationController.php \Drupal\language_hierarchy\Controller\ConfigTranslationController::itemPage()

Language translations overview page for a configuration name.

Parameters

\Symfony\Component\HttpFoundation\Request $request: Page request object.

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

string $plugin_id: The plugin ID of the mapper.

Return value

array Page render array.

Overrides ConfigTranslationController::itemPage

File

src/Controller/ConfigTranslationController.php, line 89

Class

ConfigTranslationController
Overrides the configuration translation overview.

Namespace

Drupal\language_hierarchy\Controller

Code

public function itemPage(Request $request, RouteMatchInterface $route_match, $plugin_id) {

  /** @var \Drupal\config_translation\ConfigMapperInterface $mapper */
  $mapper = $this->configMapperManager
    ->createInstance($plugin_id);
  $mapper
    ->populateFromRouteMatch($route_match);
  $page = parent::itemPage($request, $route_match, $plugin_id);
  try {
    $original_langcode = $mapper
      ->getLangcode();
  } catch (ConfigMapperLanguageException $exception) {
    $original_langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
  }
  $fake_request = $request
    ->duplicate();
  foreach (array_diff(Element::children($page['languages']), [
    $original_langcode,
  ]) as $langcode) {
    if (!empty($page['languages'][$langcode]['operations']['#links']['edit'])) {
      $fake_route_match = RouteMatch::createFromRequest($fake_request);
      $mapper
        ->populateFromRouteMatch($fake_route_match);
      $mapper
        ->setLangcode($langcode);

      // Check this translation really does exist directly in the language.
      $exists_directly = FALSE;
      foreach ($mapper
        ->getConfigNames() as $config_name) {

        /** @var \Drupal\language\Config\LanguageConfigOverride $override */
        $override = $this->configFactoryOverride
          ->getOverride($langcode, $config_name);
        if ($override
          ->getLangcode() === $langcode) {
          $exists_directly = TRUE;
        }
      }
      if (!$exists_directly) {

        // Replace the edit & delete operations with an add one.
        unset($page['languages'][$langcode]['operations']['#links']['edit'], $page['languages'][$langcode]['operations']['#links']['delete']);
        $page['languages'][$langcode]['operations']['#links']['add'] = [
          'title' => $this
            ->t('Add'),
          'url' => Url::fromRoute($mapper
            ->getAddRouteName(), $mapper
            ->getAddRouteParameters()),
        ];
      }
    }
  }
  return $page;
}