Skip to content

Commit

Permalink
pass the element and the field into the callback
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinSecondred committed Oct 4, 2021
1 parent 9681303 commit 74b1f30
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
17 changes: 13 additions & 4 deletions src/services/SeederService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -70,7 +69,6 @@ public function populateFields($fields, ElementInterface $element): ElementInter
}
}
}
$element->setFieldValues($entryFields);

return $element;
}
Expand Down Expand Up @@ -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');
}
}
10 changes: 5 additions & 5 deletions src/services/fields/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -121,20 +122,19 @@ public function getSettings(Field $field, ElementInterface $element)
/**
* getCallBack
*
* @param $settings
* @param null $class
*
* @return mixed|null
*
* @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){
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 74b1f30

Please sign in to comment.