Hi,

I'm fairly new to Backdrop so apologies for a basic question.

I have tried added some simple php to the body of a page using the edit tab and the source option, and using the full html editor.

When I save the page my <?php tag is comment out i.e. replaced with <!--?php.

What am I doing wrong?

Thanks.

Accepted answer

You're missing the 'PHP filter' module which makes this possible. However this was removed from Backdrop core and I don't believe a replacement exists in contrib. This was for security reasons, so I think the recommended way to add custom PHP code is either via a module or theme function...

Comments

mazze's picture

Hi Richard,

welcome to Backdrop... so did you enter PHP code into the Text field? Or did you edit a template file?

 

 

Hi and thanks.

When logged into my website as admin I get two tabs, View and Edit. I selected Edit and entered the code into the Body (Edit Summary) part of the page. (Is this called a block?)

You're missing the 'PHP filter' module which makes this possible. However this was removed from Backdrop core and I don't believe a replacement exists in contrib. This was for security reasons, so I think the recommended way to add custom PHP code is either via a module or theme function...

Ok thanks. Sorry for the big delay in replying. What I can't figure out is, once I've written a php module, how to I actually call that functionality in a backdrop webpage? All the php examples/tutorials online assume that the php is embedded in an html page within <?php... ?> tags which are unsupported in Backdrop.

indigoxela's picture

What I can't figure out is, once I've written a php module, how to I actually call that functionality in a backdrop webpage?

Backdrop uses "hooks" for that and has tons of them. If your module wants to inject something into page markup, you'd, for instance, use template_preprocess_page().

Doing the first steps with Backdrop might be easier, if you start with a theme, though.

You still have all the hooks available, but it's easier to put something in a template file (*.tpl.php).

You'd copy the base template file to your theme, flush caches, and adapt what you need.

Maybe it helps to have a look at how the core themes like Basis do things with templates.

If you can't figure out which template file to copy, ask here in the forum.

 

Thanks. I read the API documentation about hooks but I couldn't find an example of the syntax you use to call a hook within the markup of a webpage.

indigoxela's picture

...to call a hook within the markup of a webpage

Hooks are typically called from a xxx.module file or from a theme's template.php.

Here's a hook called from a theme which adapts some page variables, adds some css...

Here's a hook called from another theme that adapts the markup of a layout.

If you want to add some php code directly to a template file, you don't need hooks.

But maybe I totally misunderstand what you want to achieve...

What exactly would you like to add to the body of a page or pages? Content? Markup?

I'm probably thinking about it in the wrong way. All the examples I have seen of using php put the php code within <?php tags within the body of html page however that is not supported within Backdrop. So I was trying to figure out how to modify the body of the html page to get at a variable . So for example, if I had some php code that read a value, say some exif data from a photo, how would I call the php from that page and then get that value back to the page to display it.

bugfolder's picture

@richard, there's multiple ways of accomplishing this, depending on how you want it displayed.

If it's something that you want to display on its own, as if it were a field in a content type, you can implement hook_field_extra_fields(), which creates "pseudo-fields" that you can then manage through the display settings for the content type.

A quick and dirty way of doing it would be to put some placeholder text in the code (e.g., EXIF_DATA), then use hook_node_alter() to replace it with your data programmatically. You should be careful with this, though, since there could be unintended side effects if that placeholder text happens to show up elsewhere (e.g., via user entry).

A cleaner way to do the same thing would be to use the Token system. See the documentation on hook_token_info() to get started. Also this post.

Thanks. Your first suggestion makes sense as I know where the content display settings are in the admin menu - admin/structure/content types/foobar content type/manage displays etc.

I've looked at the hook_token_info documentation and once again there is lots of information about what is going on server-side but very little about how to use it. Does the token magically appear under the admin menu structure somewhere? if so, where? If not how does it get into a page? Some examples in the documentation would really help.

bugfolder's picture

I totally agree with your "would be nice in the docs" comments. I confess that I've not used the token system myself (it's been on my "learn how to do this" list for a while), but I've used the  hook_node_alter()  technique a fair amount (with suitable security precautions: placeholders only on admin-only-editable pages).

Since BD is very close to D7, you can find a lot of useful instruction and examples for how to do things in BD by searching for the equivalent thing in D7. For example, this page gives a nice description of how to add tokens to text fields using the Token Filter module (and here's the Backdrop version of that module).