Control's set_attribute can't be set to 0


When I try to add a attribute with the value 0, it remains empty in the frontend source code.
$this->set_attribute( 'wrapper', 'data-test', [ 0 ] );
in public function render() outputs
<... data-test="" ...> instead of <... data-test="0" ...>
In quotes it doesn’t work, too.

After digging in includes/elements/base.php, I found in line 1105 and 1260:
empty( $value ) and ! empty( $val ).
Because empty(0) == true, no value is shown in the frontend at data-test.

For test purposes I replaced
[1105] empty( $value ) with $value === null
[1260] ! empty( $val ) with isset( $val )
and it works fine for me.

Is there any reason for the current behavior or would you adopt my suggestion?

1 Like