Skip to content

Commit

Permalink
Merge pull request #1 from nanuc/main
Browse files Browse the repository at this point in the history
Add Metabase's additional parameters
  • Loading branch information
uyab committed Dec 22, 2021
2 parents 8ee8a0a + 2f4d5e9 commit 26637df
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ Metabase secret key can be found in metabase settings page (only accessible by a
@php($params = ['category' => 'php'])
<x-metabase dashboard="1" :params="$params"></x-metabase>
// BEWARE of the colon in ":params" (not "param") because we are passing array variable directly to the component
<!-- passing additional metabase parameters -->
<x-metabase dashboard="1" :params="$params" :bordered="false" titled theme="night"></x-metabase>
```

## Common Problems
Expand Down
25 changes: 24 additions & 1 deletion src/MetabaseComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class MetabaseComponent extends Component

public ?int $question;

public bool $bordered;

public bool $titled;

public ?string $theme;

/**
* @var string[]
*/
Expand All @@ -22,11 +28,14 @@ class MetabaseComponent extends Component
* @param int|null $question
* @param array<string> $params
*/
public function __construct(?int $dashboard = null, ?int $question = null, array $params = [])
public function __construct(?int $dashboard = null, ?int $question = null, array $params = [], $bordered = false, $titled = false, $theme = null)
{
$this->dashboard = $dashboard;
$this->question = $question;
$this->params = $params;
$this->bordered = $bordered;
$this->titled = $titled;
$this->theme = $theme;
}

/**
Expand All @@ -38,7 +47,21 @@ public function render()
{
$metabase = app(MetabaseService::class);
$metabase->setParams($this->params);
$metabase->setAdditionalParams($this->getAdditionalParams());
$iframeUrl = $metabase->generateEmbedUrl((int) $this->dashboard, (int) $this->question);
return view('metabase::iframe', compact('iframeUrl'));
}


private function getAdditionalParams()
{
$additionalParameters['bordered'] = $this->bordered;
$additionalParameters['titled'] = $this->titled;

if($this->theme) {
$additionalParameters['theme'] = $this->theme;
}

return $additionalParameters;
}
}
17 changes: 16 additions & 1 deletion src/MetabaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class MetabaseService
*/
private $params;

/**
* @var array<string> @params
*/
private $additionalParams;

/**
* @param array<string> $params
*
Expand All @@ -28,6 +33,16 @@ public function setParams(array $params): void
$this->params = $params;
}

/**
* @param array $params
*
* @return void
*/
public function setAdditionalParams(array $params): void
{
$this->additionalParams = $params;
}

private string $type = 'dashboard';

/**
Expand Down Expand Up @@ -62,7 +77,7 @@ public function generateEmbedUrl(?int $dashboard, ?int $question): string
->toString();

return sprintf(
'%s/embed/%s/%s#bordered=true&titled=false',
'%s/embed/%s/%s#' . http_build_query($this->additionalParams),
config('services.metabase.url'),
$this->type,
$token
Expand Down

0 comments on commit 26637df

Please sign in to comment.