-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(SFT-686): fixing user photo upload
- Loading branch information
1 parent
d0dc9a6
commit f8141a8
Showing
3 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/plugin/src/Integrations/Elements/User/EventListeners/PhotoTransform.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Solspace\Freeform\Integrations\Elements\User\EventListeners; | ||
|
||
use Solspace\Freeform\Events\Integrations\ElementIntegrations\ProcessValueEvent; | ||
use Solspace\Freeform\Fields\Interfaces\FileUploadInterface; | ||
use Solspace\Freeform\Integrations\Elements\User\User; | ||
use Solspace\Freeform\Library\Bundles\FeatureBundle; | ||
use Solspace\Freeform\Library\Integrations\Types\Elements\ElementIntegrationInterface; | ||
use yii\base\Event; | ||
|
||
class PhotoTransform extends FeatureBundle | ||
{ | ||
public function __construct() | ||
{ | ||
Event::on( | ||
ElementIntegrationInterface::class, | ||
ElementIntegrationInterface::EVENT_PROCESS_VALUE, | ||
[$this, 'processPhoto'] | ||
); | ||
} | ||
|
||
public function processPhoto(ProcessValueEvent $event): void | ||
{ | ||
if (!$event->getIntegration() instanceof User) { | ||
return; | ||
} | ||
|
||
if ('photo' !== $event->getHandle()) { | ||
return; | ||
} | ||
|
||
$value = $event->getValue(); | ||
$field = $event->getFreeformField(); | ||
|
||
if (empty($value)) { | ||
$event->setValue(null); | ||
|
||
return; | ||
} | ||
|
||
if ($field instanceof FileUploadInterface) { | ||
$asset = $field->getAssets()->one(); | ||
if ($asset) { | ||
$event->setValue($asset); | ||
} | ||
|
||
return; | ||
} | ||
|
||
$assetId = reset($value); | ||
if ($assetId && is_numeric($assetId)) { | ||
$asset = \Craft::$app->getAssets()->getAssetById((int) $assetId); | ||
if ($asset) { | ||
$event->setValue($asset); | ||
} | ||
} | ||
} | ||
} |