Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
<?php
/**
* @file
* Allows entities to be translated into different languages.
*/
Issue #2878556 by plach, matsbla, vijaycs85, Gábor Hojtsy, catch, gabesullice, effulgentsia, hchonov, hass, amateescu, xjm: Ensure that changes to untranslatable fields affect only one translation in pending revisions
2018-01-20 15:56:17 +00:00
use Drupal\content_translation\BundleTranslationSettingsInterface;
2014-09-17 14:39:29 +00:00
use Drupal\Core\Access\AccessResult;
2015-01-25 12:02:54 +00:00
use Drupal\Core\Entity\ContentEntityFormInterface;
2015-01-25 12:03:16 +00:00
use Drupal\Core\Entity\ContentEntityInterface;
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
use Drupal\Core\Entity\EntityInterface;
2015-01-25 12:03:16 +00:00
use Drupal\Core\Entity\EntityTypeInterface;
2014-07-31 00:50:42 +00:00
use Drupal\Core\Form\FormStateInterface;
2014-06-10 16:52:58 +00:00
use Drupal\Core\Language\LanguageInterface;
2014-06-30 03:33:08 +00:00
use Drupal\Core\Routing\RouteMatchInterface;
2015-10-01 23:25:03 +00:00
use Drupal\Core\StringTranslation\TranslatableMarkup;
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
/**
* Implements hook_help().
*/
2014-06-30 03:33:08 +00:00
function content_translation_help($route_name, RouteMatchInterface $route_match) {
2014-05-07 02:04:53 +00:00
switch ($route_name) {
case 'help.page.content_translation':
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
2017-03-04 01:20:24 +00:00
$output .= '<p>' . t('The Content Translation module allows you to translate content, comments, custom blocks, taxonomy terms, users and other <a href=":field_help" title="Field module help, with background on content entities">content entities</a>. Together with the modules <a href=":language">Language</a>, <a href=":config-trans">Configuration Translation</a>, and <a href=":locale">Interface Translation</a>, it allows you to build multilingual websites. For more information, see the <a href=":translation-entity">online documentation for the Content Translation module</a>.', [':locale' => (\Drupal::moduleHandler()->moduleExists('locale')) ? \Drupal::url('help.page', ['name' => 'locale']) : '#', ':config-trans' => (\Drupal::moduleHandler()->moduleExists('config_translation')) ? \Drupal::url('help.page', ['name' => 'config_translation']) : '#', ':language' => \Drupal::url('help.page', ['name' => 'language']), ':translation-entity' => 'https://www.drupal.org/documentation/modules/translation', ':field_help' => \Drupal::url('help.page', ['name' => 'field'])]) . '</p>';
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Enabling translation') . '</dt>';
2017-03-04 01:20:24 +00:00
$output .= '<dd>' . t('In order to translate content, the website must have at least two <a href=":url">languages</a>. When that is the case, you can enable translation for the desired content entities on the <a href=":translation-entity">Content language</a> page. When enabling translation you can choose the default language for content and decide whether to show the language selection field on the content editing forms.', [':url' => \Drupal::url('entity.configurable_language.collection'), ':translation-entity' => \Drupal::url('language.content_settings_page'), ':language-help' => \Drupal::url('help.page', ['name' => 'language'])]) . '</dd>';
2014-06-06 18:48:58 +00:00
$output .= '<dt>' . t('Enabling field translation') . '</dt>';
$output .= '<dd>' . t('You can define which fields of a content entity can be translated. For example, you might want to translate the title and body field while leaving the image field untranslated. If you exclude a field from being translated, it will still show up in the content editing form, but any changes made to that field will be applied to <em>all</em> translations of that content.') . '</dd>';
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
$output .= '<dt>' . t('Translating content') . '</dt>';
2014-06-06 18:48:58 +00:00
$output .= '<dd>' . t('If translation is enabled you can translate a content entity via the Translate tab (or Translate link). The Translations page of a content entity gives an overview of the translation status for the current content and lets you add, edit, and delete its translations. This process is similar for every translatable content entity on your site.') . '</dd>';
$output .= '<dt>' . t('Changing the source language for a translation') . '</dt>';
$output .= '<dd>' . t('When you add a new translation, the original text you are translating is displayed in the edit form as the <em>source</em>. If at least one translation of the original content already exists when you add a new translation, you can choose either the original content (default) or one of the other translations as the source, using the select list in the Source language section. After saving the translation, the chosen source language is then listed on the Translate tab of the content.') . '</dd>';
$output .= '<dt>' . t('Setting status of translations') . '</dt>';
$output .= '<dd>' . t('If you edit a translation in one language you may want to set the status of the other translations as <em>out-of-date</em>. You can set this status by selecting the <em>Flag other translations as outdated</em> checkbox in the Translation section of the content editing form. The status will be visible on the Translations page.') . '</dd>';
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
$output .= '</dl>';
return $output;
2012-12-29 08:13:54 +00:00
2014-05-07 02:04:53 +00:00
case 'language.content_settings_page':
2012-12-29 08:13:54 +00:00
$output = '';
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
if (!\Drupal::languageManager()->isMultilingual()) {
2017-03-04 01:20:24 +00:00
$output .= '<p>' . t('Before you can translate content, there must be at least two languages added on the <a href=":url">languages administration</a> page.', [':url' => \Drupal::url('entity.configurable_language.collection')]) . '</p>';
2012-12-29 08:13:54 +00:00
}
return $output;
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
}
2013-01-07 11:36:47 +00:00
/**
* Implements hook_module_implements_alter().
*/
2013-06-25 19:16:20 +00:00
function content_translation_module_implements_alter(&$implementations, $hook) {
2013-01-07 11:36:47 +00:00
switch ($hook) {
2015-11-12 22:14:00 +00:00
// Move our hook_entity_type_alter() implementation to the end of the list.
2014-02-20 10:04:54 +00:00
case 'entity_type_alter':
2013-06-25 19:16:20 +00:00
$group = $implementations['content_translation'];
unset($implementations['content_translation']);
$implementations['content_translation'] = $group;
2013-01-07 11:36:47 +00:00
break;
2018-01-27 23:19:18 +00:00
// Move our hook_entity_bundle_info_alter() implementation to the top of the
// list, so that any other hook implementation can rely on bundles being
// correctly marked as translatable.
case 'entity_bundle_info_alter':
$group = $implementations['content_translation'];
$implementations = ['content_translation' => $group] + $implementations;
break;
2013-01-07 11:36:47 +00:00
}
}
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
/**
* Implements hook_language_type_info_alter().
*/
2013-06-25 19:16:20 +00:00
function content_translation_language_types_info_alter(array &$language_types) {
2013-07-03 12:48:55 +00:00
// Make content language negotiation configurable by removing the 'locked'
// flag.
2014-06-10 16:52:58 +00:00
$language_types[LanguageInterface::TYPE_CONTENT]['locked'] = FALSE;
unset($language_types[LanguageInterface::TYPE_CONTENT]['fixed']);
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
/**
2014-02-20 10:04:54 +00:00
* Implements hook_entity_type_alter().
2014-01-02 15:19:48 +00:00
*
* The content translation UI relies on the entity info to provide its features.
2015-01-25 12:03:16 +00:00
* See the documentation of hook_entity_type_build() in the Entity API
* documentation for more details on all the entity info keys that may be
* defined.
2014-01-02 15:19:48 +00:00
*
* To make Content Translation automatically support an entity type some keys
* may need to be defined, but none of them is required unless the entity path
* is different from the usual /ENTITY_TYPE/{ENTITY_TYPE} pattern (for instance
2015-05-18 12:47:46 +00:00
* "/taxonomy/term/{taxonomy_term}"). Here are a list of those optional keys:
* - canonical: This key (in the 'links' entity info property) must be defined
* if the entity path is different from /ENTITY_TYPE/{ENTITY_TYPE}
* - translation: This key (in the 'handlers' entity annotation property)
* specifies the translation handler for the entity type. If an entity type is
* translatable and no translation handler is defined,
* \Drupal\content_translation\ContentTranslationHandler will be assumed.
* Every translation handler must implement
* \Drupal\content_translation\ContentTranslationHandlerInterface.
* - content_translation_ui_skip: By default, entity types that do not have a
* canonical link template cannot be enabled for translation. Setting this key
* to TRUE overrides that. When that key is set, the Content Translation
* module will not provide any UI for translating the entity type, and the
2015-12-03 19:04:31 +00:00
* entity type should implement its own UI. For instance, this is useful for
* entity types that are embedded into others for editing (which would not
* need a canonical link, but could still support translation).
2015-05-18 12:47:46 +00:00
* - content_translation_metadata: To implement its business logic the content
* translation UI relies on various metadata items describing the translation
* state. The default implementation is provided by
* \Drupal\content_translation\ContentTranslationMetadataWrapper, which is
* relying on one field for each metadata item (field definitions are provided
* by the translation handler). Entity types needing to customize this
* behavior can specify an alternative class through the
* 'content_translation_metadata' key in the entity type definition. Every
* content translation metadata wrapper needs to implement
* \Drupal\content_translation\ContentTranslationMetadataWrapperInterface.
2015-01-25 12:03:16 +00:00
*
2014-01-02 15:19:48 +00:00
* If the entity paths match the default pattern above and there is no need for
2015-01-25 12:03:16 +00:00
* an entity-specific translation handler, Content Translation will provide
* built-in support for the entity. However enabling translation for each
* translatable bundle will be required.
2014-01-02 15:19:48 +00:00
*
* @see \Drupal\Core\Entity\Annotation\EntityType
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
*/
2014-02-20 10:04:54 +00:00
function content_translation_entity_type_alter(array &$entity_types) {
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
// Provide defaults for translation info.
2014-02-20 10:04:54 +00:00
/** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
foreach ($entity_types as $entity_type) {
if ($entity_type->isTranslatable()) {
2014-08-22 11:59:25 +00:00
if (!$entity_type->hasHandlerClass('translation')) {
$entity_type->setHandlerClass('translation', 'Drupal\content_translation\ContentTranslationHandler');
2014-01-01 10:08:57 +00:00
}
2015-01-25 12:03:16 +00:00
if (!$entity_type->get('content_translation_metadata')) {
$entity_type->set('content_translation_metadata', 'Drupal\content_translation\ContentTranslationMetadataWrapper');
}
Issue #2486177 by disasm, amateescu, plach, Pere Orga, piyuesh23, webchick, Saphyel, dawehner, jibran, YesCT, yched, xjm, Gábor Hojtsy, Bojhan: Deleting an entity translation from the UI deletes the whole entity
2015-05-24 07:44:46 +00:00
if (!$entity_type->getFormClass('content_translation_deletion')) {
$entity_type->setFormClass('content_translation_deletion', '\Drupal\content_translation\Form\ContentTranslationDeleteForm');
}
2013-11-07 10:55:26 +00:00
2014-02-20 10:04:54 +00:00
$translation = $entity_type->get('translation');
2014-01-01 10:08:57 +00:00
if (!$translation || !isset($translation['content_translation'])) {
2017-03-04 01:20:24 +00:00
$translation['content_translation'] = [];
2013-11-07 10:55:26 +00:00
}
2013-03-18 12:40:12 +00:00
2014-02-20 10:04:54 +00:00
if ($entity_type->hasLinkTemplate('canonical')) {
2013-11-15 13:31:24 +00:00
// Provide default route names for the translation paths.
2014-02-20 10:04:54 +00:00
if (!$entity_type->hasLinkTemplate('drupal:content-translation-overview')) {
2015-11-12 22:14:00 +00:00
$translations_path = $entity_type->getLinkTemplate('canonical') . '/translations';
$entity_type->setLinkTemplate('drupal:content-translation-overview', $translations_path);
$entity_type->setLinkTemplate('drupal:content-translation-add', $translations_path . '/add/{source}/{target}');
$entity_type->setLinkTemplate('drupal:content-translation-edit', $translations_path . '/edit/{language}');
$entity_type->setLinkTemplate('drupal:content-translation-delete', $translations_path . '/delete/{language}');
2014-01-01 10:08:57 +00:00
}
2014-01-02 15:19:48 +00:00
// @todo Remove this as soon as menu access checks rely on the
2015-05-24 20:08:46 +00:00
// controller. See https://www.drupal.org/node/2155787.
2017-03-04 01:20:24 +00:00
$translation['content_translation'] += [
2013-11-15 13:31:24 +00:00
'access_callback' => 'content_translation_translate_access',
2017-03-04 01:20:24 +00:00
];
2013-11-07 10:55:26 +00:00
}
2014-02-20 10:04:54 +00:00
$entity_type->set('translation', $translation);
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
2013-03-18 12:40:12 +00:00
}
}
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
2013-03-18 12:40:12 +00:00
/**
* Implements hook_entity_bundle_info_alter().
*/
2013-06-25 19:16:20 +00:00
function content_translation_entity_bundle_info_alter(&$bundles) {
Issue #2878556 by plach, matsbla, vijaycs85, Gábor Hojtsy, catch, gabesullice, effulgentsia, hchonov, hass, amateescu, xjm: Ensure that changes to untranslatable fields affect only one translation in pending revisions
2018-01-20 15:56:17 +00:00
/** @var \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager */
$content_translation_manager = \Drupal::service('content_translation.manager');
foreach ($bundles as $entity_type_id => &$info) {
2013-03-18 12:40:12 +00:00
foreach ($info as $bundle => &$bundle_info) {
Issue #2878556 by plach, matsbla, vijaycs85, Gábor Hojtsy, catch, gabesullice, effulgentsia, hchonov, hass, amateescu, xjm: Ensure that changes to untranslatable fields affect only one translation in pending revisions
2018-01-20 15:56:17 +00:00
$bundle_info['translatable'] = $content_translation_manager->isEnabled($entity_type_id, $bundle);
if ($content_translation_manager instanceof BundleTranslationSettingsInterface) {
$settings = $content_translation_manager->getBundleTranslationSettings($entity_type_id, $bundle);
$bundle_info['untranslatable_fields.default_translation_affected'] = !empty($settings['untranslatable_fields_hide']);
}
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
}
}
2015-01-25 12:03:16 +00:00
/**
* Implements hook_entity_base_field_info().
*/
function content_translation_entity_base_field_info(EntityTypeInterface $entity_type) {
/** @var \Drupal\content_translation\ContentTranslationManagerInterface $manager */
$manager = \Drupal::service('content_translation.manager');
$entity_type_id = $entity_type->id();
if ($manager->isSupported($entity_type_id)) {
$definitions = $manager->getTranslationHandler($entity_type_id)->getFieldDefinitions();
$installed_storage_definitions = \Drupal::entityManager()->getLastInstalledFieldStorageDefinitions($entity_type_id);
// We return metadata storage fields whenever content translation is enabled
// or it was enabled before, so that we keep translation metadata around
// when translation is disabled.
// @todo Re-evaluate this approach and consider removing field storage
// definitions and the related field data if the entity type has no bundle
2017-11-14 13:54:31 +00:00
// enabled for translation.
// @see https://www.drupal.org/node/2907777
2015-01-25 12:03:16 +00:00
if ($manager->isEnabled($entity_type_id) || array_intersect_key($definitions, $installed_storage_definitions)) {
return $definitions;
}
}
}
2014-03-27 16:21:47 +00:00
/**
* Implements hook_field_info_alter().
*
* Content translation extends the @FieldType annotation with following key:
* - column_groups: contains information about the field type properties
* which columns should be synchronized across different translations and
* which are translatable. This is useful for instance to translate the
* "alt" and "title" textual elements of an image field, while keeping the
2016-02-25 15:54:27 +00:00
* same image on every translation. Each group has the following keys:
* - title: Title of the column group.
* - translatable: (optional) If the column group should be translatable by
* default, defaults to FALSE.
* - columns: (optional) A list of columns of this group. Defaults to the
* name of he group as the single column.
* - require_all_groups_for_translation: (optional) Set to TRUE to enforce
* that making this column group translatable requires all others to be
* translatable too.
2014-03-27 16:21:47 +00:00
*
2016-02-25 15:54:27 +00:00
* @see Drupal\image\Plugin\Field\FieldType\ImageItem
2014-03-27 16:21:47 +00:00
*/
function content_translation_field_info_alter(&$info) {
foreach ($info as $key => $settings) {
// Supply the column_groups key if it's not there.
if (empty($settings['column_groups'])) {
2017-03-04 01:20:24 +00:00
$info[$key]['column_groups'] = [];
2014-03-27 16:21:47 +00:00
}
}
}
2013-11-23 21:09:26 +00:00
/**
2015-01-13 09:31:34 +00:00
* Implements hook_entity_operation().
2013-11-23 21:09:26 +00:00
*/
2015-01-13 09:31:34 +00:00
function content_translation_entity_operation(EntityInterface $entity) {
2017-03-04 01:20:24 +00:00
$operations = [];
2015-01-13 09:31:34 +00:00
if ($entity->hasLinkTemplate('drupal:content-translation-overview') && content_translation_translate_access($entity)->isAllowed()) {
2017-03-04 01:20:24 +00:00
$operations['translate'] = [
2013-11-23 21:09:26 +00:00
'title' => t('Translate'),
2014-10-09 06:39:37 +00:00
'url' => $entity->urlInfo('drupal:content-translation-overview'),
2015-01-13 09:31:34 +00:00
'weight' => 50,
2017-03-04 01:20:24 +00:00
];
2013-11-23 21:09:26 +00:00
}
2015-01-13 09:31:34 +00:00
return $operations;
2013-11-23 21:09:26 +00:00
}
2015-05-05 13:21:59 +00:00
/**
* Implements hook_views_data_alter().
*/
function content_translation_views_data_alter(array &$data) {
// Add the content translation entity link definition to Views data for entity
// types having translation enabled.
$entity_types = \Drupal::entityManager()->getDefinitions();
/** @var \Drupal\content_translation\ContentTranslationManagerInterface $manager */
$manager = \Drupal::service('content_translation.manager');
foreach ($entity_types as $entity_type_id => $entity_type) {
$base_table = $entity_type->getBaseTable();
if (isset($data[$base_table]) && $entity_type->hasLinkTemplate('drupal:content-translation-overview') && $manager->isEnabled($entity_type_id)) {
$t_arguments = ['@entity_type_label' => $entity_type->getLabel()];
$data[$base_table]['translation_link'] = [
'field' => [
'title' => t('Link to translate @entity_type_label', $t_arguments),
'help' => t('Provide a translation link to the @entity_type_label.', $t_arguments),
'id' => 'content_translation_link',
],
];
}
}
}
2014-01-29 08:25:00 +00:00
/**
2014-07-30 12:04:04 +00:00
* Implements hook_menu_links_discovered_alter().
2014-01-29 08:25:00 +00:00
*/
2014-07-30 12:04:04 +00:00
function content_translation_menu_links_discovered_alter(array &$links) {
2014-01-29 08:25:00 +00:00
// Clarify where translation settings are located.
2015-10-01 23:25:03 +00:00
$links['language.content_settings_page']['title'] = new TranslatableMarkup('Content language and translation');
$links['language.content_settings_page']['description'] = new TranslatableMarkup('Configure language and translation support for content.');
2014-01-29 08:25:00 +00:00
}
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
/**
* Access callback for the translation overview page.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity whose translation overview should be displayed.
2014-09-17 14:39:29 +00:00
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
*/
2013-06-25 19:16:20 +00:00
function content_translation_translate_access(EntityInterface $entity) {
Issue #2061977 by InternetDevels, kim.pepper, ianthomas_uk, herom, rhm50, naveenvalecha, andypost, mandar.harkare, sergeypavlenko, sidharthap, SIz, tkuldeep17: Replace user_access() calls with ->hasPermission() in all core modules except user.
2014-07-12 06:55:56 +00:00
$account = \Drupal::currentUser();
2015-08-31 07:09:53 +00:00
$condition = $entity instanceof ContentEntityInterface && $entity->access('view') &&
!$entity->getUntranslated()->language()->isLocked() && \Drupal::languageManager()->isMultilingual() && $entity->isTranslatable() &&
Issue #2061977 by InternetDevels, kim.pepper, ianthomas_uk, herom, rhm50, naveenvalecha, andypost, mandar.harkare, sergeypavlenko, sidharthap, SIz, tkuldeep17: Replace user_access() calls with ->hasPermission() in all core modules except user.
2014-07-12 06:55:56 +00:00
($account->hasPermission('create content translations') || $account->hasPermission('update content translations') || $account->hasPermission('delete content translations'));
2016-03-16 01:34:36 +00:00
return AccessResult::allowedIf($condition)->cachePerPermissions()->addCacheableDependency($entity);
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
/**
* Implements hook_form_alter().
*/
2014-07-31 00:50:42 +00:00
function content_translation_form_alter(array &$form, FormStateInterface $form_state) {
2014-08-26 05:24:55 +00:00
$form_object = $form_state->getFormObject();
2014-10-05 16:05:29 +00:00
if (!($form_object instanceof ContentEntityFormInterface)) {
2014-08-26 05:24:55 +00:00
return;
}
$entity = $form_object->getEntity();
Issue #2486177 by disasm, amateescu, plach, Pere Orga, piyuesh23, webchick, Saphyel, dawehner, jibran, YesCT, yched, xjm, Gábor Hojtsy, Bojhan: Deleting an entity translation from the UI deletes the whole entity
2015-05-24 07:44:46 +00:00
$op = $form_object->getOperation();
2013-11-06 14:44:42 +00:00
2017-05-16 13:57:08 +00:00
// Let the content translation handler alter the content entity form. This can
// be the 'add' or 'edit' form. It also tries a 'default' form in case neither
// of the aforementioned forms are defined.
if ($entity instanceof ContentEntityInterface && $entity->isTranslatable() && count($entity->getTranslationLanguages()) > 1 && in_array($op, ['edit', 'add', 'default'], TRUE)) {
2015-01-12 17:48:53 +00:00
$controller = \Drupal::entityManager()->getHandler($entity->getEntityTypeId(), 'translation');
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
$controller->entityFormAlter($form, $form_state, $entity);
// @todo Move the following lines to the code generating the property form
// elements once we have an official #multilingual FAPI key.
$translations = $entity->getTranslationLanguages();
2014-08-26 05:24:55 +00:00
$form_langcode = $form_object->getFormLangcode($form_state);
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
// Handle fields shared between translations when there is at least one
// translation available or a new one is being created.
if (!$entity->isNew() && (!isset($translations[$form_langcode]) || count($translations) > 1)) {
2014-12-29 22:15:19 +00:00
$langcode_key = $entity->getEntityType()->getKey('langcode');
2014-12-23 08:26:11 +00:00
foreach ($entity->getFieldDefinitions() as $field_name => $definition) {
if (isset($form[$field_name]) && $field_name != $langcode_key) {
$form[$field_name]['#multilingual'] = $definition->isTranslatable();
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
}
}
Issue #2878556 by plach, matsbla, vijaycs85, Gábor Hojtsy, catch, gabesullice, effulgentsia, hchonov, hass, amateescu, xjm: Ensure that changes to untranslatable fields affect only one translation in pending revisions
2018-01-20 15:56:17 +00:00
// The footer region, if defined, may contain multilingual widgets so we
// need to always display it.
if (isset($form['footer'])) {
$form['footer']['#multilingual'] = TRUE;
}
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
}
2013-02-12 12:05:19 +00:00
/**
2013-11-06 14:44:42 +00:00
* Implements hook_language_fallback_candidates_OPERATION_alter().
2013-02-12 12:05:19 +00:00
*
2015-04-02 10:59:11 +00:00
* Performs language fallback for inaccessible translations.
2013-02-12 12:05:19 +00:00
*/
2013-11-06 14:44:42 +00:00
function content_translation_language_fallback_candidates_entity_view_alter(&$candidates, $context) {
2014-04-01 21:16:38 +00:00
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
2013-11-06 14:44:42 +00:00
$entity = $context['data'];
2014-04-01 21:16:38 +00:00
$entity_type_id = $entity->getEntityTypeId();
2015-01-25 12:03:16 +00:00
/** @var \Drupal\content_translation\ContentTranslationManagerInterface $manager */
$manager = \Drupal::service('content_translation.manager');
if ($manager->isEnabled($entity_type_id, $entity->bundle())) {
$entity_type = $entity->getEntityType();
$permission = $entity_type->getPermissionGranularity() == 'bundle' ? $permission = "translate {$entity->bundle()} $entity_type_id" : "translate $entity_type_id";
$current_user = \Drupal::currentuser();
if (!$current_user->hasPermission('translate any entity') && !$current_user->hasPermission($permission)) {
foreach ($entity->getTranslationLanguages() as $langcode => $language) {
$metadata = $manager->getTranslationMetadata($entity->getTranslation($langcode));
if (!$metadata->isPublished()) {
unset($candidates[$langcode]);
2013-06-30 23:59:17 +00:00
}
2013-02-12 12:05:19 +00:00
}
}
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
}
/**
2014-03-28 10:30:44 +00:00
* Implements hook_entity_extra_field_info().
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
*/
2014-03-28 10:30:44 +00:00
function content_translation_entity_extra_field_info() {
2017-03-04 01:20:24 +00:00
$extra = [];
2016-07-31 20:20:40 +00:00
$bundle_info_service = \Drupal::service('entity_type.bundle.info');
2014-01-01 10:08:57 +00:00
foreach (\Drupal::entityManager()->getDefinitions() as $entity_type => $info) {
2016-07-31 20:20:40 +00:00
foreach ($bundle_info_service->getBundleInfo($entity_type) as $bundle => $bundle_info) {
2014-12-11 09:09:33 +00:00
if (\Drupal::service('content_translation.manager')->isEnabled($entity_type, $bundle)) {
2017-03-04 01:20:24 +00:00
$extra[$entity_type][$bundle]['form']['translation'] = [
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
'label' => t('Translation'),
'description' => t('Translation settings'),
'weight' => 10,
2017-03-04 01:20:24 +00:00
];
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
}
}
return $extra;
}
/**
2015-04-01 13:04:05 +00:00
* Implements hook_form_FORM_ID_alter() for 'field_config_edit_form'.
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
*/
2015-04-01 13:04:05 +00:00
function content_translation_form_field_config_edit_form_alter(array &$form, FormStateInterface $form_state) {
$field = $form_state->getFormObject()->getEntity();
2015-05-05 00:22:30 +00:00
$bundle_is_translatable = \Drupal::service('content_translation.manager')->isEnabled($field->getTargetEntityTypeId(), $field->getTargetBundle());
2014-06-20 17:40:27 +00:00
2017-03-04 01:20:24 +00:00
$form['translatable'] = [
2013-11-28 08:25:13 +00:00
'#type' => 'checkbox',
2014-09-12 11:32:10 +00:00
'#title' => t('Users may translate this field'),
2014-09-20 02:45:52 +00:00
'#default_value' => $field->isTranslatable(),
2014-06-20 17:40:27 +00:00
'#weight' => -1,
2014-01-24 07:39:56 +00:00
'#disabled' => !$bundle_is_translatable,
2014-09-20 02:45:52 +00:00
'#access' => $field->getFieldStorageDefinition()->isTranslatable(),
2017-03-04 01:20:24 +00:00
];
2014-01-24 07:39:56 +00:00
// Provide helpful pointers for administrators.
if (\Drupal::currentUser()->hasPermission('administer content translation') && !$bundle_is_translatable) {
2017-03-04 01:20:24 +00:00
$toggle_url = \Drupal::url('language.content_settings_page', [], [
2015-04-09 14:56:37 +00:00
'query' => \Drupal::destination()->getAsArray(),
2017-03-04 01:20:24 +00:00
]);
$form['translatable']['#description'] = t('To configure translation for this field, <a href=":language-settings-url">enable language support</a> for this type.', [
Issue #2570355 by Sutharsan, stefan.r, kgoel, josephdpurcell, joelpittet, justAChris, dawehner, pfrenssen, pwolanin, David_Rothstein, catch: Replace remaining !placeholder and @placeholder with :placeholder for URLs
2015-09-22 06:16:37 +00:00
':language-settings-url' => $toggle_url,
2017-03-04 01:20:24 +00:00
]);
2014-01-24 07:39:56 +00:00
}
2013-12-04 16:37:16 +00:00
2014-09-20 02:45:52 +00:00
if ($field->isTranslatable()) {
2013-06-25 19:16:20 +00:00
module_load_include('inc', 'content_translation', 'content_translation.admin');
2014-09-20 02:45:52 +00:00
$element = content_translation_field_sync_widget($field);
2013-02-19 06:57:04 +00:00
if ($element) {
2015-04-01 13:04:05 +00:00
$form['third_party_settings']['content_translation']['translation_sync'] = $element;
$form['third_party_settings']['content_translation']['translation_sync']['#weight'] = -10;
2013-02-19 06:57:04 +00:00
}
}
}
/**
2013-06-25 10:27:47 +00:00
* Implements hook_entity_presave().
2013-02-19 06:57:04 +00:00
*/
2013-06-25 19:16:20 +00:00
function content_translation_entity_presave(EntityInterface $entity) {
2015-01-25 12:03:16 +00:00
if ($entity instanceof ContentEntityInterface && $entity->isTranslatable() && !$entity->isNew()) {
2018-01-28 02:19:56 +00:00
/** @var \Drupal\content_translation\ContentTranslationManagerInterface $manager */
$manager = \Drupal::service('content_translation.manager');
if (!$manager->isEnabled($entity->getEntityTypeId(), $entity->bundle())) {
return;
}
2015-01-25 12:03:16 +00:00
// If we are creating a new translation we need to use the source language
// as original language, since source values are the only ones available to
// compare against.
if (!isset($entity->original)) {
2016-08-05 08:29:20 +00:00
$entity->original = \Drupal::entityTypeManager()
->getStorage($entity->entityType())->loadUnchanged($entity->id());
2015-01-25 12:03:16 +00:00
}
$langcode = $entity->language()->getId();
$source_langcode = !$entity->original->hasTranslation($langcode) ? $manager->getTranslationMetadata($entity)->getSource() : NULL;
\Drupal::service('content_translation.synchronizer')->synchronizeFields($entity, $langcode, $source_langcode);
2013-02-19 06:57:04 +00:00
}
}
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
/**
* Implements hook_element_info_alter().
*/
2013-06-25 19:16:20 +00:00
function content_translation_element_info_alter(&$type) {
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
if (isset($type['language_configuration'])) {
2013-06-25 19:16:20 +00:00
$type['language_configuration']['#process'][] = 'content_translation_language_configuration_element_process';
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
}
2013-02-19 06:57:04 +00:00
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
/**
2013-06-25 19:16:20 +00:00
* Returns a widget to enable content translation per entity bundle.
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
*
* Backward compatibility layer to support entities not using the language
* configuration form element.
*
* @todo Remove once all core entities have language configuration.
*
* @param string $entity_type
* The type of the entity being configured for translation.
* @param string $bundle
* The bundle of the entity being configured for translation.
* @param array $form
* The configuration form array.
2014-07-31 00:50:42 +00:00
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
*/
2014-07-31 00:50:42 +00:00
function content_translation_enable_widget($entity_type, $bundle, array &$form, FormStateInterface $form_state) {
2014-09-12 06:41:27 +00:00
$key = $form_state->get(['content_translation', 'key']);
$context = $form_state->get(['language', $key]) ?: [];
$context += ['entity_type' => $entity_type, 'bundle' => $bundle];
$form_state->set(['language', $key], $context);
2017-03-04 01:20:24 +00:00
$element = content_translation_language_configuration_element_process(['#name' => $key], $form_state, $form);
2013-06-25 19:16:20 +00:00
unset($element['content_translation']['#element_validate']);
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
return $element;
}
/**
* Process callback: Expands the language_configuration form element.
*
* @param array $element
* Form API element.
*
* @return
* Processed language configuration element.
*/
2014-07-31 00:50:42 +00:00
function content_translation_language_configuration_element_process(array $element, FormStateInterface $form_state, array &$form) {
Issue #2061977 by InternetDevels, kim.pepper, ianthomas_uk, herom, rhm50, naveenvalecha, andypost, mandar.harkare, sergeypavlenko, sidharthap, SIz, tkuldeep17: Replace user_access() calls with ->hasPermission() in all core modules except user.
2014-07-12 06:55:56 +00:00
if (empty($element['#content_translation_skip_alter']) && \Drupal::currentUser()->hasPermission('administer content translation')) {
2014-09-12 06:41:27 +00:00
$key = $element['#name'];
$form_state->set(['content_translation', 'key'], $key);
$context = $form_state->get(['language', $key]);
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
2017-03-04 01:20:24 +00:00
$element['content_translation'] = [
2012-12-29 08:13:54 +00:00
'#type' => 'checkbox',
'#title' => t('Enable translation'),
2014-09-28 12:01:37 +00:00
// For new bundle, we don't know the bundle name yet,
// default to no translatability.
2014-12-11 09:09:33 +00:00
'#default_value' => $context['bundle'] ? \Drupal::service('content_translation.manager')->isEnabled($context['entity_type'], $context['bundle']) : FALSE,
2017-03-04 01:20:24 +00:00
'#element_validate' => ['content_translation_language_configuration_element_validate'],
];
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
2015-02-09 13:45:36 +00:00
$submit_name = isset($form['actions']['save_continue']) ? 'save_continue' : 'submit';
2015-09-20 09:30:13 +00:00
// Only add the submit handler on the submit button if the #submit property
// is already available, otherwise this breaks the form submit function.
if (isset($form['actions'][$submit_name]['#submit'])) {
$form['actions'][$submit_name]['#submit'][] = 'content_translation_language_configuration_element_submit';
}
else {
$form['#submit'][] = 'content_translation_language_configuration_element_submit';
}
2012-12-29 08:13:54 +00:00
}
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
return $element;
}
/**
2013-06-25 19:16:20 +00:00
* Form validation handler for element added with content_translation_language_configuration_element_process().
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
*
* Checks whether translation can be enabled: if language is set to one of the
* special languages and language selector is not hidden, translation cannot be
* enabled.
*
2013-06-25 19:16:20 +00:00
* @see content_translation_language_configuration_element_submit()
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
*/
2014-07-31 00:50:42 +00:00
function content_translation_language_configuration_element_validate($element, FormStateInterface $form_state, array $form) {
2014-09-12 06:41:27 +00:00
$key = $form_state->get(['content_translation', 'key']);
2014-08-08 16:25:54 +00:00
$values = $form_state->getValue($key);
2014-12-08 16:25:00 +00:00
if (!$values['language_alterable'] && $values['content_translation'] && \Drupal::languageManager()->isLanguageLocked($values['langcode'])) {
2014-06-10 16:52:58 +00:00
foreach (\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_LOCKED) as $language) {
2014-10-16 13:45:53 +00:00
$locked_languages[] = $language->getName();
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
// @todo Set the correct form element name as soon as the element parents
// are correctly set. We should be using NestedArray::getValue() but for
// now we cannot.
2017-03-04 01:20:24 +00:00
$form_state->setErrorByName('', t('"Show language selector" is not compatible with translating content that has default language: %choice. Either do not hide the language selector or pick a specific language.', ['%choice' => $locked_languages[$values['langcode']]]));
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
}
/**
2013-06-25 19:16:20 +00:00
* Form submission handler for element added with content_translation_language_configuration_element_process().
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
*
2013-06-25 19:16:20 +00:00
* Stores the content translation settings.
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
*
2013-06-25 19:16:20 +00:00
* @see content_translation_language_configuration_element_validate()
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
*/
2014-07-31 00:50:42 +00:00
function content_translation_language_configuration_element_submit(array $form, FormStateInterface $form_state) {
2014-09-12 06:41:27 +00:00
$key = $form_state->get(['content_translation', 'key']);
$context = $form_state->get(['language', $key]);
2017-03-04 01:20:24 +00:00
$enabled = $form_state->getValue([$key, 'content_translation']);
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
2014-12-11 09:09:33 +00:00
if (\Drupal::service('content_translation.manager')->isEnabled($context['entity_type'], $context['bundle']) != $enabled) {
\Drupal::service('content_translation.manager')->setEnabled($context['entity_type'], $context['bundle'], $enabled);
2014-05-27 08:34:19 +00:00
\Drupal::entityManager()->clearCachedDefinitions();
2015-03-16 08:42:16 +00:00
\Drupal::service('router.builder')->setRebuildNeeded();
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
}
2012-12-29 08:13:54 +00:00
/**
* Implements hook_form_FORM_ID_alter() for language_content_settings_form().
*/
2014-07-31 00:50:42 +00:00
function content_translation_form_language_content_settings_form_alter(array &$form, FormStateInterface $form_state) {
2013-06-25 19:16:20 +00:00
module_load_include('inc', 'content_translation', 'content_translation.admin');
_content_translation_form_language_content_settings_form_alter($form, $form_state);
2012-12-29 08:13:54 +00:00
}
2013-02-19 06:57:04 +00:00
/**
Issue #1938912 by Manuel Garcia, joelpittet, lokapujya, duellj, Cottser, akalata, pplantinga, mdrummond, rpayanm, Hydra, lauriii: Convert language content setting table theme to a twig template
2015-10-23 00:05:35 +00:00
* Implements hook_preprocess_HOOK() for language-content-settings-table.html.twig.
2013-02-19 06:57:04 +00:00
*/
2013-06-25 19:16:20 +00:00
function content_translation_preprocess_language_content_settings_table(&$variables) {
module_load_include('inc', 'content_translation', 'content_translation.admin');
_content_translation_preprocess_language_content_settings_table($variables);
2013-02-19 06:57:04 +00:00
}
2014-09-18 13:36:48 +00:00
/**
2014-10-16 12:36:06 +00:00
* Implements hook_page_attachments().
2014-09-18 13:36:48 +00:00
*/
2014-10-16 12:36:06 +00:00
function content_translation_page_attachments(&$page) {
2014-09-18 13:36:48 +00:00
$route_match = \Drupal::routeMatch();
// If the current route has no parameters, return.
if (!($route = $route_match->getRouteObject()) || !($parameters = $route->getOption('parameters'))) {
return;
}
// Determine if the current route represents an entity.
foreach ($parameters as $name => $options) {
if (!isset($options['type']) || strpos($options['type'], 'entity:') !== 0) {
continue;
}
$entity = $route_match->getParameter($name);
Issue #2479377 by dpi, edurenye, tstoeckler, andypost, mgifford, RajabNatshah, mglaman, jespermb, hitfactory, meramo, lluvigne, amateescu: content_translation_page_attachments() should check for a canonical link before generating a URL to it
2017-03-13 09:59:53 +00:00
if ($entity instanceof ContentEntityInterface && $entity->hasLinkTemplate('canonical')) {
2014-09-18 13:36:48 +00:00
// Current route represents a content entity. Build hreflang links.
foreach ($entity->getTranslationLanguages() as $language) {
Issue #2479377 by dpi, edurenye, tstoeckler, andypost, mgifford, RajabNatshah, mglaman, jespermb, hitfactory, meramo, lluvigne, amateescu: content_translation_page_attachments() should check for a canonical link before generating a URL to it
2017-03-13 09:59:53 +00:00
$url = $entity->toUrl('canonical')
2014-09-18 13:36:48 +00:00
->setOption('language', $language)
->setAbsolute()
->toString();
2017-03-04 01:20:24 +00:00
$page['#attached']['html_head_link'][] = [
[
2014-09-18 13:36:48 +00:00
'rel' => 'alternate',
2014-10-13 09:10:32 +00:00
'hreflang' => $language->getId(),
2014-09-18 13:36:48 +00:00
'href' => $url,
2017-03-04 01:20:24 +00:00
],
2014-09-18 13:36:48 +00:00
TRUE,
2017-03-04 01:20:24 +00:00
];
2014-09-18 13:36:48 +00:00
}
}
// Since entity was found, no need to iterate further.
return;
}
}