Skip to content

Commit

Permalink
Support element render
Browse files Browse the repository at this point in the history
Fixes #3742
  • Loading branch information
lukeholder committed Nov 1, 2024
1 parent 272475a commit 84f4980
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
16 changes: 5 additions & 11 deletions src/elements/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1655,18 +1655,12 @@ public function setAttributes($values, $safeOnly = true): void
*/
public function getFieldLayout(): ?FieldLayout
{
$fieldLayout = parent::getFieldLayout();
if ($fieldLayout) {
return $fieldLayout;
}

$fieldLayout = $this->getType()->getFieldLayout();
if ($fieldLayout->id) {
$this->fieldLayoutId = $fieldLayout->id;
return $fieldLayout;
try {
return $this->getType()->getProductFieldLayout();
} catch (InvalidConfigException) {
// The product type was probably deleted
return null;
}

return null;
}

/**
Expand Down
15 changes: 8 additions & 7 deletions src/elements/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,16 +420,17 @@ public function attributes(): array
* @throws InvalidConfigException
* @throws InvalidConfigException
*/
/**
* @inheritdoc
*/
public function getFieldLayout(): ?FieldLayout
{
$fieldLayout = parent::getFieldLayout();

if (!$fieldLayout && $this->getOwnerId()) {
$fieldLayout = $this->getOwner()->getType()->getVariantFieldLayout();
$this->fieldLayoutId = $fieldLayout->id;
try {
return $this->getOwner()->getType()->getVariantFieldLayout();
} catch (InvalidConfigException) {
// The product type was probably deleted
return null;
}

return $fieldLayout;
}

/**
Expand Down
20 changes: 16 additions & 4 deletions src/models/ProductType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Craft;
use craft\base\Field;
use craft\base\FieldLayoutProviderInterface;
use craft\behaviors\FieldLayoutBehavior;
use craft\commerce\base\Model;
use craft\commerce\elements\Product;
Expand All @@ -33,7 +34,6 @@
/**
* Product type model.
* @method null setFieldLayout(FieldLayout $fieldLayout)
* @method FieldLayout getFieldLayout()
*
* @property string $cpEditUrl
* @property string $cpEditVariantUrl
Expand All @@ -47,7 +47,7 @@
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 2.0
*/
class ProductType extends Model
class ProductType extends Model implements FieldLayoutProviderInterface
{
/** @since 5.2.0 */
public const DEFAULT_PLACEMENT_BEGINNING = 'beginning';
Expand Down Expand Up @@ -224,6 +224,14 @@ public function __toString()
return (string)$this->handle;
}

/**
* @inerhitdoc
*/
public function getHandle(): ?string
{
return $this->handle;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -388,6 +396,11 @@ public function setTaxCategories(array $taxCategories): void
$this->_taxCategories = $categories;
}

public function getFieldLayout(): FieldLayout
{
return $this->productFieldLayout();

Check failure on line 401 in src/models/ProductType.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Call to an undefined method craft\commerce\models\ProductType::productFieldLayout().
}

/**
* @throws InvalidConfigException
*/
Expand Down Expand Up @@ -517,9 +530,8 @@ public function getHasVariants(): bool
/**
* @inheritdoc
*/
public function behaviors(): array
protected function defineBehaviors(): array
{
$behaviors = parent::behaviors();
$behaviors['productFieldLayout'] = [
'class' => FieldLayoutBehavior::class,
'elementType' => Product::class,
Expand Down

0 comments on commit 84f4980

Please sign in to comment.