Problem/Motivation
$node !== $node->getUntranslated() always evals to FALSE, but should be TRUE on the translated node page.
Repro:
- Install Google Analytics and go to ga settings
- Enter a fake UA code e.g. UA-1234-1
- Enable Track translation sets as one unit
- Save settings
- Create a node in English
- Create a German translation of these English node
- Edit google_analytics.module near line 172+ and enable the debugging
krumo($node != $node->getUntranslated()); // FALSE krumo($node !== $node->getUntranslated()); // FALSE - Load page in German,
$node !== $node->getUntranslated()should be TRUE and not FALSE
Proposed resolution
Remaining tasks
User interface changes
None
API changes
?
Original report by @username
What is the upgrade path for this function? I need to know with a bool if the content type is enabled for translation or not.
translation_supported_type($node->type)
I cannot believe that everyone need to copy this code to his module. This qualifies for an API addition if not there.
[content_translation.pages.inc]
// Determine whether the current entity is translatable.
$translatable = FALSE;
foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) {
$field = $instance->getField();
if ($field['translatable']) {
$translatable = TRUE;
break;
}
}
Comments
Comment #0.0
hass commenteda
Comment #1
catchComment #2
hass commentedRemoving important API functions without replacing them with a new is a regression = bug.
Comment #3
hass commentedMaybe
content_translation_enabled('node', $node->getType())... ?Comment #4
hass commentedComment #5
plachYou can also use the
translatablekey of the entity bundle info.Comment #6
plachIf you have the node object you can also simply do
$node->isTranslatable().Comment #7
hass commentedThanks a lot, this may makes things more easier... looking into it again.
I end up with:
I still guess this can be re-factored to less code...
Gave you the credit http://drupalcode.org/project/google_analytics.git/commit/e3eb255
Comment #8
plachThanks :)
Not sure what
Node::prepareLangcode()is supposed to do, but if you want to check whether the node object is not the original one the quickest way is the following:Comment #9
hass commentedIt looks like $node->language()->id returns 'en' if the node was created with English first. If a node was created in German it returns 'de'. All translations will always return the original $node->language()->id and prepareLangcode() returns the language code of the current shown translation. I need the original language code here.
I do not need to check
$node->isTranslatable()at all in this case? Less code is always better :-)Comment #10
plach$node->language()returns the language of the active translation,$node->getUntranslated()returns the original entity object, hence$node->getUntranslated()->language()returns the original language. If$node !== $node->getUntranslated()you are sure you are dealing with a translation so you don't need to check whether the node is translatable.See the documentation page for details.
Comment #11
hass commentedI guess
$node->getUntranslated()->language()->idis the one I should use in url() function...Comment #12
plachYes, definitely :)
You can just use
$node->getUntranslated()->language()which already returns the language object.Comment #13
hass commentedI'm opening it as I see a lot of questions coming in my mind now.
Both return the same? But if I switch the page language to German and see the German translation of my english node it's still an English language object with $node->language().
Aside of this you pointed me to https://drupal.org/node/2040721 and this is talking about
$node->language()->langcode, but there is no 'langcode'. It seems to be$node->language()->id. If I'm not totally wrong this documentation is outdated or is - how it should be in future? Currentlydpm($node->language()->langcode);give meNotice: Undefined property: Drupal\Core\Language\Language::$langcodeThe condition
$node !== $node->getUntranslated()also does not change if I switch the website from english to german or back. It's always FALSE.I'm confused now...
Comment #14
plachThe documentation was outdated wrt the
->langcodepart which has changed to->idmeanwhile, updated that.Regarding node language in itself, you should be aware that we are not done yet with the Entity Translation API: we are currently working on #2019055: Switch from field-level language fallback to entity-level language fallback, which is dealing with entity language negotiation, that is determining the proper translation object to be used in a particular context. We will take care of passing around the proper translation object, where we can reliably determine it. We will also provide a new method to easily determine the most fitting translation object for a given context.
Hope this helps :)
Btw, you are welcome to join us over there and provide your feedback.
Comment #15
hass commentedOk, this means re-visit these lines in some days/weeks... :-)
Trying my best... Aside - I believe "langcode" is a lot more intuitive to understand than "id", but I guess this has already been discussed somewhere else to a death end. Just like to say it... Thanks a lot for your help.
Comment #16
plachYou are probably right, but now language objects are configuration entities so they inherited the entity terminology.
Comment #17
hass commentedTracking this in #2112739: Upgrade "Track translation sets as one unit" feature
Comment #17.0
hass commenteda
Comment #19
hass commentedPer discussion with plach, moving to bug.
$node !== $node->getUntranslated()always evals toFALSE, but should be TRUE on the translated node page.Repro:
$node !== $node->getUntranslated()should be TRUEComment #20
hass commentedComment #21
hass commentedComment #22
hass commentedComment #23
plachComment #24
hass commentedComment #25
hass commentedComment #26
plachIt's not a core bug: you need to pick the correct translation. The following snippet seems to work:
We are evaluating the possibility to perform entity language negotiation earlier, but for now this is the right way to do it.
Comment #27
hass commentedThat's really not so easy to guess. But I still need
$node->isTranslatable()?Comment #28
plachNope, that looks unecessary now :)
Comment #29
hass commentedHopefully that's final code now.
http://drupalcode.org/project/google_analytics.git/commit/e82079b