Skip to content

Commit

Permalink
Craft 5 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-lenz committed May 22, 2024
1 parent 3abd09b commit c1bd0ac
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 62 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -17,7 +17,7 @@
"craftcms/plugin-installer": true
},
"platform": {
"php": "8.0.2"
"php": "8.2.0"
}
},
"require-dev": {
Expand Down
13 changes: 3 additions & 10 deletions src/foreignField/ForeignField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -387,13 +387,6 @@ protected function toRecordAttributes(ForeignFieldModel $model, ElementInterface
// Static methods
// --------------

/**
* @inheritDoc
*/
public static function hasContentColumn(): bool {
return false;
}

/**
* @return bool
*/
Expand Down Expand Up @@ -475,7 +468,7 @@ public static function t(string $message): string {
}

/**
* @inheritdoc
* @return string
*/
public static function valueType(): string {
return static::modelClass();
Expand Down
51 changes: 11 additions & 40 deletions src/foreignField/ForeignFieldModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
6 changes: 3 additions & 3 deletions src/foreignField/ForeignFieldQueryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down Expand Up @@ -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(),
]);
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/foreignField/ForeignFieldRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions src/foreignField/traits/FormHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c1bd0ac

Please sign in to comment.