Skip to content

Commit

Permalink
Add set_attribute method to Block class
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanss committed Mar 12, 2024
1 parent 1c068ce commit b7761fb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions plugins/woocommerce/changelog/update-set_attr_block
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: update

Add set_attribute method to Block class
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public function get_attributes(): array;
*/
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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ public function set_attributes( array $attributes ) {
$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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,4 +657,21 @@ public function test_get_formatted_template_with_sorting() {
'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'] );
}
}

0 comments on commit b7761fb

Please sign in to comment.