Version 1
[yaffs-website] / web / core / modules / content_translation / src / Controller / ContentTranslationController.php
1 <?php
2
3 namespace Drupal\content_translation\Controller;
4
5 use Drupal\content_translation\ContentTranslationManagerInterface;
6 use Drupal\Core\Cache\CacheableMetadata;
7 use Drupal\Core\Controller\ControllerBase;
8 use Drupal\Core\Entity\ContentEntityInterface;
9 use Drupal\Core\Language\LanguageInterface;
10 use Drupal\Core\Routing\RouteMatchInterface;
11 use Drupal\Core\Url;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Base class for entity translation controllers.
16  */
17 class ContentTranslationController extends ControllerBase {
18
19   /**
20    * The content translation manager.
21    *
22    * @var \Drupal\content_translation\ContentTranslationManagerInterface
23    */
24   protected $manager;
25
26   /**
27    * Initializes a content translation controller.
28    *
29    * @param \Drupal\content_translation\ContentTranslationManagerInterface $manager
30    *   A content translation manager instance.
31    */
32   public function __construct(ContentTranslationManagerInterface $manager) {
33     $this->manager = $manager;
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   public static function create(ContainerInterface $container) {
40     return new static($container->get('content_translation.manager'));
41   }
42
43   /**
44    * Populates target values with the source values.
45    *
46    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
47    *   The entity being translated.
48    * @param \Drupal\Core\Language\LanguageInterface $source
49    *   The language to be used as source.
50    * @param \Drupal\Core\Language\LanguageInterface $target
51    *   The language to be used as target.
52    */
53   public function prepareTranslation(ContentEntityInterface $entity, LanguageInterface $source, LanguageInterface $target) {
54     /* @var \Drupal\Core\Entity\ContentEntityInterface $source_translation */
55     $source_translation = $entity->getTranslation($source->getId());
56     $target_translation = $entity->addTranslation($target->getId(), $source_translation->toArray());
57
58     // Make sure we do not inherit the affected status from the source values.
59     if ($entity->getEntityType()->isRevisionable()) {
60       $target_translation->setRevisionTranslationAffected(NULL);
61     }
62
63     /** @var \Drupal\user\UserInterface $user */
64     $user = $this->entityManager()->getStorage('user')->load($this->currentUser()->id());
65     $metadata = $this->manager->getTranslationMetadata($target_translation);
66
67     // Update the translation author to current user, as well the translation
68     // creation time.
69     $metadata->setAuthor($user);
70     $metadata->setCreatedTime(REQUEST_TIME);
71   }
72
73   /**
74    * Builds the translations overview page.
75    *
76    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
77    *   The route match.
78    * @param string $entity_type_id
79    *   (optional) The entity type ID.
80    * @return array
81    *   Array of page elements to render.
82    */
83   public function overview(RouteMatchInterface $route_match, $entity_type_id = NULL) {
84     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
85     $entity = $route_match->getParameter($entity_type_id);
86     $account = $this->currentUser();
87     $handler = $this->entityManager()->getHandler($entity_type_id, 'translation');
88     $manager = $this->manager;
89     $entity_type = $entity->getEntityType();
90
91     // Start collecting the cacheability metadata, starting with the entity and
92     // later merge in the access result cacheability metadata.
93     $cacheability = CacheableMetadata::createFromObject($entity);
94
95     $languages = $this->languageManager()->getLanguages();
96     $original = $entity->getUntranslated()->language()->getId();
97     $translations = $entity->getTranslationLanguages();
98     $field_ui = $this->moduleHandler()->moduleExists('field_ui') && $account->hasPermission('administer ' . $entity_type_id . ' fields');
99
100     $rows = [];
101     $show_source_column = FALSE;
102
103     if ($this->languageManager()->isMultilingual()) {
104       // Determine whether the current entity is translatable.
105       $translatable = FALSE;
106       foreach ($this->entityManager->getFieldDefinitions($entity_type_id, $entity->bundle()) as $instance) {
107         if ($instance->isTranslatable()) {
108           $translatable = TRUE;
109           break;
110         }
111       }
112
113       // Show source-language column if there are non-original source langcodes.
114       $additional_source_langcodes = array_filter(array_keys($translations), function ($langcode) use ($entity, $original, $manager) {
115         $source = $manager->getTranslationMetadata($entity->getTranslation($langcode))->getSource();
116         return $source != $original && $source != LanguageInterface::LANGCODE_NOT_SPECIFIED;
117       });
118       $show_source_column = !empty($additional_source_langcodes);
119
120       foreach ($languages as $language) {
121         $language_name = $language->getName();
122         $langcode = $language->getId();
123
124         $add_url = new Url(
125           "entity.$entity_type_id.content_translation_add",
126           [
127             'source' => $original,
128             'target' => $language->getId(),
129             $entity_type_id => $entity->id(),
130           ],
131           [
132             'language' => $language,
133           ]
134         );
135         $edit_url = new Url(
136           "entity.$entity_type_id.content_translation_edit",
137           [
138             'language' => $language->getId(),
139             $entity_type_id => $entity->id(),
140           ],
141           [
142             'language' => $language,
143           ]
144         );
145         $delete_url = new Url(
146           "entity.$entity_type_id.content_translation_delete",
147           [
148             'language' => $language->getId(),
149             $entity_type_id => $entity->id(),
150           ],
151           [
152             'language' => $language,
153           ]
154         );
155         $operations = [
156           'data' => [
157             '#type' => 'operations',
158             '#links' => [],
159           ],
160         ];
161
162         $links = &$operations['data']['#links'];
163         if (array_key_exists($langcode, $translations)) {
164           // Existing translation in the translation set: display status.
165           $translation = $entity->getTranslation($langcode);
166           $metadata = $manager->getTranslationMetadata($translation);
167           $source = $metadata->getSource() ?: LanguageInterface::LANGCODE_NOT_SPECIFIED;
168           $is_original = $langcode == $original;
169           $label = $entity->getTranslation($langcode)->label();
170           $link = isset($links->links[$langcode]['url']) ? $links->links[$langcode] : ['url' => $entity->urlInfo()];
171           if (!empty($link['url'])) {
172             $link['url']->setOption('language', $language);
173             $row_title = $this->l($label, $link['url']);
174           }
175
176           if (empty($link['url'])) {
177             $row_title = $is_original ? $label : $this->t('n/a');
178           }
179
180           // If the user is allowed to edit the entity we point the edit link to
181           // the entity form, otherwise if we are not dealing with the original
182           // language we point the link to the translation form.
183           $update_access = $entity->access('update', NULL, TRUE);
184           $translation_access = $handler->getTranslationAccess($entity, 'update');
185           $cacheability = $cacheability
186             ->merge(CacheableMetadata::createFromObject($update_access))
187             ->merge(CacheableMetadata::createFromObject($translation_access));
188           if ($update_access->isAllowed() && $entity_type->hasLinkTemplate('edit-form')) {
189             $links['edit']['url'] = $entity->urlInfo('edit-form');
190             $links['edit']['language'] = $language;
191           }
192           elseif (!$is_original && $translation_access->isAllowed()) {
193             $links['edit']['url'] = $edit_url;
194           }
195
196           if (isset($links['edit'])) {
197             $links['edit']['title'] = $this->t('Edit');
198           }
199           $status = ['data' => [
200             '#type' => 'inline_template',
201             '#template' => '<span class="status">{% if status %}{{ "Published"|t }}{% else %}{{ "Not published"|t }}{% endif %}</span>{% if outdated %} <span class="marker">{{ "outdated"|t }}</span>{% endif %}',
202             '#context' => [
203               'status' => $metadata->isPublished(),
204               'outdated' => $metadata->isOutdated(),
205             ],
206           ]];
207
208           if ($is_original) {
209             $language_name = $this->t('<strong>@language_name (Original language)</strong>', ['@language_name' => $language_name]);
210             $source_name = $this->t('n/a');
211           }
212           else {
213             $source_name = isset($languages[$source]) ? $languages[$source]->getName() : $this->t('n/a');
214             $delete_access = $entity->access('delete', NULL, TRUE);
215             $translation_access = $handler->getTranslationAccess($entity, 'delete');
216             $cacheability = $cacheability
217               ->merge(CacheableMetadata::createFromObject($delete_access))
218               ->merge(CacheableMetadata::createFromObject($translation_access));
219             if ($entity->access('delete') && $entity_type->hasLinkTemplate('delete-form')) {
220               $links['delete'] = [
221                 'title' => $this->t('Delete'),
222                 'url' => $entity->urlInfo('delete-form'),
223                 'language' => $language,
224               ];
225             }
226             elseif ($translation_access->isAllowed()) {
227               $links['delete'] = [
228                 'title' => $this->t('Delete'),
229                 'url' => $delete_url,
230               ];
231             }
232           }
233         }
234         else {
235           // No such translation in the set yet: help user to create it.
236           $row_title = $source_name = $this->t('n/a');
237           $source = $entity->language()->getId();
238
239           $create_translation_access = $handler->getTranslationAccess($entity, 'create');
240           $cacheability = $cacheability
241             ->merge(CacheableMetadata::createFromObject($create_translation_access));
242           if ($source != $langcode && $create_translation_access->isAllowed()) {
243             if ($translatable) {
244               $links['add'] = [
245                 'title' => $this->t('Add'),
246                 'url' => $add_url,
247               ];
248             }
249             elseif ($field_ui) {
250               $url = new Url('language.content_settings_page');
251
252               // Link directly to the fields tab to make it easier to find the
253               // setting to enable translation on fields.
254               $links['nofields'] = [
255                 'title' => $this->t('No translatable fields'),
256                 'url' => $url,
257               ];
258             }
259           }
260
261           $status = $this->t('Not translated');
262         }
263         if ($show_source_column) {
264           $rows[] = [
265             $language_name,
266             $row_title,
267             $source_name,
268             $status,
269             $operations,
270           ];
271         }
272         else {
273           $rows[] = [$language_name, $row_title, $status, $operations];
274         }
275       }
276     }
277     if ($show_source_column) {
278       $header = [
279         $this->t('Language'),
280         $this->t('Translation'),
281         $this->t('Source language'),
282         $this->t('Status'),
283         $this->t('Operations'),
284       ];
285     }
286     else {
287       $header = [
288         $this->t('Language'),
289         $this->t('Translation'),
290         $this->t('Status'),
291         $this->t('Operations'),
292       ];
293     }
294
295     $build['#title'] = $this->t('Translations of %label', ['%label' => $entity->label()]);
296
297     // Add metadata to the build render array to let other modules know about
298     // which entity this is.
299     $build['#entity'] = $entity;
300     $cacheability
301       ->addCacheTags($entity->getCacheTags())
302       ->applyTo($build);
303
304     $build['content_translation_overview'] = [
305       '#theme' => 'table',
306       '#header' => $header,
307       '#rows' => $rows,
308     ];
309
310     return $build;
311   }
312
313   /**
314    * Builds an add translation page.
315    *
316    * @param \Drupal\Core\Language\LanguageInterface $source
317    *   The language of the values being translated. Defaults to the entity
318    *   language.
319    * @param \Drupal\Core\Language\LanguageInterface $target
320    *   The language of the translated values. Defaults to the current content
321    *   language.
322    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
323    *   The route match object from which to extract the entity type.
324    * @param string $entity_type_id
325    *   (optional) The entity type ID.
326    *
327    * @return array
328    *   A processed form array ready to be rendered.
329    */
330   public function add(LanguageInterface $source, LanguageInterface $target, RouteMatchInterface $route_match, $entity_type_id = NULL) {
331     $entity = $route_match->getParameter($entity_type_id);
332
333     // @todo Exploit the upcoming hook_entity_prepare() when available.
334     // See https://www.drupal.org/node/1810394.
335     $this->prepareTranslation($entity, $source, $target);
336
337     // @todo Provide a way to figure out the default form operation. Maybe like
338     //   $operation = isset($info['default_operation']) ? $info['default_operation'] : 'default';
339     //   See https://www.drupal.org/node/2006348.
340
341     // Use the add form handler, if available, otherwise default.
342     $operation = $entity->getEntityType()->hasHandlerClass('form', 'add') ? 'add' : 'default';
343
344     $form_state_additions = [];
345     $form_state_additions['langcode'] = $target->getId();
346     $form_state_additions['content_translation']['source'] = $source;
347     $form_state_additions['content_translation']['target'] = $target;
348     $form_state_additions['content_translation']['translation_form'] = !$entity->access('update');
349
350     return $this->entityFormBuilder()->getForm($entity, $operation, $form_state_additions);
351   }
352
353   /**
354    * Builds the edit translation page.
355    *
356    * @param \Drupal\Core\Language\LanguageInterface $language
357    *   The language of the translated values. Defaults to the current content
358    *   language.
359    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
360    *   The route match object from which to extract the entity type.
361    * @param string $entity_type_id
362    *   (optional) The entity type ID.
363    *
364    * @return array
365    *   A processed form array ready to be rendered.
366    */
367   public function edit(LanguageInterface $language, RouteMatchInterface $route_match, $entity_type_id = NULL) {
368     $entity = $route_match->getParameter($entity_type_id);
369
370     // @todo Provide a way to figure out the default form operation. Maybe like
371     //   $operation = isset($info['default_operation']) ? $info['default_operation'] : 'default';
372     //   See https://www.drupal.org/node/2006348.
373
374     // Use the edit form handler, if available, otherwise default.
375     $operation = $entity->getEntityType()->hasHandlerClass('form', 'edit') ? 'edit' : 'default';
376
377     $form_state_additions = [];
378     $form_state_additions['langcode'] = $language->getId();
379     $form_state_additions['content_translation']['translation_form'] = TRUE;
380
381     return $this->entityFormBuilder()->getForm($entity, $operation, $form_state_additions);
382   }
383
384 }