Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set_attribute method to Block class #45523

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'] );
}
}