Skip to content

Commit b25f502

Browse files
dabrtadriendupuismnocon
authored
IBX-9594: Describe creating a custom DAM connector (#2642)
* IBX-9594: Describe creating a custom DAM connector --------- Co-authored-by: dabrt <[email protected]> Co-authored-by: Adrien Dupuis <[email protected]> Co-authored-by: Marek Nocoń <[email protected]>
1 parent 28b2711 commit b25f502

File tree

13 files changed

+297
-7
lines changed

13 files changed

+297
-7
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
parameters:
2+
ibexa.site_access.config.<scope>.image_asset_view_defaults:
3+
full:
4+
commons:
5+
template: '@@ibexadesign/commons_asset_view.html.twig'
6+
match:
7+
SourceBasedViewMatcher: commons
8+
default:
9+
template: '@@ibexadesign/ui/field_type/image_asset_view.html.twig'
10+
match: []

code_samples/back_office/images/config/services.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,28 @@ services:
66
- '@ibexa.api.service.content'
77
- '@ibexa.field_type.ezbinaryfile.io_service'
88
- '@Ibexa\Core\Helper\TranslationHelper'
9+
10+
App\Connector\Dam\Handler\WikimediaCommonsHandler:
11+
tags:
12+
- { name: 'ibexa.platform.connector.dam.handler', source: 'commons' }
13+
14+
App\Connector\Dam\Transformation\WikimediaCommonsTransformationFactory:
15+
tags:
16+
- { name: 'ibexa.platform.connector.dam.transformation_factory', source: 'commons' }
17+
18+
commons_asset_variation_generator:
19+
class: Ibexa\Connector\Dam\Variation\URLBasedVariationGenerator
20+
tags:
21+
- { name: 'ibexa.platform.connector.dam.variation_generator', source: 'commons' }
22+
23+
commons_search_tab:
24+
class: Ibexa\Connector\Dam\View\Search\Tab\GenericSearchTab
25+
public: false
26+
arguments:
27+
$identifier: 'commons'
28+
$source: 'commons'
29+
$name: 'Wikimedia Commons'
30+
$searchFormType: 'Ibexa\Connector\Dam\Form\Search\GenericSearchType'
31+
$formFactory: '@form.factory'
32+
tags:
33+
- { name: 'ibexa.admin_ui.tab', group: 'connector-dam-search' }
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace App\Connector\Dam\Handler;
4+
5+
use Ibexa\Contracts\Connector\Dam\Asset;
6+
use Ibexa\Contracts\Connector\Dam\AssetCollection;
7+
use Ibexa\Contracts\Connector\Dam\AssetIdentifier;
8+
use Ibexa\Contracts\Connector\Dam\AssetMetadata;
9+
use Ibexa\Contracts\Connector\Dam\AssetSource;
10+
use Ibexa\Contracts\Connector\Dam\AssetUri;
11+
use Ibexa\Contracts\Connector\Dam\Handler\Handler as HandlerInterface;
12+
use Ibexa\Contracts\Connector\Dam\Search\AssetSearchResult;
13+
use Ibexa\Contracts\Connector\Dam\Search\Query;
14+
15+
class WikimediaCommonsHandler implements HandlerInterface
16+
{
17+
public function search(Query $query, int $offset = 0, int $limit = 20): AssetSearchResult
18+
{
19+
$searchUrl = 'https://commons.wikimedia.org/w/api.php?action=query&list=search&format=json&srnamespace=6'
20+
. '&srsearch=' . urlencode($query->getPhrase())
21+
. '&sroffset=' . $offset
22+
. '&srlimit=' . $limit
23+
;
24+
25+
$jsonResponse = file_get_contents($searchUrl);
26+
if ($jsonResponse === false) {
27+
return new AssetSearchResult(0, new AssetCollection([]));
28+
}
29+
30+
$response = json_decode($jsonResponse, true);
31+
if (!isset($response['query']['search'])) {
32+
return new AssetSearchResult(0, new AssetCollection([]));
33+
}
34+
35+
$assets = [];
36+
foreach ($response['query']['search'] as $result) {
37+
$identifier = str_replace('File:', '', $result['title']);
38+
$assets[] = $this->fetchAsset($identifier);
39+
}
40+
41+
return new AssetSearchResult(
42+
(int) ($response['query']['searchinfo']['totalhits'] ?? 0),
43+
new AssetCollection($assets)
44+
);
45+
}
46+
47+
public function fetchAsset(string $id): Asset
48+
{
49+
$metadataUrl = 'https://commons.wikimedia.org/w/api.php?action=query&prop=imageinfo&iiprop=extmetadata&format=json'
50+
. '&titles=File%3a' . urlencode($id)
51+
;
52+
53+
$jsonResponse = file_get_contents($metadataUrl);
54+
if ($jsonResponse === false) {
55+
throw new \RuntimeException('Couldn\'t retrieve asset metadata');
56+
}
57+
58+
$response = json_decode($jsonResponse, true);
59+
if (!isset($response['query']['pages'])) {
60+
throw new \RuntimeException('Couldn\'t parse asset metadata');
61+
}
62+
63+
$pageData = array_values($response['query']['pages'])[0] ?? null;
64+
if (!isset($pageData['imageinfo'][0]['extmetadata'])) {
65+
throw new \RuntimeException('Couldn\'t parse image asset metadata');
66+
}
67+
68+
$imageInfo = $pageData['imageinfo'][0]['extmetadata'];
69+
70+
return new Asset(
71+
new AssetIdentifier($id),
72+
new AssetSource('commons'),
73+
new AssetUri('https://commons.wikimedia.org/w/index.php?title=Special:Redirect/file/' . urlencode($id)),
74+
new AssetMetadata([
75+
'page_url' => "https://commons.wikimedia.org/wiki/File:$id",
76+
'author' => $imageInfo['Artist']['value'] ?? null,
77+
'license' => $imageInfo['LicenseShortName']['value'] ?? null,
78+
'license_url' => $imageInfo['LicenseUrl']['value'] ?? null,
79+
])
80+
);
81+
}
82+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace App\Connector\Dam\Transformation;
4+
5+
use Ibexa\Contracts\Connector\Dam\Variation\Transformation;
6+
use Ibexa\Contracts\Connector\Dam\Variation\TransformationFactory as TransformationFactoryInterface;
7+
8+
class WikimediaCommonsTransformationFactory implements TransformationFactoryInterface
9+
{
10+
/** @param array<string, scalar> $transformationParameters */
11+
public function build(?string $transformationName = null, array $transformationParameters = []): Transformation
12+
{
13+
if (null === $transformationName) {
14+
return new Transformation(null, array_map('strval', $transformationParameters));
15+
}
16+
17+
$transformations = $this->buildAll();
18+
19+
if (array_key_exists($transformationName, $transformations)) {
20+
return $transformations[$transformationName];
21+
}
22+
23+
throw new \InvalidArgumentException(sprintf('Unknown transformation "%s".', $transformationName));
24+
}
25+
26+
public function buildAll(): array
27+
{
28+
return [
29+
'reference' => new Transformation('reference', []),
30+
'tiny' => new Transformation('tiny', ['width' => '30']),
31+
'small' => new Transformation('small', ['width' => '100']),
32+
'medium' => new Transformation('medium', ['width' => '200']),
33+
'large' => new Transformation('large', ['width' => '300']),
34+
];
35+
}
36+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% extends '@ibexadesign/ui/field_type/image_asset_view.html.twig' %}
2+
3+
{% block asset_preview %}
4+
{{ parent() }}
5+
<div>
6+
<a href="{{ asset.assetMetadata.page_url }}">Image</a>
7+
{% if asset.assetMetadata.author %} by {{ asset.assetMetadata.author }}{% endif %}
8+
{% if asset.assetMetadata.license and asset.assetMetadata.license_url %}
9+
under <a href="{{ asset.assetMetadata.license_url }}">{{ asset.assetMetadata.license }}</a>
10+
{% endif %}.
11+
</div>
12+
{% endblock %}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ezimageasset.dam_asset.page_url: Image page
2+
ezimageasset.dam_asset.author: Image author
3+
ezimageasset.dam_asset.license: License
4+
ezimageasset.dam_asset.license_url: License page

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
"ibexa/solr": "5.0.x-dev",
6767
"ibexa/version-comparison": "5.0.x-dev",
6868
"league/oauth2-google": "^4.0",
69-
"ibexa/core-search": "~5.0.x-dev"
69+
"ibexa/core-search": "~5.0.x-dev",
70+
"ibexa/connector-dam": "~5.0.x-dev"
7071
},
7172
"scripts": {
7273
"fix-cs": "php-cs-fixer fix --config=.php-cs-fixer.php -v --show-progress=dots",

docs/administration/back_office/add_user_setting.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ You can order the settings in the **User** menu by setting their `priority`.
2727
`group` indicates the group that the setting is placed in.
2828
It can be one of the built-in groups, or a custom one.
2929

30-
To create a custom setting group, create an `App\Setting\Group\MyGroup.php` file:
30+
To create a custom setting group, create an `App/Setting/Group/MyGroup.php` file:
3131

3232
``` php
3333
[[= include_file('code_samples/back_office/settings/src/Setting/Group/MyGroup.php') =]]

docs/administration/back_office/back_office_elements/formatting_date_and_time.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ You can format date and time by using the following services:
1717
- `@ibexa.user.settings.full_date_format.formatter`
1818
- `@ibexa.user.settings.full_time_format.formatter`
1919

20-
To use them, create an `src\Service\MyService.php` file containing:
20+
To use them, create an `src/Service/MyService.php` file containing:
2121

2222
``` php
2323
<?php

docs/commerce/storefront/extend_storefront.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition: commerce
77

88
## Built-in menus
99

10-
With the `ibexa\storefront` package come the following built-in menus:
10+
With the `ibexa/storefront` package come the following built-in menus:
1111

1212
| Item | Value | Description |
1313
|------------|----------|---------|

0 commit comments

Comments
 (0)