An option to preselect the default value of a language_field with the current user language would be nice.

Comments

kitikonti’s picture

kitikonti’s picture

Status: Active » Needs review
johnv’s picture

Status: Needs review » Needs work

This sounds like a good idea. But the patch adds a new hook, making the system heavier.
How about this:
- in the widget settings, add a new language code 'zus => "users current language" ' in the list of languages;
- in the widget, when setting the '#default_value', determine the correct value for language code 'zus'.

Perhaps we should request a new language code in Drupal Core - language system. In D8 already 'und' and 'zxx' exist.

kitikonti’s picture

I understand the idea of your approach but i dont understand the way you want do this? You want that i remove my new hook but then you write to add a new option in the widget settings? How should i do this without a new hook?

I think what we want is to ONLY add the "Current language" option to the field settings form where you select the default value. But to do this i also need a new hook (the same as i already use). And on every other form if "Current language" was selected as default value, we remove this option and instead set the current language as default value.

johnv’s picture

Status: Needs work » Needs review
StatusFileSize
new1.45 KB

Please review my attached version.
Indeed, the extra hook is needed, because languagefield does not use its own widget.
- Your 'preselect' option might be better for the sitebuilder, since it is more explicit. My extra 'zuser' option is a bit underwater.
- My version uses $user instead of $language. I think it is clearer, because with $language, it is not clear (to me) if you only have 1 site language or N user languages.)

In your use case, is the user language only a proposal, or is it forbidden to choose another value?

johnv’s picture

kitikonti’s picture

I have not tested your patch now, maybe i could later today. I dont understand now our/your goal? You wrote that you like my idea more than the "Current user" version, but your patch uses the "Current user" version?
And the other question is, does $user and its language property always exists or only for authenticated users?

In my use case the user never sees the field, i have restricted its access with the field permissions module. Maybe we should add some documentation for this use case.
I use the language field to get the language of guests who makes a request to a product, and answer in their language.

johnv’s picture

Indeed, my patch uses an extra language in the language list, instead of an extra checkbox, but it is too obfuscated (?). Itis a proof of concept.

About the language choice:
- in my test I change the language of the authenticated user, so the language is in the $user object.
- in your situation your anonymous user has choosen a site-wide language. I did not test that.
My point was: to me, in your first patch, it was not clear if the language could change depending on the users settings.

(I'm not sure if this post has made anything clearer :-)

johnv’s picture

Issue summary: View changes

@kitikonti, I'm investigating this in the D8-version, and have patches coming up.
In D8, 3 new options exist:
- default language (the language upon installation)
- interface language (via language switcher or else)
- users preferred language (via user settings)
These cover both the use cases in your an mine patch.

Also, in latest dev-release, the language select in the field settings is multi-value now since #2116687: Using new (custom) languages AND predefined languages got in. So a new checkbox 'preselect' is not necessary, as I'll extend the current list of (3) options.

Patches coming up soon.

kitikonti’s picture

Ok, sry that i had currently not time to test your patch.

  • johnv committed f7842b5 on 8.x-1.x
    Issue #2358913: Added Options for site/user/interface language.
    

  • johnv committed 4321356 on
    Issue #2358913: Added Options for site/user/interface language.
    
johnv’s picture

Title: Preselect language » Add options for site/user/interface language as default value
Status: Needs review » Fixed

This is now added to the D7-version and the D8-version.

kitikonti’s picture

Could i also get a patch for the d7 version? I dont really want to use the dev version on a production site. Thx for your work.

johnv’s picture

If you test and accept the current version, I will release an official release. or you can open #12. For some reason the version is not shown in the automatic comment.

kitikonti’s picture

Ok i will do a test in a view minutes.

kitikonti’s picture

Status: Fixed » Needs review
StatusFileSize
new1.73 KB

So i tested with the latest dev and i found a problem and made some minor changes.

If i use "current interface language" as default language i get an error on line 399 because $value has no element with the key 'value'. I dont really know if there are other cases but in my case $input is the array and $value in the foreach is just a string. So i removed the ['value'] two times. This works in my case, it then calls the _languagefield_getLanguageConfigurationValues function which uses the 'current_interface' case and returns the current language.

On line 385 you have a if statement with no actions, which is the exact same statment than the previous one. So i removed this. But this will run into another problem, because the following else statement, which i think we dont need, wont be executed. Because $input has always a value at this step we dont need the else statment, if it was empty it would get set in the if statement before.

On line 630 we should not use 'und', instead use LANGUAGE_NONE, which is the same.

johnv’s picture

Ja, the code is wrong. See below how I meant it (the first part). (I have no access to Drupal ATM)

function _languagefield_widget_value($element, $input = FALSE, $form_state) {
  if (!$input && isset($element['#default_value'])) {
    // The widget is shown, and a default/actual value is given.
    $input = $element['#default_value'];
  }
  elseif (!$input) {
    // The widget is shown, and no default/actual value is given.
    // Do nothing.
  }
  else {
    // The form is submitted.
    if (!is_array($input)) {
      $input = _languagefield_getLanguageConfigurationValues($input);
    }
    else {
      // Checkboxes lose their value when empty.
      // If the display field is present make sure its unchecked value is saved.
      // Convert the values to real languagecodes,
      // but ONLY on Entity form, NOT in the 'field settings - default value'.
      foreach ($input as &$value) {
//        $value['value'] = _languagefield_getLanguageConfigurationValues($value['value']); // D8??
        $value = _languagefield_getLanguageConfigurationValues($value); // D7
      }
    }
  }
  return $input;
}
kitikonti’s picture

Ahh ok so the conversion from "current language" to the "language code" do not happen on displaying the widget. It will get converted after submitting (the else statement). I am currently not on my dev desktop, but i think this would work. Maybe i could test it later today.

  • johnv committed 48faa27 on 7.x-1.x
    Issue #2358913: Added Options for site/user/interface language -2-.
    

  • johnv committed 743f5af on 8.x-1.x
    Issue #2358913: Added Options for site/user/interface language -2-
    
johnv’s picture

Indeed, the [#value_callback] executed both on showing and on committing the widget. (In D8, the massageFormValues() method is used.)
The current dev-version should work. I only doubt if we should expose the user with the technical names in the options list and the default value.
But that is UX.

kitikonti’s picture

Status: Needs review » Needs work
StatusFileSize
new712 bytes

So tested this again and run into another problem. If the user creating the node has not access to the languagefield (this is my case, because of field permissions module) $input is false on submitting. So in the _languagefield_widget_value function it will pass the first if statement. In this statement we just pass the blank default value to $input (in my case current_interface) which leads in a db error message because 'current_interface' string is too long for the db field column.
I dont know if you will do a fix for this because this is just my use case, but a db error is also not nice. I suggest to use the _languagefield_getLanguageConfigurationValues function also in this first if statement, so we always get a useable language code for the db.

I also found another problem, but i think this should be discussed in a new issue. Currently this field could not be used as multivalue field. For example if you set multiple default values only one will be used.

johnv’s picture

Changing the value upon showing the widget seems fine. However, I tested the multiple value use case (which was wrong in first instance) but i thought it was corrected in #20. Can you provide a pstch for that?

johnv’s picture

Changing the value upon showing the widget seems fine. However, I tested the multiple value use case (which was wrong in first instance) but i thought it was corrected in #20. Can you provide a pstch for that?

kitikonti’s picture

I dont know what you mean with

However, I tested the multiple value use case (which was wrong in first instance) but i thought it was corrected in #20.

Do you mean the comment #20? So the latest dev release?

johnv’s picture

Yes. Before #20 it was wrong, as you described earlier. #20 contains a fix for multivalue.

kitikonti’s picture

Ok, i dont have time until next week, so if its still open than i will have a look on it.

johnv’s picture

Perhaps your problem is that in the fist part of the function, we do not difference between single and multivalue, as we do in the second half.

kitikonti’s picture

Yes i know that this is the problem, but have not tried any solution yet.

  • johnv committed 4fe7853 on 7.x-1.x
    Issue #2358913: Added Options for site/user/interface language -3-
    
johnv’s picture

Hmm, you're making it difficult in #23.
Adjacent patch makes some things better, but still will not work for your situation, where the widget apparently is not submitted.
That cannot be handled in the widget, but in some hook_field*() hook.

johnv’s picture

Status: Needs work » Fixed

@kitikonti , let's close this issue. We can re-open a new issue regarding #23 when needed.

Status: Fixed » Closed (fixed)

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