diff --git a/modules/ephoto_dam_field/ephoto_dam_field.module b/modules/ephoto_dam_field/ephoto_dam_field.module index f51ec824b97851347565b9219ff5102f925960f2..4239e13bd54678537300b726e99bf4e5a4b6b03c 100644 --- a/modules/ephoto_dam_field/ephoto_dam_field.module +++ b/modules/ephoto_dam_field/ephoto_dam_field.module @@ -5,9 +5,30 @@ * Module ephoto_dam_field. */ + use Drupal\Component\Serialization\Json; + use \Drupal\Core\Render\Markup; + /** * Hook : implements hook_field_widget_form_alter(). */ function ephoto_dam_field_field_widget_form_alter(&$element, &$form_state, $context) { - $element['#attached']['library'][] = 'ephoto_dam_field/edit'; + $field_definition = $context['items']->getFieldDefinition(); + + if ($field_definition->getType() === 'ephoto_dam_field') { + $settings = $field_definition->getSettings(); + $settings_encoded = str_replace('"', "'", Json::encode($settings)); + + $element['#attached']['library'][] = 'ephoto_dam_field/edit'; + + // I do not like it + // It would be nice to put the settings AFTER the ".libraries", which would make it possible to call directly EphotoDamField.load($settings_encoded) + $element['#attached']['html_head'][] = [ + [ + '#tag' => 'script', + '#value' => Markup::create('EphotoDamFieldSettings=' . $settings_encoded . ';'), + '#weight' => 200 + ], + 'ephoto_dam_field_js_settings' + ]; + } } diff --git a/modules/ephoto_dam_field/js/ephoto_dam_field.js b/modules/ephoto_dam_field/js/ephoto_dam_field.js index d6513af10bcb07f142f6abf0fa34d7ce01ae487c..ec9d6ce3827622422adcabcae79b40399773a844 100644 --- a/modules/ephoto_dam_field/js/ephoto_dam_field.js +++ b/modules/ephoto_dam_field/js/ephoto_dam_field.js @@ -7,26 +7,29 @@ (function ($) { EphotoDamField = { - /** - * EPHOTO ID of Drupal Addon - */ + /** + * EPHOTO ID of Drupal Addon + */ CLIENT_ID : 'CaCvqS5H', - /** - * Settings - */ + /** + * Settings + */ settings: null, - /** - * Ephoto Dam instance - */ + /** + * Ephoto Dam instance + */ ephoto: null, - /** - * Ephoto Dam Server connected - */ - connected: false, + /** + * Ephoto Dam Server connected + */ + connected: false, + /** + * Enable version support + */ enableVersionSupport: function(el) { if(el.checked) { $('.ephoto-dam-field-version-field').removeClass('ephoto-dam-field-hidden'); @@ -43,49 +46,76 @@ } }, - select: function(settings) { - this.settings = settings; + /** + * Load Ephoto Dam Api + */ + load: function() { + this.settings = EphotoDamFieldSettings; $.getScript(settings.url + 'api/apiJS.js', this.initApi.bind(this)); }, + /** + * Init Ephoto Dam Api + */ initApi: function() { - var self = this; + var self = this; - if(!ePhoto) { - console.log(Drupal.t('Error loading the Ephoto Dam API')); - return; - } + if (!ePhoto) { + console.log(Drupal.t('Error loading the Ephoto Dam API')); + return; + } - console.log('loaded!'); - console.log(this.settings.url); + console.log('loaded!'); + console.log(this.settings.url); - this.ephoto = new ePhoto({ - 'server': this.settings.url, - //'authID': CKEDITOR.tools.getCookie('drupal_ephoto_dam_authid'), - 'client': this.CLIENT_ID - }); + this.ephoto = new ePhoto({ + 'server': this.settings.url, + //'authID': CKEDITOR.tools.getCookie('drupal_ephoto_dam_authid'), + 'client': this.CLIENT_ID + }); - this.ephoto.connect(); + this.ephoto.connect(); - this.ephoto.callOnConnect(function() { - //var today = expires = new Date(); - - //expires.setDate(today.getDate() + 2); + //this.ephoto.File.setMode('embed'); - //CKEDITOR.tools.setCookie( 'drupal_ephoto_dam_authid', self.ephoto.getAuthID()); + //this.ephoto.File.enableDCore(); - self.connected = true; - }); + this.ephoto.callOnConnect(function() { + //var today = expires = new Date(); + + //expires.setDate(today.getDate() + 2); - this.ephoto.File.callOnFileReceived(this.insert.bind(this)); - - this.ephoto.File.get(); + //CKEDITOR.tools.setCookie( 'drupal_ephoto_dam_authid', self.ephoto.getAuthID()); + + self.connected = true; + }); + + this.ephoto.File.callOnFileReceived(this.insert.bind(this)); }, + /** + * + */ + selectVersionUrls: function() { + this.ephoto.File.get(); + }, + + /** + * + */ + selectUrl: function(delta) { + this.ephoto.File.get(); + }, + + /** + * + */ insert: function() { } } + EphotoDamField.load(); + })(jQuery); diff --git a/modules/ephoto_dam_field/src/Plugin/Field/FieldWidget/EphotoDamFieldWidget.php b/modules/ephoto_dam_field/src/Plugin/Field/FieldWidget/EphotoDamFieldWidget.php index 8c7cd3235dd0239e56ee0985eacf43a67d26cf56..6082f25d02ffaef9f75cba98e85c0a54d38f48ee 100644 --- a/modules/ephoto_dam_field/src/Plugin/Field/FieldWidget/EphotoDamFieldWidget.php +++ b/modules/ephoto_dam_field/src/Plugin/Field/FieldWidget/EphotoDamFieldWidget.php @@ -26,13 +26,8 @@ class EphotoDamFieldWidget extends WidgetBase { * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { - $settings = $this->getFieldSettings(); - $settings['delta'] = $delta; - - $settings_encoded = str_replace('"', "'", Json::encode($settings)); - $is_default_values_form = $this->isDefaultValueWidget($form_state); - $version_support = $settings['version_support']; + $version_support = $this->getFieldSetting('version_support'); if (self::$first) { $classes = ['ephoto-dam-field-button', 'ephoto-dam-field-version-button']; @@ -44,9 +39,9 @@ class EphotoDamFieldWidget extends WidgetBase { '#disabled' => $is_default_values_form, '#attributes' => [ 'class' => $classes, - 'onclick' => 'EphotoDamField.select(' . $settings_encoded . ');return false', + 'onclick' => 'EphotoDamField.selectVersionUrls();return false', 'class' => $classes, - ] + ], ]; self::$first = false; @@ -89,7 +84,7 @@ class EphotoDamFieldWidget extends WidgetBase { '#disabled' => $is_default_values_form, '#attributes' => [ 'class' => $classes, - 'onclick' => 'EphotoDamField.select(' . $settings_encoded . ');return false', + 'onclick' => 'EphotoDamField.selectUrl(' . $delta . ');return false', 'class' => $classes ] ];