2

This worked in Drupal 7 but isn't in Drupal 8.

$form['covid-19'] = array(
    '#markup' => '<div style="border: 2px solid red;padding:10px;margin-bottom:10px;"><strong>Due to the COVID-19 situation, we will only be purchasing e-books and e-audiobooks at this time.</strong> You are welcome to enter your information for print books, DVDs, or CDs, and they will be purchased when the libraries re-open.  Inter-library Loans [loans from outside Yavapai County] are suspended for now.</div>',
);

I want the div with style info to stay intact so I can just comment it out when the crisis is over. Then I want to leave it there as a placeholder ready for the next crisis.

In this example, the strong tag makes it through but the div does not.

I also tried setting the div info as a #prefix and #suffix but that didn't work either.

Is there a way to do this?

Thank you!

  • 1
    See Pass raw html to #markup for numerous workarounds to the (intentional) filtering of markup on rendering. Personally, I think it would probably be easier to just add a .crisis class to your site css... – stevekeiretsu Mar 26 at 23:49
  • Thanks for that link. Here's what worked for me: use Drupal\Core\Render\Markup; [..] return [ '#markup' => Markup::create($html), ]; – Chanel Mar 27 at 16:20
0

Markup strips away some tags and attributes. Here is a way to do it.

Add a class to your form

$form['#attributes']['class'][] = 'crisis';

Add css rule to your theme's css

.crisis {
    display: none;
} 

if you do not have a library and a .css file to use, you need to create them. Define a library in your theme or module in your THEMEorMODULE.libraries.yml

crisis:
  version: 1.x
  css:
    theme:
      css/crisis.css: {}

create css/crisis.css file and put your css rule(s) in it

and attach the library to your form

$form['#attached']['library'][] = 'THEMEorMODULE/crisis'

and clear cache

NOTE: The above (pseudo)code is not tested. For more information please have a look here

| improve this answer | |
  • 1
    This is valuable information and I'm sure I'm going to use it soon. But for this situation I really wanted to keep everything in one place. – Chanel Mar 27 at 16:22

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.