Add set_attribute method to Block class (#45523)
This commit is contained in:
parent
50beebe3e5
commit
8bef21dbc3
|
@ -0,0 +1,4 @@
|
|||
Significance: minor
|
||||
Type: update
|
||||
|
||||
Add set_attribute method to Block class
|
|
@ -70,6 +70,14 @@ interface BlockInterface {
|
|||
*/
|
||||
public function set_attributes( array $attributes );
|
||||
|
||||
/**
|
||||
* Set a block attribute value without replacing the entire attributes object.
|
||||
*
|
||||
* @param string $key The attribute key.
|
||||
* @param mixed $value The attribute value.
|
||||
*/
|
||||
public function set_attribute( string $key, $value );
|
||||
|
||||
/**
|
||||
* Get the parent container that the block belongs to.
|
||||
*/
|
||||
|
|
|
@ -201,6 +201,16 @@ class AbstractBlock implements BlockInterface {
|
|||
$this->attributes = $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a block attribute value without replacing the entire attributes object.
|
||||
*
|
||||
* @param string $key The attribute key.
|
||||
* @param mixed $value The attribute value.
|
||||
*/
|
||||
public function set_attribute( string $key, $value ) {
|
||||
$this->attributes[ $key ] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the template that this block belongs to.
|
||||
*/
|
||||
|
|
|
@ -657,4 +657,21 @@ class BlockTest extends WC_Unit_Test_Case {
|
|||
'Failed asserting that the inner blocks are sorted by order.'
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Test for set_attribute method.
|
||||
*/
|
||||
public function test_set_attribute() {
|
||||
$template = new BlockTemplate();
|
||||
|
||||
$block = new Block(
|
||||
array(
|
||||
'blockName' => 'test-block-name',
|
||||
),
|
||||
$template
|
||||
);
|
||||
|
||||
$block->set_attribute( 'test-attr', 'test-value' );
|
||||
|
||||
$this->assertSame( 'test-value', $block->get_attributes()['test-attr'] );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue