Problem/Motivation

Stemming from followup comments: https://www.drupal.org/project/custom_field/issues/3496669#comment-15925128

There's a use case where user could create a translation in other language before and independent of default site language entity existing. The subfields should always be visible in this case as this translation will be considered the default.

Proposed resolution

Our logic for the $entity variable is incorrect as we need to load the translation from the current language and use the $entity->isDefaultTranslation(). Additionally, as recommended in the related issue, we need to also evaluate access on if the $entity->isNew().

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

apmsooner created an issue. See original summary.

apmsooner’s picture

Status: Active » Needs review

Please test patch.

jurgenhaas’s picture

Status: Needs review » Needs work

This is looking pretty good, but there is 1 part that I found that still needs a change.

Checking for $entity instanceof TranslatableInterface isn't what you expect. This is because all content entities are an instance of ContentEntityInterface which extends TranslatableRevisionableInterface which extends TranslatableInterface. In other words: every content entity is an instance of TranslatableInterface.

What you really want to check is $entity->isTranslatable(). This gives you the real information whether the current entity is translatable. If it's not, you can skip all the translation logic.

It seems that this is not only within the scope of the current MR. You most likely need to replace $entity instanceof TranslatableInterface with $entity->isTranslatable() in the whole code base.

apmsooner’s picture

Status: Needs work » Needs review

Thanks for catching that. I've made revisions and ready to review again. Noted other places in the codebase will need similar revisions and those places should be limited to the formatter logic which I'm in the process of refactoring/simplifying logic as part of https://www.drupal.org/project/custom_field/issues/3496708. Mainly the BaseFormatter.php file handles some logic for entity references so I will take care of it there in that ticket so we can keep the scope of this ticket limited.

jurgenhaas’s picture

Status: Needs review » Reviewed & tested by the community

This is looking good for the use cases I've tested before and again after the last commit. Not sure, if this now covers everything, though. It's certainly a complex topic.

Not sure what else is going on in the other issue, I haven't had time to look into that as well. It's just important to note, that $entity instanceof TranslatableInterface always returns TRUE - and that's most likely not what's expected.

  • apmsooner committed 5cc6ec38 on 3.0.x
    Issue #3497244 by apmsooner, jurgenhaas: Translatable subfield access...
apmsooner’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for all the due diligence in this review!

As for the other issue, i'm adding some optional html wrapper logic for the formatters similar to the fences module. In starting that feature, I realized the existing formatter logic was over complicating things and needed to be more like core with the way the plugins get instantiated so I'm simplifying/standardizing the apis on those for cleaner and reduced code.

apmsooner’s picture

Version: 3.0.x-dev » 3.1.x-dev
apmsooner’s picture

Status: Fixed » Closed (fixed)