Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir’s picture

Tagging.

Berdir’s picture

Status: Active » Needs review
fubhy’s picture

Yai for this.

+++ b/core/lib/Drupal/Core/Session/AccountInterface.phpundefined
@@ -55,4 +55,44 @@ public function getSecureSessionId();
+   * @param $default
+   *   Optional default language code to return if the account
+   *   has no valid language.

missing @param type (string)

+++ b/core/lib/Drupal/Core/Session/AccountInterface.phpundefined
@@ -55,4 +55,44 @@ public function getSecureSessionId();
+  public function getPreferredLangcode($type = NULL, $default = NULL);

Let's add two methods for this and get rid of that $type argument.

+++ b/core/lib/Drupal/Core/Session/UserSession.phpundefined
@@ -66,6 +66,27 @@ class UserSession implements AccountInterface {
+  /**
+   * The preferred language code of the account.
+   *
+   * @var string
+   */
+  protected $preferred_admin_langcode;

The preferred admin language code.

+++ b/core/lib/Drupal/Core/Session/UserSession.phpundefined
@@ -112,4 +133,45 @@ public function getSessionId() {
+  function getPreferredLangcode($type = NULL, $default = NULL) {
+    $language_list = language_list();
+    if (isset($type)) {
+      $preferred_langcode = $this->{'preferred_' . $type . '_langcode'};
+    }
+    else {
+      $preferred_langcode = $this->preferred_langcode;
+    }
+    if (!empty($preferred_langcode) && isset($language_list[$preferred_langcode])) {
+      return $language_list[$preferred_langcode]->langcode;
+    }
+    else {
+      return $default ? $default : language_default()->langcode;
+    }

Yeah... Umh, let's really go with two separate methods (one for admin, one for non-admin). :)

+++ b/core/modules/user/lib/Drupal/user/UserInterface.phpundefined
@@ -50,4 +50,163 @@ public function addRole($rid);
+   * @return int
+   *   Timestamp of the creation date.
+   *

Empty line.

+++ b/core/modules/user/user.moduleundefined
@@ -2197,31 +2191,18 @@ function theme_user_signature($variables) {
+function user_preferred_langcode(AccountInterface $account, $type = NULL, $default = NULL) {
+  return $account->getPreferredLangcode($type, $default);
 }

Okay, I see where this is coming from now. Still. It's weird. Let's change that.

Gábor Hojtsy’s picture

Yeah in core, we have two preferred langcodes, admin and regular. And there are 3 languages assignable to an account in total. There is also the entity language, which is the third. The UI does not expose all 3. If you install Drupal by default, it will only expose the language if language module is enabled. Then the language selector will set the entity language and the preferred language at once. The admin preferred language is only exposed if some language type has it configured (and the user is an admin). It is only used in that case also. So these are all built in properties/behaviours and not extensible as such. So we can have specific methods for them theoretically :)

Berdir’s picture

Status: Needs review » Needs work

The last submitted patch, user-account-interface-2026347-5.patch, failed testing.

Berdir’s picture

Status: Needs work » Needs review
FileSize
6.44 KB
54.77 KB

Fixed the docblock and killed user_preferred_langcode(), I spent more time on thinking about how to update it's documentation than killing it ;)

Status: Needs review » Needs work

The last submitted patch, user-account-interface-2026347-6.patch, failed testing.

fubhy’s picture

Cool. Much cleaner now.

+++ b/core/modules/user/lib/Drupal/user/UserInterface.phpundefined
@@ -50,4 +50,146 @@ public function addRole($rid);
+  public function activate();
...
+  public function block();

Can we make these return $this? I like it when methods for which a return value makes no sense are chainable.

Berdir’s picture

Status: Needs work » Needs review
FileSize
2.05 KB
55.15 KB

Made them do that, although I think it's less frequently used than e.g. query conditions but our coding standards basically say you should do that.

Also fixed the user bc decorator.

Status: Needs review » Needs work

The last submitted patch, user-account-interface-2026347-10.patch, failed testing.

Berdir’s picture

Status: Needs work » Needs review
FileSize
3.19 KB
55.15 KB

Stupid typo.

fubhy’s picture

Status: Needs review » Reviewed & tested by the community

Great. RTBC if green.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, user-account-interface-2026347-12.patch, failed testing.

Berdir’s picture

Status: Needs work » Needs review
FileSize
781 bytes
55.15 KB

Missed one preferred admin language calls.

Status: Needs review » Needs work
Issue tags: -sprint, -Entity Field API, -Field API NG blocker

The last submitted patch, user-account-interface-2026347-15.patch, failed testing.

Berdir’s picture

Status: Needs work » Needs review
Issue tags: +sprint, +Entity Field API, +Field API NG blocker
fubhy’s picture

Status: Needs review » Reviewed & tested by the community

Cool. Back to RTBC.

tim.plunkett’s picture

+++ b/core/modules/user/lib/Drupal/user/Plugin/views/argument_validator/User.phpundefined
@@ -124,7 +124,7 @@ public function validateArgument($argument) {
+        $name = $GLOBALS['user']->getUserName() ?: config('user.settings')->get('anonymous');

@@ -174,7 +171,7 @@ public function processSummaryArguments(&$args) {
+        $args[$uids_arg_keys[$uid]] = $account->label();

How are label() and getUserName() different?

+++ b/core/modules/user/lib/Drupal/user/UserBCDecorator.phpundefined
@@ -15,6 +15,12 @@
   /**
+   *
+   * @var \Drupal\user\UserInterface

Missing the rest of the doc.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Based on #19

Berdir’s picture

So.. . I added getUsername() because we need something user_format_name() that doesn't cause a loop as label() -> user_uri_callback() -> user_format_name().

Alternatively, we could kill user_format_name() and inline it. That means we need to move the anonymous logic into the User/UserSession classes.. which I would be fine with.

Opinions?

fubhy’s picture

Yes, discussed this with berdir a few minutes ago and I think it is fine with in-lining that code. Best part about it: Removing user_format_name().

Berdir’s picture

Status: Needs work » Needs review
FileSize
5.42 KB
57.5 KB

Ok. deprecated user_format_name() and moved the implementation into the two classes. It is now the same as label(), but I think much more meaningful, and better documented for this case.

I think that's a pattern that we want to use for other entity types as well. label() is generic, and if you have any entity and just want whatever it wants to use as it's label, call label(). If you are working with users, getUsername() is easier to understand, if you have a $node, getTitle() is, also, more meaningful and means we can also use it in edit forms and so on, where we don't want it to be alterable.

That does mean we're losing the ability for making it alterable there, but especially in this case, the username already has a specific alter, so an alterable callback is unnecessary.

Anyone disagreeing? :)

fago’s picture

Sounds good!

tim.plunkett’s picture

I disagree with having getName/getTitle for each one. Having label() across all entity types is a feature.
I certainly don't want to have to have getNid() getTid() getCid() and go back to remembering which is which.

I'm okay with the inlining, but I still don't want two methods to do the exact same thing. We've worked hard to remove that weird overlap in Views. (Display handlers, anyone?)

msonnabaum’s picture

Agree with #23. It's fine to have a label() method, but it should pass through to a method that's more descriptive.

The getNid() getTid() getCid() example doesn't really work, because you'd just call id(). Those are exposing implementation details, not domain knowledge.

alexpott’s picture

I agree with @msonnabaum here...

Berdir’s picture

Re-roll. Fixed up langcode stuff and removed an unrelated comment fix in those negotation test, not sure where they are from, maybe phpstorm fixed that up automatically...

fubhy’s picture

Status: Needs review » Reviewed & tested by the community

Great, RTBC if it comes back green.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, user-account-interface-2026347-28.patch, failed testing.

Berdir’s picture

Status: Needs work » Needs review
FileSize
1.7 KB
55.88 KB

langcode/id fixes.

ParisLiakos’s picture

Status: Needs review » Reviewed & tested by the community

back to rtbc

YesCT’s picture

Issue tags: +RTBC July 1

This issue was RTBC and passing tests on July 1, the beginning of API freeze.

YesCT’s picture

(tag did not stick. trying again.)

Berdir’s picture

alexpott’s picture

Title: Adding methods to UserInterface/AccountInterface » Change notice: Adding methods to UserInterface/AccountInterface
Priority: Normal » Critical
Status: Reviewed & tested by the community » Active
Issue tags: +Needs change record

Committed 5a85a56 and pushed to 8.x. Thanks!

I think we need to update the existing change notice and we need to announce the removal of user_preferred_langcode()

tstoeckler’s picture

Status: Active » Needs review
+++ b/core/lib/Drupal/Core/Session/AccountInterface.php
@@ -55,4 +55,63 @@ public function getSecureSessionId();
+
+  /**
+   * Returns the preferred language code of the account.
+   *
+   * @param string $default
+   *   (optional) Default language code to return if the account
+   *   has no valid language, defaults to the site default language.
+   *
+   * @return string
+   *   The language code that is preferred by the account.
+   */
+  public function getPreferredLangcode($default = NULL);

+++ b/core/modules/language/language.module
@@ -463,8 +463,9 @@ function language_get_default_langcode($entity_type, $bundle) {
+      $language_code = $user->getPreferredLangcode();
+      if (!empty($language_code)) {
+        $default_value = $language_code;
       }

+++ b/core/modules/language/language.negotiation.inc
@@ -196,8 +196,12 @@ function language_from_user($languages) {
+    $langcode = $user->getPreferredLangcode();
+    $default_langcode = language_default()->id;
+    if (!empty($langcode) && $langcode != $default_langcode && isset($languages[$langcode])) {
+      return $langcode;

@@ -221,9 +225,13 @@ function language_from_user_admin(array $languages, Request $request = NULL) {
+    $langcode = $user->getPreferredAdminLangcode();
+    $default_langcode = language_default()->id;
+    if (!empty($langcode) && $langcode != $default_langcode && isset($languages[$langcode]) && path_is_admin($request_path)) {
+      return $langcode;

Pretty sure this doesn't work as intended, as getPreferred(Admin)Langcode() never returns NULL. Instead it has $default support built-in.

Setting to "needs review" for that.

tstoeckler’s picture

Status: Needs review » Active

Oops, thought this was "fixed". That was pointless.

Berdir’s picture

Title: Change notice: Adding methods to UserInterface/AccountInterface » Adding methods to UserInterface/AccountInterface
Priority: Critical » Normal
Status: Active » Fixed
Issue tags: -Needs change record, -sprint

Updated https://drupal.org/node/1783392, see https://drupal.org/node/1783392/revisions/view/2350726/2763509 and referenced this issue in https://drupal.org/node/2017231 and https://drupal.org/node/1806650.

@tstoeckler: Yes, that looks pretty weird but I'm quite sure that I did not change the behavior here, I just converted a function call to a method. The old function returned the same thing. Can you open a new issue?

Status: Fixed » Closed (fixed)

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

scor’s picture