Fixed Template compiler variable setting

Affected version
2.1.1

Lukas W.

Formerly katsulynx
Running the following template modification:

Template:
admin:helper_criteria

Search:
Code:
/(<xf:foreach loop="\$customFields".*>.*<\/xf:option>)(\s*<\/xf:checkboxrow>)/sU
Replace:
Code:
$1
                                        <xf:set var="$fieldName" value="thusercriteria_user_field_checked_{$fieldId}" />
                                        <xf:option name="user_criteria[{$fieldName}][rule]" selected="{$criteria.{$fieldName}}" value="{$fieldName}" label="{{ phrase('thusercriteria_has_value') }}"/>

                                        <xf:set var="$fieldName" value="thusercriteria_user_field_unchecked_{$fieldId}" />
                                        <xf:option name="user_criteria[{$fieldName}][rule]" selected="{$criteria.{$fieldName}}" value="{$fieldName}" label="{{ phrase('thusercriteria_has_no_value') }}"/>

                                        <xf:set var="$fieldName" value="thusercriteria_user_field_preg-match_{$fieldId}" />
                                        <xf:option name="user_criteria[{$fieldName}][rule]" value="{$fieldName}" selected="{$criteria.{$fieldName}}" label="{{ phrase('thusercriteria_preg_matches:') }}">
                                            <xf:textbox name="user_criteria[{$fieldName}][data][regex]" value="{$criteria.{$fieldName}.regex}" />
                                        </xf:option>
The template compiler will compile this to the following code:
Code:
                $__vars['fieldName'] = 'thusercriteria_user_field_checked_' . $__vars['fieldId'];
               $__vars['fieldName'] = 'thusercriteria_user_field_unchecked_' . $__vars['fieldId'];
               $__vars['fieldName'] = 'thusercriteria_user_field_preg-match_' . $__vars['fieldId'];
               $__compilerTemp24 = array(array(
                  'name' => 'user_criteria[' . $__vars['fieldName'] . '][rule]',
                  'value' => $__vars['fieldName'],
                  'selected' => $__vars['criteria'][$__vars['fieldName']],
                  'label' => ($__vars['choices'] ? 'User choice is among' : 'User value contains text' . $__vars['xf']['language']['label_separator']),
                  '_dependent' => array('
                                    ' . $__compilerTemp21 . '
                                 '),
                  '_type' => 'option',
               )
,array(
                  'name' => 'user_criteria[' . $__vars['fieldName'] . '][rule]',
                  'selected' => $__vars['criteria'][$__vars['fieldName']],
                  'value' => $__vars['fieldName'],
                  'label' => 'User has provided a value',
                  '_type' => 'option',
               )
,array(
                  'name' => 'user_criteria[' . $__vars['fieldName'] . '][rule]',
                  'selected' => $__vars['criteria'][$__vars['fieldName']],
                  'value' => $__vars['fieldName'],
                  'label' => 'User has provided no value',
                  '_type' => 'option',
               )
,array(
                  'name' => 'user_criteria[' . $__vars['fieldName'] . '][rule]',
                  'value' => $__vars['fieldName'],
                  'selected' => $__vars['criteria'][$__vars['fieldName']],
                  'label' => 'User value matches regular expression' . $__vars['xf']['language']['label_separator'],
                  '_dependent' => array($__templater->formTextBox(array(
                  'name' => 'user_criteria[' . $__vars['fieldName'] . '][data][regex]',
                  'value' => $__vars['criteria'][$__vars['fieldName']]['regex'],
               ))),
                  '_type' => 'option',
               ));
Thus breaking the usage of $fieldName, as they are all three set one after another.

Changing the replace to
Code:
$1
<xf:if is="true">
                                        <xf:set var="$fieldName" value="thusercriteria_user_field_checked_{$fieldId}" />
                                        <xf:option name="user_criteria[{$fieldName}][rule]" selected="{$criteria.{$fieldName}}" value="{$fieldName}" label="{{ phrase('thusercriteria_has_value') }}"/>

                                        <xf:set var="$fieldName" value="thusercriteria_user_field_unchecked_{$fieldId}" />
                                        <xf:option name="user_criteria[{$fieldName}][rule]" selected="{$criteria.{$fieldName}}" value="{$fieldName}" label="{{ phrase('thusercriteria_has_no_value') }}"/>

                                        <xf:set var="$fieldName" value="thusercriteria_user_field_preg-match_{$fieldId}" />
                                        <xf:option name="user_criteria[{$fieldName}][rule]" value="{$fieldName}" selected="{$criteria.{$fieldName}}" label="{{ phrase('thusercriteria_preg_matches:') }}">
                                            <xf:textbox name="user_criteria[{$fieldName}][data][regex]" value="{$criteria.{$fieldName}.regex}" />
                                        </xf:option>
</xf:if>
will fix the issue and the compiler will set all variables on the right positions
Code:
if (true) {
   $__vars['fieldName'] = 'thusercriteria_user_field_checked_' . $__vars['fieldId'];
   $__compilerTemp24[] = array(
      'name' => 'user_criteria[' . $__vars['fieldName'] . '][rule]',
      'selected' => $__vars['criteria'][$__vars['fieldName']],
      'value' => $__vars['fieldName'],
      'label' => 'User has provided a value',
      '_type' => 'option',
   );
   $__vars['fieldName'] = 'thusercriteria_user_field_unchecked_' . $__vars['fieldId'];
   $__compilerTemp24[] = array(
      'name' => 'user_criteria[' . $__vars['fieldName'] . '][rule]',
      'selected' => $__vars['criteria'][$__vars['fieldName']],
      'value' => $__vars['fieldName'],
      'label' => 'User has provided no value',
      '_type' => 'option',
   );
   $__vars['fieldName'] = 'thusercriteria_user_field_preg-match_' . $__vars['fieldId'];
   $__compilerTemp24[] = array(
      'name' => 'user_criteria[' . $__vars['fieldName'] . '][rule]',
      'value' => $__vars['fieldName'],
      'selected' => $__vars['criteria'][$__vars['fieldName']],
      'label' => 'User value matches regular expression' . $__vars['xf']['language']['label_separator'],
      '_dependent' => array($__templater->formTextBox(array(
      'name' => 'user_criteria[' . $__vars['fieldName'] . '][data][regex]',
      'value' => $__vars['criteria'][$__vars['fieldName']]['regex'],
   ))),
      '_type' => 'option',
   );
}
 

Chris D

XenForo developer
Staff member
Forgot to post earlier.

We found some issues with the previous fix, so this is not included in XF 2.1.2.

As such, I've unmarked it as fixed and moved it back to the bug reports forum for consideration in the future.
 

XF Bug Bot

XenForo bug fixer bot
Staff member
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.1.7).

Change log:
Force choice builder to use temporary variable with set tags
There may be a delay before changes are rolled out to the XenForo Community.
 
Top