Commit 75608d37 authored by ephoto_dam's avatar ephoto_dam

Ephoto Dam field support in progress

parent 78421148
......@@ -8,3 +8,9 @@ display:
header: true
dependencies:
- core/jquery
settings:
css:
component:
plugin/plugin.css: {}
header: true
......@@ -28,16 +28,12 @@
display:none !important
}
/*
.ephoto-dam-field-version-field {
float:left;
margin-right:5px;
margin-top:4px;
#edit-settings-url {
width:350px
}
*/
/*
.ephoto-dam-field-value-field {
margin-top:-5px
#edit-settings-captions-format {
width:350px;
height:50px;
resize:none
}
*/
\ No newline at end of file
......@@ -6,7 +6,27 @@
*/
(function ($) {
EPHOTO_DAM_FIELD = {
EphotoDamField = {
/**
* EPHOTO ID of Drupal Addon
*/
CLIENT_ID : 'CaCvqS5H',
/**
* Settings
*/
settings: null,
/**
* Ephoto Dam instance
*/
ephoto: null,
/**
* Ephoto Dam Server connected
*/
connected: false,
enableVersionSupport: function(el) {
if(el.checked) {
$('.ephoto-dam-field-version-field').removeClass('ephoto-dam-field-hidden');
......@@ -21,7 +41,51 @@
$('.ephoto-dam-field-version-button').addClass('ephoto-dam-field-hidden');
$('.ephoto-dam-field-field-button').removeClass('ephoto-dam-field-hidden');
}
},
select: function(settings) {
this.settings = settings;
$.getScript(settings.url + 'api/apiJS.js', this.initApi.bind(this));
},
initApi: function() {
var self = this;
if(!ePhoto) {
console.log(Drupal.t('Error loading the Ephoto Dam API'));
return;
}
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.connect();
this.ephoto.callOnConnect(function() {
//var today = expires = new Date();
//expires.setDate(today.getDate() + 2);
//CKEDITOR.tools.setCookie( 'drupal_ephoto_dam_authid', self.ephoto.getAuthID());
self.connected = true;
});
this.ephoto.File.callOnFileReceived(this.insert.bind(this));
this.ephoto.File.get();
},
insert: function() {
}
};
}
})(jQuery);
<?php
namespace Drupal\ephoto_dam_field\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldConfig;
use Symfony\Component\DependencyInjection\ContainerInterface;
// https://happyculture.coop/blog/drupal-8-creer-un-formateur-de-champ
/**
* Plugin implementation of the Ephoto Dam field formatter
*
* @FieldFormatter(
* id = "ephoto_dam_field_format",
* label = @Translation("Ephoto Dam Field Format"),
* field_types = {
* "ephoto_dam_field"
* }
* )
*/
class EphotoDamFieldFormat extends FormatterBase implements ContainerFactoryPluginInterface {
/**
* Constructs a new instance of the plugin.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Third party settings.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, $settings, $label, $view_mode, $third_party_settings) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['label'],
$configuration['view_mode'],
$configuration['third_party_settings']
);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
foreach ($items as $delta => $item) {
$provider = $this->providerManager->loadProviderFromInput($item->value);
if (!$provider) {
$element[$delta] = ['#theme' => 'video_embed_field_missing_provider'];
}
else {
$autoplay = $this->currentUser->hasPermission('never autoplay videos') ? FALSE : $this->getSetting('autoplay');
$element[$delta] = $provider->renderEmbedCode($this->getSetting('width'), $this->getSetting('height'), $autoplay);
$element[$delta]['#cache']['contexts'][] = 'user.permissions';
$element[$delta] = [
'#type' => 'container',
'#attributes' => ['class' => [Html::cleanCssIdentifier(sprintf('video-embed-field-provider-%s', $provider->getPluginId()))]],
'children' => $element[$delta],
];
// For responsive videos, wrap each field item in it's own container.
if ($this->getSetting('responsive')) {
$element[$delta]['#attached']['library'][] = 'video_embed_field/responsive-video';
$element[$delta]['#attributes']['class'][] = 'video-embed-field-responsive-video';
}
}
}
return $element;
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'responsive' => TRUE,
'width' => '854',
'height' => '480',
'autoplay' => TRUE,
];
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['autoplay'] = [
'#title' => $this->t('Autoplay'),
'#type' => 'checkbox',
'#description' => $this->t('Autoplay the videos for users without the "never autoplay videos" permission. Roles with this permission will bypass this setting.'),
'#default_value' => $this->getSetting('autoplay'),
];
$elements['responsive'] = [
'#title' => $this->t('Responsive Video'),
'#type' => 'checkbox',
'#description' => $this->t("Make the video fill the width of it's container, adjusting to the size of the user's screen."),
'#default_value' => $this->getSetting('responsive'),
];
// Loosely match the name attribute so forms which don't have a field
// formatter structure (such as the WYSIWYG settings form) are also matched.
$responsive_checked_state = [
'visible' => [
[
':input[name*="responsive"]' => ['checked' => FALSE],
],
],
];
$elements['width'] = [
'#title' => $this->t('Width'),
'#type' => 'number',
'#field_suffix' => 'px',
'#default_value' => $this->getSetting('width'),
'#required' => TRUE,
'#size' => 20,
'#states' => $responsive_checked_state,
];
$elements['height'] = [
'#title' => $this->t('Height'),
'#type' => 'number',
'#field_suffix' => 'px',
'#default_value' => $this->getSetting('height'),
'#required' => TRUE,
'#size' => 20,
'#states' => $responsive_checked_state,
];
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$dimensions = $this->getSetting('responsive') ? $this->t('Responsive') : $this->t('@widthx@height', ['@width' => $this->getSetting('width'), '@height' => $this->getSetting('height')]);
$summary[] = $this->t('Embedded Video (@dimensions@autoplay).', [
'@dimensions' => $dimensions,
'@autoplay' => $this->getSetting('autoplay') ? $this->t(', autoplaying') : '',
]);
return $summary;
}
/**
* Get an instance of the Video field formatter plugin.
*
* This is useful because there is a lot of overlap to the configuration and
* display of a video in a WYSIWYG and configuring a field formatter. We
* get an instance of the plugin with our own WYSIWYG settings shimmed in,
* as well as a fake field_definition because one in this context doesn't
* exist. This allows us to reuse aspects such as the form and settings
* summary for the WYSIWYG integration.
*
* @param array $settings
* The settings to pass to the plugin.
*
* @return static
* The formatter plugin.
*/
public static function mockInstance($settings) {
return \Drupal::service('plugin.manager.field.formatter')->createInstance('video_embed_field_video', [
'settings' => !empty($settings) ? $settings : [],
'third_party_settings' => [],
'field_definition' => new FieldConfig([
'field_name' => 'mock',
'entity_type' => 'mock',
'bundle' => 'mock',
]),
'label' => '',
'view_mode' => '',
]);
}
}
......@@ -85,15 +85,25 @@ class EphotoDamField extends FieldItemBase {
* {@inheritdoc}
*/
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$form['version_support'] = [
'#title' => $this->t('Version support'),
'#description' => $this->t('An url will be automatically associated with each version with the same name in Ephoto Dam') . '.',
'#type' => 'checkbox',
'#default_value' => $this->getSetting('version_support'),
'#attributes' => [
'onclick' => 'EPHOTO_DAM_FIELD.enableVersionSupport(this)'
]
$form['url'] = [
'#title' => $this->t('Server URL'),
'#type' => 'url',
'#default_value' => $this->getSetting('url'),
'#description' => $this->t('Please enter the URL of your Ephoto Dam software'),
'#required' => true,
'#attributes' => ['placeholder' => $this->t('https://ephoto.mycompany.com/')],
'#size' => 50,
];
$form['version_support'] = [
'#title' => $this->t('Version support'),
'#description' => $this->t('An url will be automatically associated with each version with the same name in Ephoto Dam') . '.',
'#type' => 'checkbox',
'#default_value' => $this->getSetting('version_support'),
'#attributes' => [
'onclick' => 'EphotoDamField.enableVersionSupport(this)'
]
];
return $form;
}
......@@ -103,6 +113,7 @@ class EphotoDamField extends FieldItemBase {
*/
public static function defaultFieldSettings() {
return [
'url' => '',
'version_support' => false
];
}
......
......@@ -5,6 +5,7 @@ namespace Drupal\ephoto_dam_field\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Serialization\Json;
/**
* Ephoto Dam Widget
......@@ -25,8 +26,13 @@ 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 = $this->getFieldSetting('version_support');
$version_support = $settings['version_support'];
if (self::$first) {
$classes = ['ephoto-dam-field-button', 'ephoto-dam-field-version-button'];
......@@ -34,11 +40,12 @@ class EphotoDamFieldWidget extends WidgetBase {
$element['version-import'] = [
'#type' => 'button',
'#value' => $this->t('Import'),
'#value' => $this->t('Select'),
'#disabled' => $is_default_values_form,
'#attributes' => [
'class' => $classes,
'onclick' => 'alert("Import!");return false',
'class' => $classes
'onclick' => 'EphotoDamField.select(' . $settings_encoded . ');return false',
'class' => $classes,
]
];
......@@ -64,6 +71,7 @@ class EphotoDamFieldWidget extends WidgetBase {
'#type' => 'textfield',
'#title' => '&nbsp;',
'#default_value' => isset($items[$delta]->version) ? $items[$delta]->version : null,
'#disabled' => !$is_default_values_form,
'#size' => 20,
'#maxlength' => 60,
'#attributes' => [
......@@ -78,9 +86,10 @@ class EphotoDamFieldWidget extends WidgetBase {
$element['field-import'] = [
'#type' => 'button',
'#value' => $this->t('Import'),
'#disabled' => $is_default_values_form,
'#attributes' => [
'class' => $classes,
'onclick' => 'alert("Import!");return false',
'onclick' => 'EphotoDamField.select(' . $settings_encoded . ');return false',
'class' => $classes
]
];
......
......@@ -10,8 +10,7 @@
/**
* A CKEditor plugin for vido embeds.
*/
CKEDITOR.plugins.add('ephoto_dam', {
CKEDITOR.plugins.add('ephoto_dam', {
/**
* EPHOTO ID of Drupal Addon
*/
......
......@@ -20,142 +20,142 @@ use Drupal\ephoto_dam\Plugin\Field\FieldFormatter\EphotoDamField;
* )
*/
class EphotoDam extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface {
/**
* Default settings
*
* @const
*/
private const DEFAULT_SETTINGS = [
'url' => '',
'captions' => true,
'captions_format' => '[dc:rights]',
'zoom' => true,
'images_size' => 320,
'videos_size' => 360,
'documents_size' => 320
];
/**
* {@inheritdoc}
*/
public function getFile() {
return drupal_get_path('module', 'ephoto_dam') . '/plugin/plugin.js';
}
/**
* {@inheritdoc}
*/
public function getConfig(Editor $editor) {
return ['ephoto_dam' => $this->getSettings($editor)];
}
/**
* {@inheritdoc}
*/
public function getButtons() {
return [
'ephoto_dam' => [
'label' => $this->t('Select a file from Ephoto Dam'),
'image' => drupal_get_path('module', 'ephoto_dam') . '/plugin/icon.png',
],
];
}
private const DEFAULT_SETTINGS = [
'url' => '',
'captions' => true,
'captions_format' => '[dc:rights]',
'zoom' => true,
'images_size' => 320,
'videos_size' => 360,
'documents_size' => 320
];
/**
* {@inheritdoc}
*/
public function getFile() {
return drupal_get_path('module', 'ephoto_dam') . '/plugin/plugin.js';
}
/**
* {@inheritdoc}
*/
public function getConfig(Editor $editor) {
return ['ephoto_dam' => $this->getSettings($editor)];
}
/**
* {@inheritdoc}
*/
public function getButtons() {
return [
'ephoto_dam' => [
'label' => $this->t('Select a file from Ephoto Dam'),
'image' => drupal_get_path('module', 'ephoto_dam') . '/plugin/icon.png',
],
];
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
$settings = (object) $this->getSettings($editor);
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
$settings = (object) $this->getSettings($editor);
$form['#attached']['library'][] = 'ephoto_dam/settings';
$form['#attached']['library'][] = 'ephoto_dam/settings';
$form['url'] = [
'#title' => $this->t('Server URL'),
'#type' => 'url',
'#default_value' => $settings->url,
'#description' => $this->t('Please enter the URL of your Ephoto Dam software'),
'#required' => true,
'#attributes' => ['placeholder' => $this->t('https://ephoto.mycompany.com/')],
'#size' => 50,
];
$form['url'] = [
'#title' => $this->t('Server URL'),
'#type' => 'url',
'#default_value' => $settings->url,
'#description' => $this->t('Please enter the URL of your Ephoto Dam software'),
'#required' => true,
'#attributes' => ['placeholder' => $this->t('https://ephoto.mycompany.com/')],
'#size' => 50,
];
$form['captions'] = [
'#title' => $this->t('Captions'),
'#type' => 'checkbox',
'#default_value' => $settings->captions,
'#description' => $this->t('Display the caption under images, videos and documents'),
'#required' => false,
];
$form['captions'] = [
'#title' => $this->t('Captions'),
'#type' => 'checkbox',
'#default_value' => $settings->captions,
'#description' => $this->t('Display the caption under images, videos and documents'),
'#required' => false,
];
$form['captions_format'] = [
'#title' => $this->t('Captions format'),
'#type' => 'textarea',
'#default_value' => $settings->captions_format,
'#required' => false,
];
$form['captions_format'] = [
'#title' => $this->t('Captions format'),
'#type' => 'textarea',
'#default_value' => $settings->captions_format,
'#required' => false,
];
$form['zoom'] = [
'#title' => $this->t('Zoom'),
'#type' => 'checkbox',
'#default_value' => $settings->zoom,
'#description' => $this->t('View large images by clicking on them'),
'#required' => false,
];
$form['zoom'] = [
'#title' => $this->t('Zoom'),
'#type' => 'checkbox',
'#default_value' => $settings->zoom,
'#description' => $this->t('View large images by clicking on them'),
'#required' => false,
];
$form['images_size'] = [
'#title' => $this->t('Images size'),
'#type' => 'number',
'#default_value' => $settings->images_size,
'#description' => $this->t('Please enter the size of the images for the display'),
'#required' => true,
'#field_suffix' => 'px',
'#attributes' => ['min' => 0, 'max' => 6000],
'#size' => 20,
];
$form['images_size'] = [
'#title' => $this->t('Images size'),
'#type' => 'number',
'#default_value' => $settings->images_size,
'#description' => $this->t('Please enter the size of the images for the display'),
'#required' => true,
'#field_suffix' => 'px',
'#attributes' => ['min' => 0, 'max' => 6000],
'#size' => 20,
];
$form['videos_size'] = [
'#title' => $this->t('Videos size'),
'#type' => 'number',
'#default_value' => $settings->videos_size,
'#description' => $this->t('Please enter the size of the videos for the display'),
'#required' => true,
'#field_suffix' => 'px',
'#attributes' => ['min' => 0, 'max' => 6000],