Problem/Motivation

We added unnecessary setters to the LanugageInterface. The whole point of the Language object is that it is not configurable. The ConfigurableLanguage object exists for that purpose.

Proposed resolution

  • This issue removes all setters from the LanguageInterface
  • Removes implementations of such setters from Language and ConfigurableLanguage

Remaining tasks

Review patch.

User interface changes

None.

API changes

  • Removal of all setters from Language and ConfigurableLanguage
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alexpott’s picture

Status: Active » Needs review
FileSize
21.48 KB

Here's a patch.

Gábor Hojtsy’s picture

Issue summary: View changes
Issue tags: +language-base

Updated issue summary, it said setValuesFromValues while it is setValuesFromLanguage.

I am wondering that now that so much was done to unify the two interfaces / implementations, why do we need the intermediary Language object when we save a new ConfigurableLanguage?

Also re the locked property, a ConfigurableLanguage may not be in fact configurable on the UI IF it is locked. Drupal core ships with 2 non-configurable languages as config entities (AKA ConfigurableLanguage :D). Naming things is hard right? :)

alexpott’s picture

Issue summary: View changes

Removing incorrect statements about the locked property from the issue summary.

alexpott’s picture

I am wondering that now that so much was done to unify the two interfaces / implementations, why do we need the intermediary Language object when we save a new ConfigurableLanguage?

I think we don't need it either. Created #2336177: ConfigurableLanguage cannot be created/removed directly, integrate language_save() and language_delete() to address this.

Gábor Hojtsy’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +sprint

Yeah this looks like an amazing intermediary step then :) I am a huge proponent of not doing everything in one issue :)

Gábor Hojtsy’s picture

Status: Reviewed & tested by the community » Needs work

Wait, the method_id stuff. core/modules/language/tests/language_test/language_test.module still uses it to test if the right method was used and that is not affected by this patch. Also surprising it passes with removal of that. How can that be?

alexpott’s picture

Status: Needs work » Postponed

I'm postponing on #2336177: ConfigurableLanguage cannot be created/removed directly, integrate language_save() and language_delete() since that makes the need for ConfigurableLanguage::setValuesFromLanguage() obsolete :)

YesCT’s picture

YesCT’s picture

maybe a separate issue to handle

+++ b/core/lib/Drupal/Core/Language/LanguageDefault.php
@@ -53,7 +53,7 @@ public function get() {
* The default language.
*/
public function set(LanguageInterface $language) {
- $language->default = TRUE;
+ $language->setDefault(TRUE);
$this->language = $language;
}

default will be a protected in #2226533: Changes to the Language class due to the LanguageInterface (followup) and this issue takes out the setDefault().

Maybe we should not have the default property? since... a recent discussion has said that if a language is default is not stored on the language object. #2336177: ConfigurableLanguage cannot be created/removed directly, integrate language_save() and language_delete()

YesCT’s picture

YesCT’s picture

YesCT’s picture

slightly related to an irc conversation we had about language tests: #2337943: make a language test base helper that makes a random langcode (not in scope of this issue though)

martin107’s picture

I am not expressing an opinion one way or the other, but just to say I appreciate an informed discussion,

I have a use case for keeping some get/setters in \Drupal\language\Entity\ConfigurableLanguage

namely setDirection() and setName()

please see https://www.drupal.org/node/2337843#comment-9146131 which discusses how to transform \Drupal\language\Form\LanguageEditForm::submitForm()

  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Prepare a language object for saving.
    $languages = language_list();
    $langcode = $form_state->getValue('langcode');
    $language = $languages[$langcode];
    $language->name = $form_state->getValue('name');
    $language->direction = $form_state->getValue('direction');
    language_save($language);

    $form_state->setRedirect('language.admin_overview');
  }
Gábor Hojtsy’s picture

@martin107: #2336177: ConfigurableLanguage cannot be created/removed directly, integrate language_save() and language_delete() makes much more sweeping changes, no need for set methods. See there.

Gábor Hojtsy’s picture

Status: Postponed » Needs work
Issue tags: +Needs reroll

That just landed, so this needs a reroll.

alexpott’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
3.7 KB
15.77 KB

Yep - we no longer need ConfigurableLanguage::setValuesFromLanguage() :)

Still need to remove changes that removed the language method property since this is used and with the current code I think this is the one setter we need.

alexpott’s picture

FileSize
15.07 KB
1.45 KB

So there were no "real" usages of the method_id getter and setter methods. So I've not added them back. I've added the public properties out - we can completely remove the method_id in another issue. I noticed that property names were not actually equivalent anyway show that the current code is a bit of mess anyways. This patch makes the property names the same to make the refactor easier and the code in LanguageNegotiationMethodBase::persist() at least consist for Language and ConfigurableLanguage.

Gábor Hojtsy’s picture

So language negotiation would only happen if language module is turned on, right? Otherwise all language types are resolved to the default language. In which case can a Lanaguage object (as opposed to ConfigurableLanguage) be result of language negotiation and therefore need the method_id?

Gábor Hojtsy’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Oh wait, I see it was on both classes BEFORE the patch. I guess that was for API compatibility because the getter/setter was on the interface. Since you want to remove this anyway, there is little point debating it on this issue, and you just restored a non-modified situation regarding those properties. So good then. Let's get this in too :)

  • catch committed 1bf3b8d on 8.0.x
    Issue #2334763 by alexpott: Tidy up of LanguageInterface - removal of...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.0.x, thanks!

Gábor Hojtsy’s picture

Issue tags: -sprint

Yay, thanks!

tstoeckler’s picture

Why exactly was stuff like setWeight() removed from ConfigurableLanguage? I agree that it doesn't make sense on Language, but on ConfigurableLanguage? In the unit tests, the calls to setWeight() are being replaced with e.g. new ConfigurableLanguage(['weight' => 0]) which is far worse DX than $language->setWeight(). I didn't check how the UI sets the language weights.

I would like to open an issue to revert that change but am wondering if there are some specific arguments that I am missing here.

alexpott’s picture

The UI uses set->('weight', $value). I'm not opposed to adding setters to ConfigurableLanguageInterface - but if it is only being used in tests what is the point - just more code to maintain.

tstoeckler’s picture

Well, in my book setWeight() > set('weight'), but sure I'll open a follow-up, it's not a big deal either way. Just wanted to know if there was anything I missed. Thanks for the clarification!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.