content_translation.pages.inc 9.57 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * The content translation user interface.
6 7 8 9
 */

use Drupal\Core\Language\Language;
use Drupal\Core\Entity\EntityInterface;
10
use Drupal\Core\Entity\ContentEntityInterface;
11 12 13 14 15 16

/**
 * Translations overview page callback.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity whose translation overview should be displayed.
17
 *
18 19
 * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
 *   Use \Drupal\content_translation\Controller\ContentTranslationController::overview().
20
 */
21
function content_translation_overview(EntityInterface $entity) {
22
  $controller = content_translation_controller($entity->getEntityTypeId());
23
  $languages = \Drupal::languageManager()->getLanguages();
24
  $original = $entity->getUntranslated()->language()->id;
25
  $translations = $entity->getTranslationLanguages();
26
  $administrator = \Drupal::currentUser()->hasPermission('administer languages');
27

28 29
  $rel = array();
  foreach (array('canonical', 'edit-form', 'drupal:content-translation-overview') as $name) {
30
    $rel[$name] = $entity->getSystemPath($name);
31
  }
32 33 34 35

  $header = array(t('Language'), t('Translation'), t('Source language'), t('Status'), t('Operations'));
  $rows = array();

36
  if (\Drupal::languageManager()->isMultilingual()) {
37

38 39
    // Determine whether the current entity is translatable.
    $translatable = FALSE;
40 41
    foreach (\Drupal::entityManager()->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle()) as $field_definition) {
      if ($field_definition->isTranslatable()) {
42 43 44 45 46 47 48
        $translatable = TRUE;
        break;
      }
    }

    foreach ($languages as $language) {
      $language_name = $language->name;
49
      $langcode = $language->id;
50

51 52
      $add_path = $rel['drupal:content-translation-overview'] . '/add/' . $original . '/' . $langcode;
      $translate_path = $rel['drupal:content-translation-overview'] . '/edit/' . $langcode;
53 54

      $add_links = _content_translation_get_switch_links($add_path);
55
      $edit_links = _content_translation_get_switch_links($rel['edit-form']);
56
      $translate_links = _content_translation_get_switch_links($translate_path);
57
      $delete_links = _content_translation_get_switch_links($rel['drupal:content-translation-overview'] . '/delete/' . $langcode);
58 59 60 61 62 63 64 65 66 67 68

      $operations = array(
        'data' => array(
          '#type' => 'operations',
          '#links' => array(),
        ),
      );
      $links = &$operations['data']['#links'];

      if (isset($translations[$langcode])) {
        // Existing translation in the translation set: display status.
69
        $source = isset($entity->translation[$langcode]['source']) ? $entity->translation[$langcode]['source'] : '';
70
        $is_original = $langcode == $original;
71
        $label = $entity->getTranslation($langcode)->label();
72
        $link = isset($links->links[$langcode]['href']) ? $links->links[$langcode] : array('href' => $rel['canonical'], 'language' => $language);
73 74 75 76 77 78
        $row_title = l($label, $link['href'], $link);

        if (empty($link['href'])) {
          $row_title = $is_original ? $label : t('n/a');
        }

79 80 81
        // If the user is allowed to edit the entity we point the edit link to
        // the entity form, otherwise if we are not dealing with the original
        // language we point the link to the translation form.
82
        if ($entity->access('update')) {
83
          $links['edit'] = isset($edit_links->links[$langcode]['href']) ? $edit_links->links[$langcode] : array('href' => $rel['edit-form'], 'language' => $language);
84 85 86 87 88 89
        }
        elseif (!$is_original && $controller->getTranslationAccess($entity, 'update')) {
          $links['edit'] = isset($translate_links->links[$langcode]['href']) ? $translate_links->links[$langcode] : array('href' => $translate_path, 'language' => $language);
        }

        if (isset($links['edit'])) {
90
          $links['edit']['title'] = t('Edit');
91 92
        }

93 94
        $translation = $entity->translation[$langcode];
        $status = !empty($translation['status']) ? t('Published') : t('Not published');
95
        // @todo Add a theming function here.
96
        $status = '<span class="status">' . $status . '</span>' . (!empty($translation['outdated']) ? ' <span class="marker">' . t('outdated') . '</span>' : '');
97 98

        if ($is_original) {
99
          $language_name = t('<strong>@language_name (Original language)</strong>', array('@language_name' => $language_name));
100 101 102 103
          $source_name = t('n/a');
        }
        else {
          $source_name = isset($languages[$source]) ? $languages[$source]->name : t('n/a');
104 105
          if ($controller->getTranslationAccess($entity, 'delete')) {
            $links['delete'] = isset($delete_links->links[$langcode]['href']) ? $delete_links->links[$langcode] : array('href' => $delete_links, 'language' => $language);
106
            $links['delete']['title'] = t('Delete');
107
          }
108 109 110 111 112
        }
      }
      else {
        // No such translation in the set yet: help user to create it.
        $row_title = $source_name = t('n/a');
113
        $source = $entity->language()->id;
114

115
        if ($source != $langcode && $controller->getTranslationAccess($entity, 'create')) {
116 117
          if ($translatable) {
            $links['add'] = isset($add_links->links[$langcode]['href']) ? $add_links->links[$langcode] : array('href' => $add_path, 'language' => $language);
118
            $links['add']['title'] = t('Add');
119
          }
120 121
          elseif ($administrator) {
            $links['nofields'] = array('title' => t('No translatable fields'), 'route_name' => 'language.content_settings_page', 'language' => $language);
122 123 124 125 126 127 128 129 130 131
          }
        }

        $status = t('Not translated');
      }

      $rows[] = array($language_name, $row_title, $source_name, $status, $operations);
    }
  }

132
  $build['#title'] = t('Translations of %label', array('%label' => $entity->label()));
133 134 135 136 137

  // Add metadata to the build render array to let other modules know about
  // which entity this is.
  $build['#entity'] = $entity;

138
  $build['content_translation_overview'] = array(
139
    '#type' => 'table',
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    '#header' => $header,
    '#rows' => $rows,
  );

  return $build;
}

/**
 * Returns the localized links for the given path.
 *
 * @param string $path
 *   The path for which language switch links should be provided.
 *
 * @returns
 *   A renderable array of language switch links.
 */
156
function _content_translation_get_switch_links($path) {
157
  $links = \Drupal::languageManager()->getLanguageSwitchLinks(Language::TYPE_CONTENT, $path);
158 159
  if (empty($links)) {
    // If content language is set up to fall back to the interface language,
160
    // then there will be no switch links for Language::TYPE_CONTENT, ergo we
161
    // also need to use interface switch links.
162
    $links = \Drupal::languageManager()->getLanguageSwitchLinks(Language::TYPE_INTERFACE, $path);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  }
  return $links;
}

/**
 * Page callback for the translation addition page.
 *
 * @param EntityInterface $entity
 *   The entity being translated.
 * @param Language $source
 *   (optional) The language of the values being translated. Defaults to the
 *   entity language.
 * @param Language $target
 *   (optional) The language of the translated values. Defaults to the current
 *   content language.
 *
 * @return array
 *   A processed form array ready to be rendered.
181
 *
182 183
 * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
 *   Use \Drupal\content_translation\Controller\ContentTranslationController::add().
184
 */
185
function content_translation_add_page(EntityInterface $entity, Language $source = NULL, Language $target = NULL) {
186
  $source = !empty($source) ? $source : $entity->language();
187
  $target = !empty($target) ? $target : \Drupal::languageManager()->getCurrentLanguage(Language::TYPE_CONTENT);
188
  // @todo Exploit the upcoming hook_entity_prepare() when available.
189
  content_translation_prepare_translation($entity, $source, $target);
190
  $form_state['langcode'] = $target->id;
191 192
  $form_state['content_translation']['source'] = $source;
  $form_state['content_translation']['target'] = $target;
193
  $form_state['content_translation']['translation_form'] = !$entity->access('update');
194
  return \Drupal::service('entity.form_builder')->getForm($entity, 'default', $form_state);
195 196 197 198 199 200 201 202 203 204 205 206 207
}

/**
 * Page callback for the translation edit page.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity being translated.
 * @param \Drupal\Core\Language\Language $language
 *   (optional) The language of the translated values. Defaults to the current
 *   content language.
 *
 * @return array
 *   A processed form array ready to be rendered.
208
 *
209 210
 * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
 *   Use \Drupal\content_translation\Controller\ContentTranslationController::edit().
211
 */
212
function content_translation_edit_page(EntityInterface $entity, Language $language = NULL) {
213
  $language = !empty($language) ? $language : \Drupal::languageManager()->getCurrentLanguage(Language::TYPE_CONTENT);
214
  $form_state['langcode'] = $language->id;
215
  $form_state['content_translation']['translation_form'] = TRUE;
216
  return \Drupal::service('entity.form_builder')->getForm($entity, 'default', $form_state);
217 218 219 220 221 222 223 224 225 226 227 228
}

/**
 * Populates target values with the source values.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entitiy being translated.
 * @param \Drupal\Core\Language\Language $source
 *   The language to be used as source.
 * @param \Drupal\Core\Language\Language $target
 *   The language to be used as target.
 */
229
function content_translation_prepare_translation(EntityInterface $entity, Language $source, Language $target) {
230 231
  if ($entity instanceof ContentEntityInterface) {
    $source_translation = $entity->getTranslation($source->id);
232
    $entity->addTranslation($target->id, $source_translation->toArray());
233
  }
234
}