Fixed CodeMirror PHP mode is broken in ACP

Affected version
2.0.1 - 2.0.2

CMTV

Active member
#1
Hi!

Put the code below in any admin template (for example in addon_list):
HTML:
<xf:codeeditorrow mode="php" data-line-wrapping="true" class="codeEditor--autoSize" />
This code creates code editor row without any errors:
php_mode.png

But the code is not highlighting and throws an error when hitting "Enter":
php_mode_error.png

I think this error happens because of the incorrect mode calls order. "clike" mode should be loaded first and only after that "php" mode should be loaded. So I tried to change the order in CodeLanguage class:
PHP:
//
// Before:
// 'php',
// 'clike'
//
// Now:
'php' => [
    'modes' => [
        'clike',
        'php'
    ],
    'mime' => 'text/x-php',
    'common' => true
],
I did not notice any errors and the editor is even making automatic tabulation but still no highlighting...
modified.png

Moreover, function has a cm-variable class but it must has cm-keyword class so there is something working completely wrong with mode="php"...

Other modes (html, javascript) are working fine:
html_is_working_fine.png


UPD 1: tried to change cm-variable class to cm-keyword. The word was highlighted correctly so everything is okay with LESS/CSS side.
 
Last edited:

Chris D

XenForo developer
Staff member
#2
I'm not totally sure I understand exactly how this was working with any of the modes, such as the ones you identified that work without issue. There's nothing in the JS that explicitly sets the mode so there must be some sort of magic that happens with some modes but not others, otherwise code editing/syntax highlighting will never have worked. The exception perhaps being when using the rich text editor code editor, which dynamically loads the selected modes in anyway.

Regardless, the solution is mostly to just add mode: this.options.mode into the Code Mirror initialisation and that sorts it. Though I have made some other changes.

I consciously decided that the PHP mode should only be PHP syntax highlighting, rather than being mixed, but I think that decision was perhaps a bit harsh (after all, a lot of people do still use HTML inline within PHP) so the PHP mode now loads all of these, with a different mime type:
PHP:
'modes' => [
   'htmlmixed',
   'xml',
   'javascript',
   'css',
   'clike',
   'php'
],
'mime' => 'application/x-httpd-php',
Also, you will note, the modes are loaded in the correct order now.

So this should be sorted for the next release, thank you.
 
Top