From 833956af79693dace908642ab2c6ed674aa46f25 Mon Sep 17 00:00:00 2001 From: olivierbon Date: Thu, 19 Sep 2024 17:02:35 +0200 Subject: [PATCH] Adds Link field support --- src/Plugin.php | 19 +++++++++++++++++++ src/linktypes/Product.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/linktypes/Product.php diff --git a/src/Plugin.php b/src/Plugin.php index d33133c..8f24df1 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -18,6 +18,7 @@ use craft\events\DefineConsoleActionsEvent; use craft\events\RegisterComponentTypesEvent; use craft\events\RegisterUrlRulesEvent; +use craft\fields\Link; use craft\helpers\UrlHelper; use craft\services\Elements; use craft\services\Fields; @@ -25,6 +26,7 @@ use craft\shopify\elements\Product; use craft\shopify\fields\Products as ProductsField; use craft\shopify\handlers\Product as ProductHandler; +use craft\shopify\linktypes\Product as ProductLinkType; use craft\shopify\models\Settings; use craft\shopify\services\Api; use craft\shopify\services\Products; @@ -113,6 +115,7 @@ public function init() $this->_registerElementTypes(); $this->_registerUtilityTypes(); $this->_registerFieldTypes(); + $this->_registerLinkTypes(); $this->_registerVariables(); $this->_registerResaveCommands(); @@ -214,6 +217,22 @@ private function _registerFieldTypes(): void }); } + /** + * Register Link types + * + * @since 5.2.0 + */ + private function _registerLinkTypes(): void + { + if (!class_exists(Link::class)) { + return; + } + + Event::on(Link::class, Link::EVENT_REGISTER_LINK_TYPES, function(RegisterComponentTypesEvent $event) { + $event->types[] = ProductLinkType::class; + }); + } + /** * Register Shopify twig variables to the main craft variable * diff --git a/src/linktypes/Product.php b/src/linktypes/Product.php new file mode 100644 index 0000000..a8825c3 --- /dev/null +++ b/src/linktypes/Product.php @@ -0,0 +1,32 @@ + + * @since 5.2.0 + */ +class Product extends BaseElementLinkType +{ + protected static function elementType(): string + { + return ProductElement::class; + } + + public static function displayName(): string + { + return ProductElement::lowerDisplayName(); + } + +}