Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-73332

Fix template shortentext helper - length as variable

XMLWordPrintable

    • MOODLE_311_STABLE, MOODLE_400_STABLE
    • MOODLE_311_STABLE, MOODLE_400_STABLE
    • MDL-73332-master
    • Hide

      Steps to test

      1. Login as admin
      2. Create a new course, course1, and add an activity forum to it.
      3. Add a new post to the forum with a lot of words.
      4. Modify forum_discussion_post to

          {{#shortentext}} {{maxlength}}, {{{message}}} {{/shortentext}} 
          

      5. After this line here add

            'maxlength' => 24, 
           

      6. After this line here add

            'maxlength' => ['type' => PARAM_INT],
            

      7. The user should see roughly 24 characters.
      Show
      Steps to test Login as admin Create a new course, course1, and add an activity forum to it. Add a new post to the forum with a lot of words. Modify forum_discussion_post to {{#shortentext}} {{maxlength}}, {{{message}}} {{/shortentext}} After this line here add 'maxlength' => 24 , After this line here add 'maxlength' => [ 'type' => PARAM_INT], The user should see roughly 24 characters.

      In the documentation on templates, the section on the shortentext helper states:

      "The helper takes two comma separated arguments. The first is the desired length and the second is the text to be shortened. Both can be provided as context variables." (emphasis mine)

      However, I was trying to provide the length argument as a context variable, and it wasn't working. After looking at lib/classes/output/mustache_shorten_text_helper.php, it was obvious that second argument, text, was looking for context variables but the first, length,  was not. 

       // Split the text into an array of variables.
       list($length, $text) = explode(',', $args, 2);
       $length = trim($length);
       $text = trim($text);
       
       // Allow mustache tags in the text.
       $text = $helper->render($text);
       
       return shorten_text($text, $length)

      Again, emphasis mine.

      After some testing, adding

      $length = $helper->render($length);

      does, indeed, seems to fix the problem and align the code with the documentation.

      Obviously this isn't a well-exercised issue - or it would have been found by now, so I'm including a patch for the current main branch.

       

       

       

        1. MDL-73332_master_step 3.png
          MDL-73332_master_step 3.png
          175 kB
        2. MDL-73332_master_step 7.png
          MDL-73332_master_step 7.png
          55 kB
        3. MDL-73332_v311_step 3.png
          MDL-73332_v311_step 3.png
          188 kB
        4. MDL-73332_v311_step 7.png
          MDL-73332_v311_step 7.png
          63 kB
        5. MDL-73332_v400_step 3.png
          MDL-73332_v400_step 3.png
          163 kB
        6. MDL-73332_v400_step 7.png
          MDL-73332_v400_step 7.png
          47 kB

            martygilbert Marty Gilbert
            martygilbert Marty Gilbert
            Sujith Haridasan Sujith Haridasan
            Victor Déniz Falcón Victor Déniz Falcón
            Angelia Dela Cruz Angelia Dela Cruz
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 2 hours, 10 minutes
                2h 10m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.