From c1bd0ac99e99bab60d9f3469099c2cc1f36fcf1b Mon Sep 17 00:00:00 2001 From: Sebastian Lenz Date: Wed, 22 May 2024 09:55:26 +0200 Subject: [PATCH] Craft 5 compatibility --- composer.json | 6 +-- src/foreignField/ForeignField.php | 13 ++--- src/foreignField/ForeignFieldModel.php | 51 ++++--------------- .../ForeignFieldQueryExtension.php | 6 +-- src/foreignField/ForeignFieldRecord.php | 4 +- src/foreignField/traits/FormHelpers.php | 4 -- 6 files changed, 22 insertions(+), 62 deletions(-) diff --git a/composer.json b/composer.json index a1e6503..fcfbfa3 100644 --- a/composer.json +++ b/composer.json @@ -3,8 +3,8 @@ "description": "A collection of utility classes for Craft CMS development", "license": "MIT", "require": { - "php": "^8.0", - "craftcms/cms": "^4.0.0" + "php": "^8.2", + "craftcms/cms": "^5.1.3" }, "autoload": { "psr-4": { @@ -17,7 +17,7 @@ "craftcms/plugin-installer": true }, "platform": { - "php": "8.0.2" + "php": "8.2.0" } }, "require-dev": { diff --git a/src/foreignField/ForeignField.php b/src/foreignField/ForeignField.php index 2f9b0fc..e93bf0f 100644 --- a/src/foreignField/ForeignField.php +++ b/src/foreignField/ForeignField.php @@ -54,11 +54,11 @@ public function afterElementSave(ElementInterface $element, bool $isNew): void { */ public function behaviors(): array { $behaviors = parent::behaviors(); - + $behaviors['typecast'] = [ 'class' => AttributeTypecastBehavior::class, ]; - + return $behaviors; } @@ -387,13 +387,6 @@ protected function toRecordAttributes(ForeignFieldModel $model, ElementInterface // Static methods // -------------- - /** - * @inheritDoc - */ - public static function hasContentColumn(): bool { - return false; - } - /** * @return bool */ @@ -475,7 +468,7 @@ public static function t(string $message): string { } /** - * @inheritdoc + * @return string */ public static function valueType(): string { return static::modelClass(); diff --git a/src/foreignField/ForeignFieldModel.php b/src/foreignField/ForeignFieldModel.php index 4cd9369..6571e2c 100644 --- a/src/foreignField/ForeignFieldModel.php +++ b/src/foreignField/ForeignFieldModel.php @@ -5,11 +5,9 @@ use Craft; use craft\base\ElementInterface; use craft\base\Model; -use craft\elements\MatrixBlock; use Exception; use lenz\craft\utils\helpers\ArrayHelper; use lenz\craft\utils\helpers\ElementHelpers; -use yii\base\InvalidConfigException; /** * Class ForeignModel @@ -78,7 +76,7 @@ public function getOwner(): ElementInterface|null { /** * @return ElementInterface|null - * @throws InvalidConfigException + * @noinspection PhpUnused Public API */ public function getRoot(): ElementInterface|null { if ($this->_root === false) { @@ -164,46 +162,19 @@ protected function translate(string $message): string { /** * @param ElementInterface $element * @return ElementInterface - * @throws InvalidConfigException */ private static function toParentElement(ElementInterface $element): ElementInterface { - if ($element instanceof MatrixBlock) { - return self::toParentElement(self::toMatrixParentElement($element)); - } - - if (is_a($element, 'verbb\supertable\elements\SuperTableBlockElement')) { - /** @noinspection PhpPossiblePolymorphicInvocationInspection */ - return self::toParentElement($element->getOwner()); - } - - return $element; - } - - /** - * @param MatrixBlock $element - * @return ElementInterface - * @throws Exception - */ - private static function toMatrixParentElement(MatrixBlock $element): ElementInterface { - try { - $owner = $element->getOwner(); - if ($owner->id == $element->primaryOwnerId) { - return $owner; - } - } catch (\Throwable) { - // If the owner is trashed, we'll get an exception here. Ignore it and try fetching the element. - } - - $sites = [$element->siteId, '*']; - $elements = Craft::$app->getElements(); - - while (count($sites)) { - $owner = $elements->getElementById($element->primaryOwnerId, null, array_shift($sites), ['trashed' => null]); - if ($owner) { - return $owner; + $index = 0; + do { + $parent = $element->getParent(); + + if (is_null($parent)) { + return $element; + } else { + $element = $parent; } - } + } while (++$index < 50); - throw new Exception("Invalid parent id $element->primaryOwnerId for matrix block with id $element->id in site $element->siteId."); + return $element; } } diff --git a/src/foreignField/ForeignFieldQueryExtension.php b/src/foreignField/ForeignFieldQueryExtension.php index 32c5e12..a0b2d60 100644 --- a/src/foreignField/ForeignFieldQueryExtension.php +++ b/src/foreignField/ForeignFieldQueryExtension.php @@ -65,7 +65,7 @@ public final function __construct(array $config) { /** * @return void */ - public function onAfterPrepare() { + public function onAfterPrepare(): void { $enableEagerLoad = ( $this->enableEagerLoad && ( !empty($this->filters) || @@ -106,7 +106,7 @@ protected function applyOptions(array $options): static { /** * @return void */ - protected function attachEagerLoad() { + protected function attachEagerLoad(): void { $this->query->query->addSelect([ 'field:' . $this->field->handle => $this->getJsonExpression(), ]); @@ -115,7 +115,7 @@ protected function attachEagerLoad() { /** * @return void */ - protected function attachJoin() { + protected function attachJoin(): void { /** @var ActiveRecord $recordClass */ $recordClass = $this->field::recordClass(); $tableName = $recordClass::tableName(); diff --git a/src/foreignField/ForeignFieldRecord.php b/src/foreignField/ForeignFieldRecord.php index 63cd768..c5c2cc6 100644 --- a/src/foreignField/ForeignFieldRecord.php +++ b/src/foreignField/ForeignFieldRecord.php @@ -58,7 +58,7 @@ public function rules(): array { * @param Migration $migration * @param array $columns */ - public static function createTable(Migration $migration, array $columns) { + public static function createTable(Migration $migration, array $columns): void { $table = static::tableName(); if ($migration->db->tableExists($table)) { return; @@ -92,7 +92,7 @@ public static function createTable(Migration $migration, array $columns) { /** * @param Migration $migration */ - public static function dropTable(Migration $migration) { + public static function dropTable(Migration $migration): void { $table = static::tableName(); if ($migration->db->tableExists($table)) { $migration->dropTable($table); diff --git a/src/foreignField/traits/FormHelpers.php b/src/foreignField/traits/FormHelpers.php index 213f36f..d2466a5 100644 --- a/src/foreignField/traits/FormHelpers.php +++ b/src/foreignField/traits/FormHelpers.php @@ -4,10 +4,6 @@ namespace lenz\craft\utils\foreignField\traits; -use yii\base\Model; -use yii\validators\RequiredValidator; -use yii\validators\Validator; - /** * Trait FormHelpers * Helpers required by the form template provided by this module.