-
Notifications
You must be signed in to change notification settings - Fork 705
[6.x] Add custom dashboard widgets #19319
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
Draft
riasvdv
wants to merge
4
commits into
6.x
Choose a base branch
from
rias/cms-1243-custom-widgets
base: 6.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
efaa2a3
Add custom dashboard widgets
riasvdv 6ba0ca9
Coerce custom widget frontmatter
riasvdv 8476ae8
Merge remote-tracking branch 'origin/6.x' into rias/cms-1243-custom-w…
riasvdv 543f692
Merge branch '6.x' into rias/cms-1243-custom-widgets
riasvdv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace CraftCms\Cms\Dashboard; | ||
|
|
||
| use CraftCms\Cms\Dashboard\Data\CustomWidgetDefinition; | ||
| use Illuminate\Container\Attributes\Singleton; | ||
| use Illuminate\Support\Collection; | ||
| use Illuminate\Support\Facades\File; | ||
| use InvalidArgumentException; | ||
| use League\CommonMark\Extension\FrontMatter\Data\SymfonyYamlFrontMatterParser; | ||
| use League\CommonMark\Extension\FrontMatter\FrontMatterParser; | ||
| use Symfony\Component\Finder\SplFileInfo; | ||
|
|
||
| #[Singleton] | ||
| class CustomWidgets | ||
| { | ||
| /** @var Collection<string, CustomWidgetDefinition>|null */ | ||
| private ?Collection $definitions = null; | ||
|
|
||
| private readonly FrontMatterParser $frontMatterParser; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->frontMatterParser = new FrontMatterParser(new SymfonyYamlFrontMatterParser); | ||
| } | ||
|
|
||
| /** | ||
| * @return Collection<string, CustomWidgetDefinition> | ||
| */ | ||
| public function all(): Collection | ||
| { | ||
| if ($this->definitions !== null) { | ||
| return $this->definitions; | ||
| } | ||
|
|
||
| $path = resource_path('widgets'); | ||
|
|
||
| if (! is_dir($path)) { | ||
| return $this->definitions = Collection::make(); | ||
| } | ||
|
|
||
| $definitions = Collection::make(); | ||
| $handles = []; | ||
|
|
||
| foreach (File::files($path, hidden: true) as $file) { | ||
| if (strtolower($file->getExtension()) !== 'md') { | ||
| continue; | ||
| } | ||
|
|
||
| $definition = $this->definition($file); | ||
|
|
||
| if ($definition->handle !== null) { | ||
| $handle = strtolower($definition->handle); | ||
|
|
||
| if (isset($handles[$handle])) { | ||
| throw new InvalidArgumentException("Custom widget files [{$handles[$handle]}] and [{$definition->filename}] have the same handle."); | ||
| } | ||
|
|
||
| $handles[$handle] = $definition->filename; | ||
| } | ||
|
|
||
| $definitions->put($definition->id, $definition); | ||
| } | ||
|
|
||
| return $this->definitions = $definitions; | ||
| } | ||
|
|
||
| public function find(string $id): ?CustomWidgetDefinition | ||
| { | ||
| return $this->all()->get($id); | ||
| } | ||
|
|
||
| public function fromType(string $type): ?CustomWidgetDefinition | ||
| { | ||
| return $this->all()->first(fn (CustomWidgetDefinition $definition) => $definition->type() === $type); | ||
| } | ||
|
|
||
| private function definition(SplFileInfo $file): CustomWidgetDefinition | ||
| { | ||
| $filename = $file->getFilename(); | ||
| $contents = File::get($file->getPathname()); | ||
|
|
||
| $parsed = $this->frontMatterParser->parse($contents); | ||
| $metadata = (array) $parsed->getFrontMatter(); | ||
|
|
||
| $string = function (string $property) use ($filename, $metadata): ?string { | ||
| $value = $metadata[$property] ?? null; | ||
|
|
||
| if ($value === null || is_scalar($value)) { | ||
| return $value === null ? null : (string) $value; | ||
| } | ||
|
|
||
| throw new InvalidArgumentException("Custom widget file [$filename] frontmatter property [$property] must be a string or null."); | ||
| }; | ||
|
|
||
| $maxColspan = $metadata['maxColspan'] ?? null; | ||
|
|
||
| if ($maxColspan !== null) { | ||
| $maxColspan = filter_var($maxColspan, FILTER_VALIDATE_INT); | ||
|
|
||
| if ($maxColspan === false) { | ||
| throw new InvalidArgumentException("Custom widget file [$filename] frontmatter property [maxColspan] must be an integer between 1 and 4, or null."); | ||
| } | ||
| } | ||
|
|
||
| $showByDefault = filter_var($metadata['showByDefault'] ?? false, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE); | ||
|
|
||
| if ($showByDefault === null) { | ||
| throw new InvalidArgumentException("Custom widget file [$filename] frontmatter property [showByDefault] must be a boolean."); | ||
| } | ||
|
|
||
| return new CustomWidgetDefinition( | ||
| filename: $filename, | ||
| handle: $string('handle'), | ||
| label: $string('label'), | ||
| icon: $string('icon'), | ||
| maxColspan: $maxColspan, | ||
| title: $string('title'), | ||
| titleFromLabel: ! array_key_exists('title', $metadata), | ||
| subtitle: $string('subtitle'), | ||
| showByDefault: $showByDefault, | ||
| body: $parsed->getContent(), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace CraftCms\Cms\Dashboard\Data; | ||
|
|
||
| use CraftCms\Cms\Validation\Rules\HandleRule; | ||
| use InvalidArgumentException; | ||
|
|
||
| readonly class CustomWidgetDefinition | ||
| { | ||
| public string $id; | ||
|
|
||
| public function __construct( | ||
| public string $filename, | ||
| public ?string $handle, | ||
| public ?string $label, | ||
| public ?string $icon, | ||
| public ?int $maxColspan, | ||
| public ?string $title, | ||
| public bool $titleFromLabel, | ||
| public ?string $subtitle, | ||
| public bool $showByDefault, | ||
| public string $body, | ||
| ) { | ||
| if ($handle !== null) { | ||
| new HandleRule()->validate('handle', $handle, fn () => throw new InvalidArgumentException("Custom widget file [$filename] has an invalid handle.")); | ||
| } | ||
|
|
||
| if ($maxColspan !== null && ($maxColspan < 1 || $maxColspan > 4)) { | ||
| throw new InvalidArgumentException("Custom widget file [$filename] frontmatter property [maxColspan] must be an integer between 1 and 4, or null."); | ||
| } | ||
|
|
||
| $this->id = $handle ? "handle:$handle" : "path:$filename"; | ||
| } | ||
|
|
||
| public function type(): string | ||
| { | ||
| return self::class.':'.$this->id; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace CraftCms\Cms\Dashboard\Widgets; | ||
|
|
||
| use CraftCms\Cms\Dashboard\CustomWidgets; | ||
| use CraftCms\Cms\Dashboard\Data\CustomWidgetDefinition; | ||
| use CraftCms\Cms\Support\Facades\Markdown; | ||
| use CraftCms\Cms\Support\Str; | ||
| use CraftCms\Cms\Twig\Twig; | ||
| use Override; | ||
|
|
||
| class Custom extends Widget | ||
| { | ||
| public string $definitionId = ''; | ||
|
|
||
| public function __construct( | ||
| private readonly CustomWidgets $customWidgets, | ||
| private readonly Twig $twig, | ||
| array|object $config = [], | ||
| ) { | ||
| parent::__construct($config); | ||
| } | ||
|
|
||
| #[Override] | ||
| public function getType(): string | ||
| { | ||
| return $this->definition()?->type() ?? static::class; | ||
| } | ||
|
|
||
| #[Override] | ||
| public function getDisplayName(): string | ||
| { | ||
| $definition = $this->definition(); | ||
|
|
||
| if (! $definition) { | ||
| return static::displayName(); | ||
| } | ||
|
|
||
| $label = $definition->label === null ? '' : trim($this->render($definition->label, false)); | ||
|
|
||
| return $label !== '' ? $label : Str::headline(pathinfo($definition->filename, PATHINFO_FILENAME)); | ||
| } | ||
|
|
||
| #[Override] | ||
| public function getIcon(): ?string | ||
| { | ||
| return $this->definition()?->icon; | ||
| } | ||
|
|
||
| #[Override] | ||
| public function getMaxColspan(): ?int | ||
| { | ||
| return $this->definition()?->maxColspan; | ||
| } | ||
|
|
||
| #[Override] | ||
| public function getTitle(): ?string | ||
| { | ||
| $definition = $this->definition(); | ||
|
|
||
| if (! $definition) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($definition->titleFromLabel) { | ||
| return $this->getDisplayName(); | ||
| } | ||
|
|
||
| return $definition->title === null ? null : trim($this->render($definition->title, false)); | ||
| } | ||
|
|
||
| #[Override] | ||
| public function getSubtitle(): ?string | ||
| { | ||
| $subtitle = $this->definition()?->subtitle; | ||
|
|
||
| return $subtitle === null ? null : trim($this->render($subtitle, false)); | ||
| } | ||
|
|
||
| #[Override] | ||
| public function getBodyHtml(): ?string | ||
| { | ||
| $definition = $this->definition(); | ||
|
|
||
| if (! $definition) { | ||
| return null; | ||
| } | ||
|
|
||
| return Markdown::parse($this->render($definition->body, 'html')); | ||
| } | ||
|
|
||
| private function definition(): ?CustomWidgetDefinition | ||
| { | ||
| return $this->customWidgets->find($this->definitionId); | ||
| } | ||
|
|
||
| private function render(string $template, string|false $escaper): string | ||
| { | ||
| $twig = $this->twig->get(); | ||
| $twig->setDefaultEscaperStrategy($escaper); | ||
|
|
||
| try { | ||
| return $twig->createTemplate($template, $this->definition()?->filename)->render(); | ||
| } finally { | ||
| $twig->setDefaultEscaperStrategy(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.