How to sort language select list on current language context





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}






up vote
1
down vote

favorite












I have a form with a language field with the language select list widget.
The options of this field contains an array with a pattern like



['langcode' => t('language')]


Now I want to sort these options in the right order.



The default sorting is on the value but not on the translated one.



e.g. ['de' => t('german')] which I translated into "Deutsch" will now sort under "g" instead of "d".



So I did try to do it in a form alter hook and a preprocess hook but this is not the right place to do it, because the values are still untranslated here.



So my question is how can I sort these options if the values are translated?










share|improve this question




























    up vote
    1
    down vote

    favorite












    I have a form with a language field with the language select list widget.
    The options of this field contains an array with a pattern like



    ['langcode' => t('language')]


    Now I want to sort these options in the right order.



    The default sorting is on the value but not on the translated one.



    e.g. ['de' => t('german')] which I translated into "Deutsch" will now sort under "g" instead of "d".



    So I did try to do it in a form alter hook and a preprocess hook but this is not the right place to do it, because the values are still untranslated here.



    So my question is how can I sort these options if the values are translated?










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a form with a language field with the language select list widget.
      The options of this field contains an array with a pattern like



      ['langcode' => t('language')]


      Now I want to sort these options in the right order.



      The default sorting is on the value but not on the translated one.



      e.g. ['de' => t('german')] which I translated into "Deutsch" will now sort under "g" instead of "d".



      So I did try to do it in a form alter hook and a preprocess hook but this is not the right place to do it, because the values are still untranslated here.



      So my question is how can I sort these options if the values are translated?










      share|improve this question













      I have a form with a language field with the language select list widget.
      The options of this field contains an array with a pattern like



      ['langcode' => t('language')]


      Now I want to sort these options in the right order.



      The default sorting is on the value but not on the translated one.



      e.g. ['de' => t('german')] which I translated into "Deutsch" will now sort under "g" instead of "d".



      So I did try to do it in a form alter hook and a preprocess hook but this is not the right place to do it, because the values are still untranslated here.



      So my question is how can I sort these options if the values are translated?







      8 forms i18n-l10n






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 at 7:55









      Insasse

      421317




      421317






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          i think it can be done in a hook_form_alter()



          if the options are in $form['languages'], you can do something like :



          $language_options = $form['languages'];


          then manipulate the array $language_options, until it has the order you need
          then override it in form :



          $form['languages'] = $language_options;





          share|improve this answer





















          • The problem is that the values are untranslated in a form_alter_hook. I could sort it by key but this also doesnt work because the langcode differs from the value.
            – Insasse
            Nov 13 at 9:50










          • I need to render the translatablemarkup first than build a new array and sort its values.
            – Insasse
            Nov 13 at 10:01


















          up vote
          1
          down vote



          accepted










          Now I did this in a form_alter_hook.



          This worked for me.



          /**
          * Implements hook_form_FORM_ID_alter().
          */
          function arph_profile_form_FORM_ID_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
          $language_options = $form["field_languages"]["widget"][0]["value"]["#options"];
          $new_language_options = ;
          foreach($language_options as $langcode => $language) {
          /** @var DrupalCoreStringTranslationTranslatableMarkup $language */
          $new_language_options[$langcode] = $language->render();
          }
          asort($new_language_options); // We need to keep the keys here.
          $form["field_languages"]["widget"][0]["value"]["#options"] = $new_language_options;
          }





          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "220"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f272386%2fhow-to-sort-language-select-list-on-current-language-context%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            i think it can be done in a hook_form_alter()



            if the options are in $form['languages'], you can do something like :



            $language_options = $form['languages'];


            then manipulate the array $language_options, until it has the order you need
            then override it in form :



            $form['languages'] = $language_options;





            share|improve this answer





















            • The problem is that the values are untranslated in a form_alter_hook. I could sort it by key but this also doesnt work because the langcode differs from the value.
              – Insasse
              Nov 13 at 9:50










            • I need to render the translatablemarkup first than build a new array and sort its values.
              – Insasse
              Nov 13 at 10:01















            up vote
            1
            down vote













            i think it can be done in a hook_form_alter()



            if the options are in $form['languages'], you can do something like :



            $language_options = $form['languages'];


            then manipulate the array $language_options, until it has the order you need
            then override it in form :



            $form['languages'] = $language_options;





            share|improve this answer





















            • The problem is that the values are untranslated in a form_alter_hook. I could sort it by key but this also doesnt work because the langcode differs from the value.
              – Insasse
              Nov 13 at 9:50










            • I need to render the translatablemarkup first than build a new array and sort its values.
              – Insasse
              Nov 13 at 10:01













            up vote
            1
            down vote










            up vote
            1
            down vote









            i think it can be done in a hook_form_alter()



            if the options are in $form['languages'], you can do something like :



            $language_options = $form['languages'];


            then manipulate the array $language_options, until it has the order you need
            then override it in form :



            $form['languages'] = $language_options;





            share|improve this answer












            i think it can be done in a hook_form_alter()



            if the options are in $form['languages'], you can do something like :



            $language_options = $form['languages'];


            then manipulate the array $language_options, until it has the order you need
            then override it in form :



            $form['languages'] = $language_options;






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 at 9:34









            izus

            448213




            448213












            • The problem is that the values are untranslated in a form_alter_hook. I could sort it by key but this also doesnt work because the langcode differs from the value.
              – Insasse
              Nov 13 at 9:50










            • I need to render the translatablemarkup first than build a new array and sort its values.
              – Insasse
              Nov 13 at 10:01


















            • The problem is that the values are untranslated in a form_alter_hook. I could sort it by key but this also doesnt work because the langcode differs from the value.
              – Insasse
              Nov 13 at 9:50










            • I need to render the translatablemarkup first than build a new array and sort its values.
              – Insasse
              Nov 13 at 10:01
















            The problem is that the values are untranslated in a form_alter_hook. I could sort it by key but this also doesnt work because the langcode differs from the value.
            – Insasse
            Nov 13 at 9:50




            The problem is that the values are untranslated in a form_alter_hook. I could sort it by key but this also doesnt work because the langcode differs from the value.
            – Insasse
            Nov 13 at 9:50












            I need to render the translatablemarkup first than build a new array and sort its values.
            – Insasse
            Nov 13 at 10:01




            I need to render the translatablemarkup first than build a new array and sort its values.
            – Insasse
            Nov 13 at 10:01












            up vote
            1
            down vote



            accepted










            Now I did this in a form_alter_hook.



            This worked for me.



            /**
            * Implements hook_form_FORM_ID_alter().
            */
            function arph_profile_form_FORM_ID_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
            $language_options = $form["field_languages"]["widget"][0]["value"]["#options"];
            $new_language_options = ;
            foreach($language_options as $langcode => $language) {
            /** @var DrupalCoreStringTranslationTranslatableMarkup $language */
            $new_language_options[$langcode] = $language->render();
            }
            asort($new_language_options); // We need to keep the keys here.
            $form["field_languages"]["widget"][0]["value"]["#options"] = $new_language_options;
            }





            share|improve this answer



























              up vote
              1
              down vote



              accepted










              Now I did this in a form_alter_hook.



              This worked for me.



              /**
              * Implements hook_form_FORM_ID_alter().
              */
              function arph_profile_form_FORM_ID_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
              $language_options = $form["field_languages"]["widget"][0]["value"]["#options"];
              $new_language_options = ;
              foreach($language_options as $langcode => $language) {
              /** @var DrupalCoreStringTranslationTranslatableMarkup $language */
              $new_language_options[$langcode] = $language->render();
              }
              asort($new_language_options); // We need to keep the keys here.
              $form["field_languages"]["widget"][0]["value"]["#options"] = $new_language_options;
              }





              share|improve this answer

























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                Now I did this in a form_alter_hook.



                This worked for me.



                /**
                * Implements hook_form_FORM_ID_alter().
                */
                function arph_profile_form_FORM_ID_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
                $language_options = $form["field_languages"]["widget"][0]["value"]["#options"];
                $new_language_options = ;
                foreach($language_options as $langcode => $language) {
                /** @var DrupalCoreStringTranslationTranslatableMarkup $language */
                $new_language_options[$langcode] = $language->render();
                }
                asort($new_language_options); // We need to keep the keys here.
                $form["field_languages"]["widget"][0]["value"]["#options"] = $new_language_options;
                }





                share|improve this answer














                Now I did this in a form_alter_hook.



                This worked for me.



                /**
                * Implements hook_form_FORM_ID_alter().
                */
                function arph_profile_form_FORM_ID_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
                $language_options = $form["field_languages"]["widget"][0]["value"]["#options"];
                $new_language_options = ;
                foreach($language_options as $langcode => $language) {
                /** @var DrupalCoreStringTranslationTranslatableMarkup $language */
                $new_language_options[$langcode] = $language->render();
                }
                asort($new_language_options); // We need to keep the keys here.
                $form["field_languages"]["widget"][0]["value"]["#options"] = $new_language_options;
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 at 11:49

























                answered Nov 13 at 10:00









                Insasse

                421317




                421317






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f272386%2fhow-to-sort-language-select-list-on-current-language-context%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                    ComboBox Display Member on multiple fields

                    Is it possible to collect Nectar points via Trainline?