Commit 1bfd486a authored by Primsi's avatar Primsi

Issue #2776787 by dawehner, Primsi, chr.fritsch, MaskOta: Create IEF widget...

Issue #2776787 by dawehner, Primsi, chr.fritsch, MaskOta: Create IEF widget which allows to edit the media entities after the upload
parent 46a6535a
......@@ -40,3 +40,26 @@ entity_browser.browser.widget.dropzonejs_media_entity:
extensions:
type: string
label: 'Allowed file extensions'
entity_browser.browser.widget.dropzonejs_media_entity_inline_entity_form:
type: mapping
label: 'Media Entity DropzoneJS with edit widget configuration'
mapping:
media_entity_bundle:
type: string
label: 'Media type'
submit_text:
type: string
label: 'Submit button text'
upload_location:
type: string
label: 'Upload location'
dropzone_description:
type: string
label: 'Dropzone drag-n-drop zone text'
max_filesize:
type: string
label: 'Maximum size of files'
extensions:
type: string
label: 'Allowed file extensions'
......@@ -4,3 +4,9 @@ common:
js/dropzonejs_eb_widget.common.js: {}
dependencies:
- dropzonejs/integration
ief_edit:
version: VERSION
js:
js/dropzonejs_eb_widget.ief_edit.js: {}
dependencies:
- dropzonejs_eb_widget/common
......@@ -6,7 +6,7 @@
(function ($, Drupal, drupalSettings) {
"use strict";
Drupal.behaviors.dropzonejsEbWidgetCommon = {
Drupal.behaviors.dropzonejsPostIntegrationEbWidgetCommon = {
attach: function(context) {
if (typeof drupalSettings.dropzonejs.instances !== "undefined") {
_.each(drupalSettings.dropzonejs.instances, function (item) {
......
/**
* @file dropzonejs_eb_widget.common.js
*
* Bundles various dropzone eb widget behaviours.
*/
(function ($, Drupal, drupalSettings) {
"use strict";
Drupal.behaviors.dropzonejsPostIntegrationEbWidgetEditJs = {
attach: function(context) {
if (typeof drupalSettings.dropzonejs.instances !== "undefined") {
_.each(drupalSettings.dropzonejs.instances, function (item) {
var $form = $(item.instance.element).parents('form');
if ($form.hasClass("dropzonejs-disable-submit")) {
var $submit = $form.find('.is-entity-browser-submit');
$submit.prop("disabled", false);
item.instance.on("queuecomplete", function () {
var $form = this;
$('#edit-edit', $form).trigger('mousedown');
}.bind($form));
}
});
}
}
};
}(jQuery, Drupal, drupalSettings));
......@@ -165,9 +165,18 @@ class DropzoneJsEbWidget extends WidgetBase {
protected function getFiles(array $form, FormStateInterface $form_state) {
$config = $this->getConfiguration();
$additional_validators = ['file_validate_size' => [Bytes::toInt($config['settings']['max_filesize']), 0]];
if (!$this->files) {
$this->files = [];
foreach ($form_state->getValue(['upload', 'uploaded_files'], []) as $file) {
$files = $form_state->get(['dropzonejs', 'files']);
if ($files) {
$this->files = $files;
return $files;
}
// We do some casting because $form_state->getValue() might return NULL.
foreach ((array) $form_state->getValue(['upload', 'uploaded_files'], []) as $file) {
$entity = $this->dropzoneJsUploadSave->createFile(
$file['path'],
$this->getUploadLocation(),
......@@ -179,6 +188,7 @@ class DropzoneJsEbWidget extends WidgetBase {
}
}
$form_state->set(['dropzonejs', 'files'], $this->files);
return $this->files;
}
......
<?php
namespace Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\dropzonejs\Events\DropzoneMediaEntityCreateEvent;
use Drupal\dropzonejs\Events\Events;
use Drupal\entity_browser\WidgetBase;
use Drupal\inline_entity_form\Element\InlineEntityForm;
/**
* Provides an Entity Browser widget that uploads and edit new files.
*
* @EntityBrowserWidget(
* id = "dropzonejs_media_entity_inline_entity_form",
* label = @Translation("Media Entity DropzoneJS with edit"),
* description = @Translation("Adds DropzoneJS upload integration that saves Media entities and allows to edit them.")
* )
*/
class InlineEntityFormMediaWidget extends MediaEntityDropzoneJsEbWidget {
/**
* {@inheritdoc}
*/
public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
$form = parent::getForm($original_form, $form_state, $additional_widget_parameters);
// @todo Remove this when/if EB provides a way to define dependencies.
if (!$this->moduleHandler->moduleExists('inline_entity_form')) {
return [
'#type' => 'container',
'error' => [
'#markup' => $this->t('Missing requirement: in order to use this widget you have to install Inline entity form module first'),
],
];
}
$form['#attached']['library'][] = 'dropzonejs_eb_widget/ief_edit';
$form['edit'] = [
'#type' => 'submit',
'#value' => $this->t('Edit'),
'#attributes' => [
'class' => ['js-hide'],
],
'#ajax' => [
'wrapper' => 'ief-dropzone-upload',
'callback' => [static::class, 'onEdit'],
'effect' => 'fade',
],
'#submit' => [
[$this, 'submitEdit'],
],
];
$form['entities']['#prefix'] = '<div id="ief-dropzone-upload">';
$form['entities']['#suffix'] = '</div>';
$form += ['entities' => []];
if ($entities = $form_state->get('uploaded_entities')) {
foreach ($entities as $entity) {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$form['entities'][$entity->uuid()] = [
'#type' => 'inline_entity_form',
'#entity_type' => $entity->getEntityTypeId(),
'#bundle' => $entity->bundle(),
'#default_value' => $entity,
'#form_mode' => 'media_browser',
];
}
}
if (!empty(Element::children($form['entities']))) {
// Make it possible to select those submitted entities.
$pos = array_search('dropzonejs-disable-submit', $original_form['#attributes']['class']);
if ($pos !== FALSE) {
unset($original_form['#attributes']['class'][$pos]);
}
}
$form['actions']['submit'] += ['#submit' => []];
return $form;
}
/**
* Submit callback for the edit button.
*
* @param array $form
* Form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form object.
*/
public function submitEdit(array $form, FormStateInterface $form_state) {
$form_state->setRebuild(TRUE);
// Files have to saved before they can be viewed in the IEF form.
$media_entities = $this->prepareEntities($form, $form_state);
$source_field = $this->getBundle()->getTypeConfiguration()['source_field'];
foreach ($media_entities as $media_entity) {
$file = $media_entity->$source_field->entity;
$file->save();
$media_entity->$source_field->target_id = $file->id();
}
$form_state->set('uploaded_entities', $media_entities);
}
/**
* Ajax callback triggered when hitting the edit button.
*
* @param array $form
* The form.
*
* @return array
* Returns the entire form.
*/
public static function onEdit(array $form) {
return $form['widget']['entities'];
}
/**
* {@inheritdoc}
*/
public function validate(array &$form, FormStateInterface $form_state) {
// Skip the DropzoneJsEbWidget specific validations.
WidgetBase::validate($form, $form_state);
}
/**
* Prepares entities from the form.
*
* @param array $form
* The form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return \Drupal\media_entity\MediaInterface[]
* The prepared media entities.
*/
protected function prepareEntitiesFromForm($form, FormStateInterface $form_state) {
$media_entities = [];
foreach (Element::children($form['widget']['entities']) as $key) {
/** @var ContentEntityInterface $entity */
$entity = $form['widget']['entities'][$key]['#entity'];
$inline_entity_form_handler = InlineEntityForm::getInlineFormHandler($entity->getEntityTypeId());
$inline_entity_form_handler->entityFormSubmit($form['widget']['entities'][$key], $form_state);
$media_entities[] = $entity;
}
return $media_entities;
}
/**
* {@inheritdoc}
*/
public function submit(array &$element, array &$form, FormStateInterface $form_state) {
$media_entities = $this->prepareEntitiesFromForm($form, $form_state);
$source_field = $this->getBundle()->getTypeConfiguration()['source_field'];
foreach ($media_entities as $media_entity) {
$file = $media_entity->{$source_field}->entity;
/** @var \Drupal\dropzonejs\Events\DropzoneMediaEntityCreateEvent $event */
$event = $this->eventDispatcher->dispatch(Events::MEDIA_ENTITY_CREATE, new DropzoneMediaEntityCreateEvent($media_entity, $file, $form, $form_state, $element));
$media_entity = $event->getMediaEntity();
$media_entity->save();
}
if (!empty(array_filter($media_entities))) {
$this->selectEntities($media_entities, $form_state);
$this->clearFormValues($element, $form_state);
}
}
}
......@@ -155,18 +155,22 @@ class DropzoneJs extends FormElement {
$name = self::fixTmpFilename($name);
$name = file_munge_filename($name, self::getValidExtensions($element));
// Finaly rename the file and add it to results.
$new_filepath = "$temp_path/$name";
$move_result = file_unmanaged_move($old_filepath, $new_filepath);
if ($move_result) {
$return['uploaded_files'][] = [
'path' => $move_result,
'filename' => $name,
];
}
else {
drupal_set_message(t('There was a problem while processing the file named @name', ['@name' => $name]), 'error');
// Potentially we moved the file already, so let's check first whether
// we still have to move.
if (file_exists($old_filepath)) {
// Finaly rename the file and add it to results.
$new_filepath = "$temp_path/$name";
$move_result = file_unmanaged_move($old_filepath, $new_filepath);
if ($move_result) {
$return['uploaded_files'][] = [
'path' => $move_result,
'filename' => $name,
];
}
else {
drupal_set_message(t('There was a problem while processing the file named @name', ['@name' => $name]), 'error');
}
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment