I have managed to connect external Bootstrap of version 5.3.3 into my extension. More or less.
extend.php
:
<?php
// ...
return [
(new Extend\Frontend('forum'))
->content(function (Document $document) use ($htmlParts) {
$document->head = [
'<link rel="stylesheet" href="/flarum/assets/css/lib/bootstrap/bootstrap.css">',
];
$document->foot = [
// Prevent Bootstrap to inject itself into jQuery.
// Can't be written in <head>, as document.body isn't available there.
"<script>document.body.dataset.bsNoJquery = '';</script>",
// Bootstrap itself, can't be written in <head> because must be after setting data-bs-no-jquery
'<script src="/flarum/assets/js/lib/bootstrap/bootstrap.bundle.min.js" defer></script>',
];
}),
];
Alternative solution is to hack into bootstrap.bundle.js
and comment out search for jQuery:
const getjQuery = () => {
//if (window.jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
// return window.jQuery;
//}
return null;
};
Then Bootstrap can be included in the head, and without setting data-bs-no-jquery
.
I put JS and CSS files directly into /assets/
folder. I tried to add the CSS via standard way:
(new Extend\Frontend('forum'))->css(__DIR__.'/css/lib/bootstrap.css'),
But didn't succeed, as I'm getting an error: error evaluating function
rgbacolor functions take numbers as parameters index
. It's might be because of outdated LESS version of Flarum.
Before I could use bootstrap.css
I had to tediously replace 5.3.3 CSS classes to prevent clashes with the existing styles, like converting classes from .modal
to .bs-modal
.
And finally I'm able to call:
let modal = new bootstrap.Modal('[data-js-foobar]');
modal.show();
Fortunately, this is not a public extension, it's for a local project, so I can kinda live with this loopy approach. Currently I don't know more correct soultions anyway.