From 74b1f30cf702ed48f25f7758a7f266de47bd6e0d Mon Sep 17 00:00:00 2001 From: Robin Date: Mon, 4 Oct 2021 09:21:59 +0200 Subject: [PATCH] pass the element and the field into the callback --- src/services/SeederService.php | 17 +++++++++++++---- src/services/fields/Fields.php | 10 +++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/services/SeederService.php b/src/services/SeederService.php index b713c5e..25ec4bd 100644 --- a/src/services/SeederService.php +++ b/src/services/SeederService.php @@ -55,13 +55,12 @@ class SeederService extends Component */ public function populateFields($fields, ElementInterface $element): ElementInterface { - $entryFields = []; foreach ($fields as $field) { try { $fieldData = $this->getFieldData($field, $element); if ($fieldData) { - $entryFields[$field['handle']] = $fieldData;//Seeder::$plugin->$fieldProvider->$fieldType($field, $entry); - } + $element->setFieldValue($field->handle, $fieldData); + } } catch (FieldNotFoundException $e) { if (Seeder::$plugin->getSettings()->debug) { Craft::dd($e); @@ -70,7 +69,6 @@ public function populateFields($fields, ElementInterface $element): ElementInter } } } - $element->setFieldValues($entryFields); return $element; } @@ -161,6 +159,17 @@ public function getFieldData($field, ElementInterface $element) return call_user_func($registeredFieldTypes[$class], $field, $element); } + // last chance, try to find a valid callback + foreach ($registeredFieldTypes as $fieldType){ + + if(is_string($fieldType) && $class === $fieldType){ + $v = Seeder::$plugin->fields->checkForEvent($field, $element); + if($v){ + return $v; + } + } + } + throw new FieldNotFoundException('the field ' . $class . ' could not be found'); } } diff --git a/src/services/fields/Fields.php b/src/services/fields/Fields.php index 27fc4dc..2f43631 100644 --- a/src/services/fields/Fields.php +++ b/src/services/fields/Fields.php @@ -14,6 +14,7 @@ use craft\base\Component; use craft\base\ElementInterface; use craft\base\Field; +use craft\base\FieldInterface; use craft\elements\Asset; use craft\elements\Category; use craft\elements\Entry; @@ -121,7 +122,6 @@ public function getSettings(Field $field, ElementInterface $element) /** * getCallBack * - * @param $settings * @param null $class * * @return mixed|null @@ -129,12 +129,12 @@ public function getSettings(Field $field, ElementInterface $element) * @author Robin Schambach * @since 06.09.2019 */ - public function getCallBack($settings, $class = null) + public function getCallBack($settings, FieldInterface $field, ElementInterface $element, $class = null) { // just a string, no options, no class if(is_string($settings)){ $class = $class ?? $this->factory; - return call_user_func([$class, $settings]); + return $class->$settings($field, $element); } if(is_array($settings) === true){ @@ -157,7 +157,7 @@ public function getCallBack($settings, $class = null) if(count($settings) === 2 && is_object($settings[0])){ // return call_user_func($settings); // PHPstorm says this... need trying ¯\_(ツ)_/¯ - return $settings(); + return $settings($field, $element); } } @@ -203,7 +203,7 @@ public function checkForEvent(Field $field, ElementInterface $element) return $settings($field, $element); } - $value = $this->getCallBack($settings); + $value = $this->getCallBack($settings, $field, $element); if($value !== 'no-value'){ return $value;