Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
It's more like an errormessage dismiss request .. for this notice
Notice: Array to string conversion in Drupal\bootstrap\Bootstrap::cssClassFromString() (line 292 of themes/bootstrap/src/Bootstrap.php).
This is caused by Element.php's
if ($value = $this->getProperty('value', $this->getProperty('title'))) {
$class = "$prefix-" . Bootstrap::cssClassFromString($value, $this->getProperty('button_type', 'default'));
$this->addClass($class);
if ($button && $this->getProperty('split')) {
$this->addClass($class, $this::SPLIT_BUTTON);
}
}
As a button can contain a markup array this should be altered to something like
if ($value = $this->getProperty('value', $this->getProperty('title'))) {
if (is_array($value)) {
if (array_key_exists('#markup', $value)) {
$value = $value['#markup'];
}
else {
$value = implode(" ", $value);
}
}
$class = "$prefix-" . Bootstrap::cssClassFromString($value, $this->getProperty('button_type', 'default'));
$this->addClass($class);
if ($button && $this->getProperty('split')) {
$this->addClass($class, $this::SPLIT_BUTTON);
}
}
The same issue with ::glyphiconFromString
Comment | File | Size | Author |
---|---|---|---|
#19 | handle_render_arrays-2820849-19.patch | 3.69 KB | markhalliwell |
Comments
Comment #2
ytsurkComment #3
ytsurkComment #4
markhalliwellI think it makes more sense to check this in the methods that are consuming the passed value instead.
Furthermore, I also think it would be far easier to do something like the following so it encompasses everything (not just
#markup
):Comment #5
markhalliwellBetter title
Comment #6
ytsurkThx for your thoughts.
I also thought of changing it in the consuming methods, but there is an explicit str()-conversion - actually causing the notice.
This somehow made me do it outside ...
The isRenderArray check is awesome !
Comment #7
ytsurkOK - here's a complete rewrite of my first patch.
I extended Drupal\bootstrap\Utility\Element with one method called renderOrFlattenValue().
This method is meant to be reusable in all similar places .., so far I found no other.
Comment #8
ytsurksomehow missed the file ...
Comment #9
ytsurkComment #10
ytsurkimproved: return faster, do not cast strings to string ...
Comment #11
ytsurkand another tested one ....
Comment #12
markhalliwellMy solution in #4 is all that is needed. A new "flatten" method is unnecessarily verbose. It implements type checking and "optimizations" that are not needed.
Comment #13
ytsurkSorry - my bad .. i was using 8.x-3.0-rc2 and did not find you mentioned methods ..,
here's the patch like I understand your intensions, against the 8.x-3.x-dev branch.
BTW - thank you for all your efforts for this great, alive and real object-oriented theme.
Comment #14
ytsurkComment #15
markhalliwellI just remembered that
renderPlain
actually returns an instance of\Drupal\Component\Render\MarkupInterface
. So both values will need to be typecast to a string, which we can do like the following:Comment #16
markhalliwellMy pleasure :D
Comment #17
markhalliwellWe should also change the method signature so it passes the string by reference, e.g.
(&$string)
. That way, if it is a render array, it can set the necessary#printed
property when it gets rendered (and doesn't render twice) later.Comment #18
ytsurkOkey - passing as reference is a great performance enhancement, yes.
I also added the typecasting to string, but I had to use
(string)
, with''+
I tries to cast it to a int and we have another Notice ?! (wierd..)Comment #19
markhalliwellAwesome! Thanks @ytsurk, this is definitely on the right track.
I went ahead and made a few tweaks to the patch, mostly around documentation and introducing a new
Bootstrap::toString()
helper method (since this isn't likely the only place this will need it).