From 27cc399ac59f3f08228a811bd812fc82528a0041 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Wed, 15 Mar 2023 15:26:29 -0600 Subject: [PATCH] Initial work on supporting the Winter.Blog plugin Thoughts for further development: We should add a Winter\SEO\Behaviours\SeoableModel model behavior that makes it easier to configure the meta data (meta, link, & structured data) that a model provides by providing configuration through model properties and standard getSeoDataAttribute() / setSeoDataAttribute() methods that can be overridden or just configured by the model properties. --- Plugin.php | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/Plugin.php b/Plugin.php index 80c6c61..5b495e3 100644 --- a/Plugin.php +++ b/Plugin.php @@ -89,6 +89,7 @@ public function pluginDetails(): array public function boot(): void { $this->extendPagesForms(); + $this->extendWinterBlogPlugin(); $this->populateGlobalTags(); } @@ -178,7 +179,53 @@ protected function populateGlobalTags() } } }); + } + + /** + * Extends the Winter.Blog plugin with support for SEO form fields + * @TODO: Replace with a more generic approach that can be enabled / disabled / configured via config file + */ + protected function extendWinterBlogPlugin(): void + { + $controllerModels = [ + \Winter\Blog\Controllers\Posts::class => [ + \Winter\Blog\Models\Post::class, + ], + \Winter\Blog\Controllers\Categories::class => [ + \Winter\Blog\Models\Category::class, + ], + ]; + Event::listen('backend.form.extendFieldsBefore', function (\Backend\Widgets\Form $widget) use ($controllerModels) { + if ($widget->isNested) { + return; + } + + $controller = $widget->getController(); + $model = $widget->model; + + if ( + !isset($controllerModels[get_class($controller)]) + || !in_array(get_class($model), $controllerModels[get_class($controller)]) + ) { + return; + } + + $prefix = 'metadata[seo]['; + + $form = Yaml::parseFile(plugins_path('winter/seo/models/meta/fields.yaml')); + $tab = 'winter.seo::lang.models.meta.label'; + $halcyonFields = []; + foreach ($form['fields'] as $name => $config) { + $config['tab'] = $tab; + $halcyonFields["{$prefix}{$name}]"] = $config; + } + + $widget->secondaryTabs['paneCssClass'][$tab] = 'padded-pane'; + $widget->secondaryTabs['icons'][$tab] = 'icon-magnifying-glass'; + + $widget->secondaryTabs['fields'] = array_merge($widget->secondaryTabs['fields'], $halcyonFields); + }); } /**