-
Notifications
You must be signed in to change notification settings - Fork 81
IBX-9594: Describe creating a custom DAM connector #2642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 14 commits
4836c64
36db157
6e0c245
d43bc7c
320fdc1
a503470
5e16efc
9c4d7eb
57c2af9
af1c0ae
363a917
75a3bba
377e1f0
9382168
24faff1
d4f1423
c4b841e
ce26c9b
0ccda82
f869fd8
2c1ad54
3c963a0
eb2e201
4bc5ab6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| parameters: | ||
| ibexa.<site_access>.image_asset_view_defaults: | ||
| full: | ||
| commons: | ||
| template: '@@ibexadesign/commons_asset_view.html.twig' | ||
| match: | ||
| SourceBasedViewMatcher: commons | ||
| default: | ||
| template: '@@ibexadesign/ui/field_type/image_asset_view.html.twig' | ||
| match: [] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace App\Connector\Dam\Handler; | ||
|
|
||
| use Ibexa\Contracts\Connector\Dam\Asset; | ||
| use Ibexa\Contracts\Connector\Dam\AssetCollection; | ||
| use Ibexa\Contracts\Connector\Dam\AssetIdentifier; | ||
| use Ibexa\Contracts\Connector\Dam\AssetMetadata; | ||
| use Ibexa\Contracts\Connector\Dam\AssetSource; | ||
| use Ibexa\Contracts\Connector\Dam\AssetUri; | ||
| use Ibexa\Contracts\Connector\Dam\Handler\Handler as HandlerInterface; | ||
| use Ibexa\Contracts\Connector\Dam\Search\AssetSearchResult; | ||
| use Ibexa\Contracts\Connector\Dam\Search\Query; | ||
|
|
||
| class WikimediaCommonsHandler implements HandlerInterface | ||
| { | ||
| public function search(Query $query, int $offset = 0, int $limit = 20): AssetSearchResult | ||
| { | ||
| $searchUrl = 'https://commons.wikimedia.org/w/api.php?action=query&list=search&format=json&srnamespace=6' | ||
| . '&srsearch=' . urlencode($query->getPhrase()) | ||
| . '&sroffset=' . $offset | ||
| . '&srlimit=' . $limit | ||
| ; | ||
| $response = json_decode(file_get_contents($searchUrl), true); | ||
|
|
||
| $assets = []; | ||
| foreach ($response['query']['search'] as $result) { | ||
| $identifier = str_replace('File:', '', $result['title']); | ||
| $assets[] = $this->fetchAsset($identifier); | ||
| } | ||
|
|
||
| return new AssetSearchResult((int) $response['query']['searchinfo']['totalhits'], new AssetCollection($assets)); | ||
| } | ||
|
|
||
| public function fetchAsset(string $id): Asset | ||
| { | ||
| $metadataUrl = 'https://commons.wikipedia.org/w/api.php?action=query&prop=imageinfo&iiprop=extmetadata&format=json' | ||
| . '&titles=File%3a' . urlencode($id) | ||
| ; | ||
| $response = json_decode(file_get_contents($metadataUrl), true); | ||
| $imageInfo = array_values($response['query']['pages'])[0]['imageinfo'][0]['extmetadata']; | ||
|
|
||
| return new Asset( | ||
| new AssetIdentifier($id), | ||
| new AssetSource('commons'), | ||
| new AssetUri('https://commons.wikimedia.org/w/index.php?title=Special:Redirect/file/' . urlencode($id)), | ||
| new AssetMetadata([ | ||
| 'page_url' => "https://commons.wikimedia.org/wiki/File:$id", | ||
| 'author' => array_key_exists('Artist', $imageInfo) ? $imageInfo['Artist']['value'] : null, | ||
| 'license' => array_key_exists('LicenseShortName', $imageInfo) ? $imageInfo['LicenseShortName']['value'] : null, | ||
| 'license_url' => array_key_exists('LicenseUrl', $imageInfo) ? $imageInfo['LicenseUrl']['value'] : null, | ||
| ]) | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace App\Connector\Dam\Transformation; | ||
|
|
||
| use Ibexa\Contracts\Connector\Dam\Variation\Transformation; | ||
| use Ibexa\Contracts\Connector\Dam\Variation\TransformationFactory as TransformationFactoryInterface; | ||
|
|
||
| class WikimediaCommonsTransformationFactory implements TransformationFactoryInterface | ||
| { | ||
| public function build(?string $transformationName = null, array $transformationParameters = []): Transformation | ||
| { | ||
| return $this->buildAll()[$transformationName]; | ||
|
Check failure on line 12 in code_samples/back_office/images/src/Connector/Dam/Transformation/WikimediaCommonsTransformationFactory.php
|
||
| } | ||
|
|
||
| public function buildAll(): iterable | ||
| { | ||
| return [ | ||
| 'reference' => new Transformation('reference'), | ||
| 'tiny' => new Transformation('tiny', ['width' => 30]), | ||
|
Check failure on line 19 in code_samples/back_office/images/src/Connector/Dam/Transformation/WikimediaCommonsTransformationFactory.php
|
||
| 'small' => new Transformation('small', ['width' => 100]), | ||
|
Check failure on line 20 in code_samples/back_office/images/src/Connector/Dam/Transformation/WikimediaCommonsTransformationFactory.php
|
||
| 'medium' => new Transformation('medium', ['width' => 200]), | ||
|
Check failure on line 21 in code_samples/back_office/images/src/Connector/Dam/Transformation/WikimediaCommonsTransformationFactory.php
|
||
| 'large' => new Transformation('large', ['width' => 300]), | ||
|
Check failure on line 22 in code_samples/back_office/images/src/Connector/Dam/Transformation/WikimediaCommonsTransformationFactory.php
|
||
| ]; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| {% extends '@ibexadesign/ui/field_type/image_asset_view.html.twig' %} | ||
|
|
||
| {% block asset_preview %} | ||
| {{ parent() }} | ||
| <div> | ||
| <a href="{{ asset.assetMetadata.page_url }}">Image</a> by {{ asset.assetMetadata.author }} under | ||
| <a href="{{ asset.assetMetadata.license_url }}">{{ asset.assetMetadata.license }}</a>. </div> | ||
| {% endblock %} | ||
Uh oh!
There was an error while loading. Please reload this page.