Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #303 from alleyinteractive/hotfix/IRV-909/theme-co…
Browse files Browse the repository at this point in the history
…nfig-callback

Fix ability to override theme in a config callback
  • Loading branch information
amyevans authored Dec 9, 2020
2 parents 341de83 + a418111 commit 7071ed3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion inc/components/class-component.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private function process_args( array $args ): self {
->apply_context()
->set_config( $args['config'] )
->hydrate_config()
->set_theme( 'irving/site-theme' !== $this->get_name() ? $args['config']['theme'] : $args['theme'] )
->set_theme( 'irving/site-theme' !== $this->get_name() ? $this->get_config( 'theme' ) : $args['theme'] )
->set_children( $args['children'] );

return $this;
Expand Down
33 changes: 32 additions & 1 deletion tests/components/test-class-component.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,11 @@ public function get_config_for_test_schema(): array {
[
[
'test-default' => 'overridden',
'theme' => 'overridden',
],
[
'test-default' => 'overridden',
'theme' => 'default',
'theme' => 'overridden',
],
'Default config values not overridden.',
],
Expand Down Expand Up @@ -475,6 +476,36 @@ public function test_config_callback() {
);
}

/**
* Test overriding `theme` in a config callback.
*/
public function test_config_callback_for_theme() {
$name = 'example/component';

/*
* Register component with callback.
*
* We're using a callback that uses the config in order
* to ensure that the callback is passed an array.
*/
get_registry()->register_component(
$name,
[
'config_callback' => function ( array $config ) {
$config['theme'] = 'fred';
return $config;
},
]
);

$component = new Component( $name );

// Clean up.
get_registry()->unregister_component( $name );

$this->assertEquals( 'fred', $component->get_theme() );
}

/**
* Test children set from config.
*/
Expand Down

0 comments on commit 7071ed3

Please sign in to comment.