ConfigTranslationController::itemPage

public ConfigTranslationController::itemPage(Request $request, RouteMatchInterface $route_match, $plugin_id)

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.

File

core/modules/config_translation/src/Controller/ConfigTranslationController.php, line 132

Class

ConfigTranslationController
Provides page callbacks for the configuration translation interface.

Namespace

Drupal\config_translation\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 = array();
  $page['#title'] = $this->t('Translations for %label', array('%label' => $mapper->getTitle()));

  $languages = $this->languageManager->getLanguages();
  if (count($languages) == 1) {
    drupal_set_message($this->t('In order to translate configuration, the website must have at least two <a href=":url">languages</a>.', array(':url' => $this->url('entity.configurable_language.collection'))), 'warning');
  }

  try {
    $original_langcode = $mapper->getLangcode();
    $operations_access = TRUE;
  }
  catch (ConfigMapperLanguageException $exception) {
    $items = [];
    foreach ($mapper->getConfigNames() as $config_name) {
      $langcode = $mapper->getLangcodeFromConfig($config_name);
      $items[] = $this->t('@name: @langcode', [
        '@name' => $config_name,
        '@langcode' => $langcode,
      ]);
    }
    $message = [
      'message' => ['#markup' => $this->t('The configuration objects have different language codes so they cannot be translated:')],
      'items' => [
        '#theme' => 'item_list',
        '#items' => $items,
      ],
    ];
    drupal_set_message($this->renderer->renderPlain($message), 'warning');

    $original_langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
    $operations_access = FALSE;
  }

  if (!isset($languages[$original_langcode])) {
    // If the language is not configured on the site, create a dummy language
    // object for this listing only to ensure the user gets useful info.
    $language_name = $this->languageManager->getLanguageName($original_langcode);
    $languages[$original_langcode] = new Language(array('id' => $original_langcode, 'name' => $language_name));
  }

  // We create a fake request object to pass into
  // ConfigMapperInterface::populateFromRouteMatch() for the different languages.
  // Creating a separate request for each language and route is neither easily
  // possible nor performant.
  $fake_request = $request->duplicate();

  $page['languages'] = array(
    '#type' => 'table',
    '#header' => array($this->t('Language'), $this->t('Operations')),
  );
  foreach ($languages as $language) {
    $langcode = $language->getId();

    // This is needed because
    // ConfigMapperInterface::getAddRouteParameters(), for example,
    // needs to return the correct language code for each table row.
    $fake_route_match = RouteMatch::createFromRequest($fake_request);
    $mapper->populateFromRouteMatch($fake_route_match);
    $mapper->setLangcode($langcode);

    // Prepare the language name and the operations depending on whether this
    // is the original language or not.
    if ($langcode == $original_langcode) {
      $language_name = '<strong>' . $this->t('@language (original)', array('@language' => $language->getName())) . '</strong>';

      // Check access for the path/route for editing, so we can decide to
      // include a link to edit or not.
      $edit_access = $this->accessManager->checkNamedRoute($mapper->getBaseRouteName(), $route_match->getRawParameters()->all(), $this->account);

      // Build list of operations.
      $operations = array();
      if ($edit_access) {
        $operations['edit'] = array(
          'title' => $this->t('Edit'),
          'url' => Url::fromRoute($mapper->getBaseRouteName(), $mapper->getBaseRouteParameters(), ['query' => ['destination' => $mapper->getOverviewPath()]]),
        );
      }
    }
    else {
      $language_name = $language->getName();

      $operations = array();
      // If no translation exists for this language, link to add one.
      if (!$mapper->hasTranslation($language)) {
        $operations['add'] = array(
          'title' => $this->t('Add'),
          'url' => Url::fromRoute($mapper->getAddRouteName(), $mapper->getAddRouteParameters()),
        );
      }
      else {
        // Otherwise, link to edit the existing translation.
        $operations['edit'] = array(
          'title' => $this->t('Edit'),
          'url' => Url::fromRoute($mapper->getEditRouteName(), $mapper->getEditRouteParameters()),
        );

        $operations['delete'] = array(
          'title' => $this->t('Delete'),
          'url' => Url::fromRoute($mapper->getDeleteRouteName(), $mapper->getDeleteRouteParameters()),
        );
      }
    }

    $page['languages'][$langcode]['language'] = array(
      '#markup' => $language_name,
    );

    $page['languages'][$langcode]['operations'] = array(
      '#type' => 'operations',
      '#links' => $operations,
      // Even if the mapper contains multiple language codes, the source
      // configuration can still be edited.
      '#access' => ($langcode == $original_langcode) || $operations_access,
    );
  }
  return $page;
}
doc_Drupal
2016-10-29 08:54:43
Comments
Leave a Comment

Please login to continue.