I am new to Drupal 8. I am building a website with Drupal 8, and it has many blocks contain images. They have an absolute path for the src attribute right now. I would like to make them dynamic base on the base URL, but there is no text format support PHP code anymore for security reason. Is there a safe way to achieve this?


You could and should do your PHP logic with template_preprocess_block. As creating a programmatic block just so you can use PHP is more work than necessary, overkill, for this use case.

Also, without any more specific info regarding make them dynamic based on base URL, this is as far as I can go.

  • Cheers man, thank you for providing that solution. – Siyu Qian Feb 3 '17 at 20:35

PHP filter is a core module in Drupal 7 but in Drupal 8 it is removed because of some security reason but still you can use this module PHP.

Note: Enabling this module can cause security and performance issues as it allows users to execute PHP code on your site.

  • 1
    Hi, Anil.I know it is available out there man it says "security and performance issues", and it also says "There are better alternatives out there that do not expose such vulnerabilities on your site". So, I guess there are better solutions to this situation right? – Siyu Qian Feb 3 '17 at 9:47
  • Yes, you are better off to create a block in code, rather than putting PHP in the database, which is what the PHP module does. Here are instructions on how to create a block in code: drupal.org/docs/8/creating-custom-modules/create-a-custom-block – Jaypan Feb 3 '17 at 9:59
  • You can create the block as a custom block in a custom module, rather than using the contributed module – Anu Mathew Feb 3 '17 at 10:04
  • Yes, then you can create a custom block as suggested in above comments. – Anil Feb 3 '17 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.