Drupal 8 and up don't like users to "do" PHP. That's somewhat understandable, although I feel the dreaded security risk is a tad exaggerated: a bit of basic user management can prevent a lot of harm. So, I for one Do like to have PHP available within Drupal, as it was in olden days - especially in a localhost development environment. Understandably, it was a very sad day when I found out that Twig templates don't accept PHP any more, like the PHP Template system used to do. I badly miss that! 

Still, https://www.drupal.org/project/php is still available and works. 

I keep facing a recurring problem, however. 

When I create a new node, I make sure PHP is the text format, and I 'change' the editor into "source" mode. My new node is ready to receive PHP now. I save, and when correct, the code is executed. Fine... The problem occurs when I want to edit my PHP node. The code is still there, but in my editor it is now changed into html - adding "<p>", replacing "<" with "smaller than" etc. My editor is in "PHP" and "source code" mode. Why is it "eating" my PHP code??

Question: how can I preserve the actual PHP code, without unwanted html being added? How can I do my editing work in Drupal itself, without massacring my original code?  Obviously, I can have a workaround by doing all my editing outside the Drupal editor and then copy-paste it in the node, but it would make a lot of sense to be able to stay within Drupal.

Any ideas?

Comments

wombatbuddy’s picture

Probably, if you love Drupal, you need to master the new paradigms of Drupal 8-9 and Symphony.

ludootje’s picture

Thanks for your reply. 
As for my problem, I tried putting PHP as the first text filter in the list of "Basic HTML", "Plain text" etc., but it did not (yet) work. Probably need to look a bit deeper into the paradigm.  
Any help is still appreciated, though: how can I preserve PHP code and prevent it to be turned into unwanted sort-of-HTML?
Thanks!

gisle’s picture

This happens because the CKeditor text editor is enabled for the "PHP code" text format.

Navigate to Home » Administration » Configuration » Content authoring » Text formats and editors, locate the "PHP code" format and click "Configure". There is a pull-down menu that lets you configure the "Text editor" for the format. Change it from "CKEditor" to "None". Finally, press "Save configuration" at the bottom of the page.

That should take care of it. If it doesn't, uninstall the PHP module, refresh the cache and reinstall it. The "PHP code" text format works well with the default settings and should not need any configuration of filters or Text editor before you use it.

PS: This configuration of "Text editor" is not enabled by default. Someone with admin access to your site must have enabled it by mistake.

- gisle

ludootje’s picture

Thanks so much, Gisle! Your advice did the trick. I can now begin a PHP node, change it, re-save it, and it's still all PHP :-) . You made my day (and part of my week too).

Thanks for the exemplary clarity of your help, exacttttttly pinpointing where I need to do what. Not all 'help' has that quality. In Drupal, the "where" is usually far more complicated than the "what". Thanks for being meticulously clear as to where I needed to go.

After installing the PHP module, I (being the admin) changed the setting myself to "CKEditor", thinking "Duh, of course I need to pick CKEditor, because that is the editor I use. So, I have to put it on 'Yes'." What I did not understand, however, was that "no editor" is, in the eyes of the makers, also an editor :-). A warning might have been appropriate...

Once more: very much appreciation and thanks.

jaypan’s picture

A warning might have been appropriate...

This is an opportunity for you to contribute back to the Drupal community. Drupal is open-source, and any user has the opportunity to suggest improvements, and if they have the skills, to create a patch, that can then be incorporated into Drupal, for the benefit of future users.

Your statement about 'would be appropriate' isn't exactly applicable in open-source, as it's community developed and driven (though actual direction is up to the Drupal organization), and therefore the applicable statement is 'would be nice...'. So in this case, you can open a ticket in the Drupal issue queue, explain the situation, and propose a solution. If you have the skills to code that solution, you can also contribute code to the problem to get it started, but that's not a requirement. Part of Drupal is just telling the people that have been around forever, the perspective of coming to it new. Other people can then pick up the issue at some point in the future if they so choose.

You can open an issue here: https://www.drupal.org/project/issues/drupal

Contact me to contract me for D7 -> D10/11 migrations.