Skip to content

Commit 9481b2b

Browse files
authored
Merge pull request #6 from AmazeeLabs/entity-type-agnostic
refactor: make the frontify field widget entity type agnostic
2 parents 4748a3c + c9537a4 commit 9481b2b

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed
File renamed without changes.

frontify.libraries.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ frontify_media_library:
1717
- core/drupal
1818
- core/drupalSettings
1919
- frontify/frontify_finder
20-
frontify_media_form:
20+
frontify_entity_form:
2121
version: 1.x
2222
js:
23-
js/frontify_media_form.js: { }
23+
js/frontify_entity_form.js: { }
2424
css:
2525
component:
26-
css/frontify_media_form.css: { }
26+
css/frontify_entity_form.css: { }
2727
dependencies:
2828
- core/drupal
2929
- frontify/frontify_finder

js/frontify_media_form.js renamed to js/frontify_entity_form.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
(function () {
77
Drupal.frontifyMediaForm = {
88
handleFinder(el) {
9-
console.log('Frontify media form');
109
el.addEventListener('click', async element => {
1110
element.currentTarget.disabled = true;
1211
// Handle unlimited fields.
@@ -31,9 +30,9 @@
3130
domain: drupalSettings.Frontify.api_url,
3231
options: {
3332
permanentDownloadUrls: true,
34-
// @todo configure extensions based on the media bundle.
35-
// this should be done as a third party setting of the Media provider
36-
// and selected based on its type.
33+
// @todo configure extensions based on the field, so no need
34+
// to bind this to a specific media entity, and the field widget
35+
// can be used independently.
3736
filters: [
3837
{
3938
key: 'ext',
@@ -76,8 +75,10 @@
7675
$fieldItem.querySelector('textarea.frontify-asset-metadata').value =
7776
JSON.stringify(assets[0]);
7877

79-
// Media name
80-
document.querySelector('#edit-name-wrapper input').value = assets[0].title;
78+
// Media specifics: set the Media name from the Frontify one.
79+
if (drupalSettings.Frontify.parent_entity_type_id === 'media') {
80+
document.querySelector('#edit-name-wrapper input').value = assets[0].title;
81+
}
8182

8283
$field
8384
.querySelector('.frontify-wrapper-finder-overlay')

js/frontify_once.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
case 'media_library':
1414
Drupal.frontifyMediaLibrary.handleFinder(el);
1515
break;
16-
case 'media_form':
16+
case 'entity_form':
1717
Drupal.frontifyMediaForm.handleFinder(el);
1818
break;
1919
default:

src/Plugin/Field/FieldWidget/FrontifyAssetFieldWidget.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\frontify\Plugin\Field\FieldWidget;
44

5+
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
56
use Drupal\Core\Field\Attribute\FieldWidget;
67
use Drupal\Core\Field\FieldItemListInterface;
78
use Drupal\Core\Form\FormStateInterface;
@@ -92,6 +93,10 @@ public function formElement(
9293
],
9394
];
9495

96+
97+
$parent = $items->getParent();
98+
$entityTypeId = $parent instanceof EntityAdapter ? $parent->getEntity()->getEntityTypeId() : '';
99+
95100
$element['open'] = [
96101
'#type' => 'button',
97102
'#value' => $this->t('Open Frontify'),
@@ -102,14 +107,15 @@ public function formElement(
102107
'#attached' => [
103108
'library' => [
104109
'frontify/frontify_once',
105-
'frontify/frontify_media_form',
110+
'frontify/frontify_entity_form',
106111
],
107112
'drupalSettings' => [
108113
'Frontify' => [
109-
'context' => 'media_form',
114+
'context' => 'entity_form',
110115
'api_url' => $config->get('frontify_api_url'),
111116
'debug_mode' => $config->get('debug_mode'),
112117
'preview_image_width' => self::PREVIEW_IMAGE_WIDTH,
118+
'parent_entity_type_id' => $entityTypeId,
113119
],
114120
],
115121
],

0 commit comments

Comments
 (0)