Skip to content

Commit

Permalink
Merge pull request #55 from esmero/ISSUE-26a
Browse files Browse the repository at this point in the history
ISSUE-26: Add a Cancel Edit button to Webform widget editing
  • Loading branch information
DiegoPino authored Jul 9, 2020
2 parents ed1aedb + 879e03d commit af22cab
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 59 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
}
],
"require": {
"ml/json-ld": "^1.1",
"ml/json-ld": "^1.2",
"mtdowling/jmespath.php":"^2.5",
"strawberryfield/strawberryfield":"dev-8.x-1.0-beta3",
"strawberryfield/format_strawberryfield":"dev-8.x-1.0-beta3",
"drupal/webform": "^5.9",
"drupal/webform": "^5.19",
"drupal/webform_views": "^5.0"
}
}
26 changes: 18 additions & 8 deletions js/hidenodeaction-webform_strawberryfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,27 @@
if ($('.path-node fieldset[data-strawberryfield-selector="strawberry-webform-widget"]').length) {
if ($('.webform-confirmation',context).length) {
// Exclude webform own edit-actions containter
/* And hide, if present the close button but only show save if all are ready.
@TODO we need to figure out what if there are many webforms open at different states in the same
Entity Form.
*/
$('.path-node .node-form div[data-drupal-selector="edit-actions"]').not('.webform-actions').show();
} else if ($('div.field--widget-strawberryfield-webform-inline-widget').length) {
$('.path-node .node-form div[data-drupal-selector="edit-actions"]').not('.webform-actions').hide();
}
var $moderationstate = $('select[data-drupal-selector="edit-moderation-state-0-state"]', context).once('show-hide-actions');
if ($moderationstate.length) {
$('.webform-confirmation').closest('[data-strawberryfield-selector="strawberry-webform-widget"]').each(function() {
var $id = $(this).attr('id') + '-strawberry-webform-close-modal';
$('#' + $id).toggleClass('js-hide');
})

/* var $select = $moderationstate.on('change', function () {
$('.path-node .node-form div[data-drupal-selector="edit-actions"]').not('.webform-actions').show();

}); */
} else if ($('div.field--widget-strawberryfield-webform-inline-widget').length) {
$('.path-node .node-form div[data-drupal-selector="edit-actions"]').not('.webform-actions').hide();
}
var $moderationstate = $('select[data-drupal-selector="edit-moderation-state-0-state"]', context).once('show-hide-actions');
if ($moderationstate.length) {

/* var $select = $moderationstate.on('change', function () {
$('.path-node .node-form div[data-drupal-selector="edit-actions"]').not('.webform-actions').show();
}); */
}
var $nodetitle = $('input[data-drupal-selector="edit-title-0-value"]', context).once('show-hide-actions');
if ($nodetitle.length) {
Expand Down
61 changes: 51 additions & 10 deletions src/Controller/StrawberryRunnerModalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\ContentEntityInterface;


Expand Down Expand Up @@ -52,7 +53,7 @@ public function openModalForm(WebformInterface $webform = NULL, Request $request
list($source_entity_type, $bundle) = explode(':', $source_entity_types);
//@TODO allow some type of per bundle hook?
$state = $request->get('state');

$modal = $request->get('modal') ? $request->get('modal') : FALSE;

// with $uuid the uuid of the entity that is being edited and for which
// a widget is being openend in the form of a webform
Expand Down Expand Up @@ -199,24 +200,64 @@ public function openModalForm(WebformInterface $webform = NULL, Request $request
// Add an AJAX command to open a modal dialog with the form as the content.
//@TODO Allow widget to pass the mode, either inline, Dialog or Append.
//$response->addCommand(new OpenModalDialogCommand(t('Please follow the steps.'), $lawebforma, ['width' => '90%']));

//$response->addCommand(new \Drupal\Core\Ajax\AppendCommand('#edit-field-descriptive-metadata-0', $lawebforma));
// @TODO pass the selector as an argument so we can have many inserts.
$response->addCommand(new \Drupal\Core\Ajax\HtmlCommand('#edit-field-descriptive-metadata-0', $lawebforma));
if ($modal) {
// New window.
$response->addCommand(new OpenModalDialogCommand(t('Please follow the steps.'), $lawebforma, ['width' => '90%']));
}
else {

// inline replacement
$selector = 'edit-'.Html::cleanCssIdentifier($field_name ."_". $delta);
// Selector us built using the field name and the delta.
$response->addCommand(new \Drupal\Core\Ajax\AppendCommand('#'.$selector, $lawebforma));
$selector2 = '[data-drupal-selector="'.$selector .'-strawberry-webform-open-modal"]';
$selector3 = '[data-drupal-selector="'.$selector .'-strawberry-webform-close-modal"]';
$response->addCommand(new \Drupal\Core\Ajax\InvokeCommand($selector2, 'toggleClass', ['js-hide']));
$response->addCommand(new \Drupal\Core\Ajax\InvokeCommand($selector3, 'toggleClass', ['js-hide']));
}
return $response;

}

public function closeModalForm(Request $request)
{

$response = new AjaxResponse();
$response->addCommand(new CloseDialogCommand());
return $response;

public function closeModalForm(Request $request)
{

$state = $request->get('state');
$modal = $request->get('modal') ? $request->get('modal') : FALSE;

// with $uuid the uuid of the entity that is being edited and for which
// a widget is being openend in the form of a webform
// field name the machine name of the field that contains the original data
// inside that source entity

list($source_uuid, $field_name, $delta, $widgetid) = explode(':', $state);
$response = new AjaxResponse();

if ($modal) {
$response->addCommand(new CloseDialogCommand());
return $response;
}
else {
// inline replacement
$selector = 'edit-'.Html::cleanCssIdentifier($field_name ."_". $delta);
// Selector us built using the field name and the delta.
$response->addCommand(new \Drupal\Core\Ajax\RemoveCommand('#'.$selector. ' .webform-ajax-form-wrapper'));
$selector2 = '[data-drupal-selector="'.$selector .'-strawberry-webform-open-modal"]';
$selector3 = '[data-drupal-selector="'.$selector .'-strawberry-webform-close-modal"]';
$response->addCommand(new \Drupal\Core\Ajax\InvokeCommand($selector2, 'toggleClass', ['js-hide']));
$response->addCommand(new \Drupal\Core\Ajax\InvokeCommand($selector3, 'toggleClass', ['js-hide']));
/// Shows the Save buttons back.
/// @TODO this should go into Drupal.behaviors.webformstrawberryHideNodeActions JS
$response->addCommand(new \Drupal\Core\Ajax\InvokeCommand('.path-node .node-form div[data-drupal-selector="edit-actions"]', 'show', []));

}
return $response;


}


}
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,17 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen

// Webform controller wrapper URL
$this_field_name = $this->fieldDefinition->getName();
//@see \Drupal\webform_strawberryfield\Controller\StrawberryRunnerModalController
// We need to assume nothing of this will ever work without AJAX/JS.

$webform_controller_url= Url::fromRoute('webform_strawberryfield.modal_webform',
[
'webform' => $my_webform_machinename,
'source_entity_types' => "$entity_type:$bundle",
'state'=> "$entity_uuid:$this_field_name:$delta:$this_widget_id",
'modal' => FALSE
]
);

$element['strawberry_webform_open_modal'] = [
'#type' => 'link',
'#title' => $this->t('Edit @a', array('@a' => $this->getSetting('placeholder')?: $items->getName())),
Expand All @@ -361,7 +363,26 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
],
],
];

$webform_controller_url_close= Url::fromRoute('webform_strawberryfield.close_modal_webform',
[
'state'=> "$entity_uuid:$this_field_name:$delta:$this_widget_id",
'modal' => FALSE,
]
);
$element['strawberry_webform_close_modal'] = [
'#type' => 'link',
'#title' => $this->t('Cancel @a editing', array('@a' => $this->getSetting('placeholder')?: $items->getName())),
'#url' => $webform_controller_url_close,
'#attributes' => [
'class' => [
'use-ajax',
'button',
'btn-warning',
'btn',
'js-hide'
],
],
];
}


Expand Down Expand Up @@ -525,5 +546,4 @@ protected function addActivityStream(FormStateInterface $form_state) {

}


}
24 changes: 23 additions & 1 deletion src/Plugin/Field/FieldWidget/StrawberryFieldWebFormWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$my_webform->toUrl()->setAbsolute()->toString()
);
} catch (EntityMalformedException $e) {
return $this->_exceptionElement($items, $delta, $element,$form, $form_state);
return $this->_exceptionElement($items, $delta, $element,$form, $form_state);
}

$this_field_name = $this->fieldDefinition->getName();
Expand All @@ -246,6 +246,13 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
'webform' => $my_webform_machinename,
'source_entity_types' => "$entity_type:$bundle",
'state'=> "$entity_uuid:$this_field_name:$delta:$this_widget_id",
'modal' => FALSE
]
);
$webform_controller_url_close= Url::fromRoute('webform_strawberryfield.close_modal_webform',
[
'state'=> "$entity_uuid:$this_field_name:$delta:$this_widget_id",
'modal' => FALSE,
]
);

Expand Down Expand Up @@ -281,6 +288,21 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
],
];

$element['strawberry_webform_close_modal'] = [
'#type' => 'link',
'#title' => $this->t('Cancel @a editing', array('@a' => $this->getSetting('placeholder')?: $items->getName())),
'#url' => $webform_controller_url_close,
'#attributes' => [
'class' => [
'use-ajax',
'button',
'btn-warning',
'btn',
'js-hide'
],
],
];

// The following elements are kinda hidden and match the field properties
$current_value = $items[$delta]->getValue();

Expand Down
5 changes: 2 additions & 3 deletions webform_strawberryfield.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ name: Strawberry Field Webform Integration
description: Provides Webform integrations to feed a field of Strawberries.
package: Archipelago
type: module
core: 8.x
core_version_requirement: ^8.8 || ^9
php: 7.1
dependencies:
- 'drupal:field'
- 'drupal:file'
- 'drupal:system (>= 8.7)'
- 'drupal:user'
- 'strawberryfield'
- 'webform:webform (>= 8.x-5.3)'
- 'webform:webform (>= 8.x-5.19)'
54 changes: 22 additions & 32 deletions webform_strawberryfield.module
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,14 @@ function webform_strawberryfield_preprocess_webform_element_image_file(
$format = $variables['format'];

$uri = $file->getFileUri();
$filename = $file->getFilename();
$fileextension = pathinfo(
$filename,
PATHINFO_EXTENSION
);

$url = Url::fromUri(file_create_url($uri));

if (!$file->isTemporary()) {

$iiifserversettings = \Drupal::config(
Expand All @@ -333,20 +340,12 @@ function webform_strawberryfield_preprocess_webform_element_image_file(
$iiifserversettings->get('pub_server_url'),
$iiifserversettings->get('int_server_url')
);
// Deal with Drupal 8.8.x v/s 8.7
if (method_exists(
\Drupal::service('stream_wrapper_manager'),
'getTarget'
)) {
$iiifidentifier = urlencode(
\Drupal::service('stream_wrapper_manager')->getTarget(
// Deal with Drupal 8.8.x only now
$iiifidentifier = urlencode(
\Drupal::service('stream_wrapper_manager')->getTarget(
$file->getFileUri()
)
);
}
else {
$iiifidentifier = urlencode(file_uri_target($file->getFileUri()));
}
)
);

if ($iiifidentifier == NULL || empty($iiifidentifier)) {
// Nothing to do, lets leave this untouched.
Expand Down Expand Up @@ -380,10 +379,7 @@ function webform_strawberryfield_preprocess_webform_element_image_file(

$route_parameters = [
'uuid' => $file->uuid(),
'format' => 'default.' . pathinfo(
$file->getFilename(),
PATHINFO_EXTENSION
),
'format' => 'default.' . $fileextension,
];
$publicurl = Url::fromRoute(
'format_strawberryfield.tempiiifbinary',
Expand All @@ -403,7 +399,7 @@ function webform_strawberryfield_preprocess_webform_element_image_file(
$cache_key = md5($uri);
$templocation = \Drupal::service('file_system')->copy(
$uri,
'temporary://sbr_' . $cache_key . '_' . basename($uri),
'temporary://sbr_' . $cache_key . '.' . $fileextension,
FileSystemInterface::EXISTS_REPLACE
);
$templocation = \Drupal::service('file_system')->realpath(
Expand All @@ -417,18 +413,14 @@ function webform_strawberryfield_preprocess_webform_element_image_file(
}
if ($templocation) {
$result = exec(
'exiftool -json -q -a -gps:all -Common "-gps*" -xmp:all -ImageWidth -ImageHeight -Canon -Nikon-AllDates -pdf:all -ee -MIMEType ' . escapeshellcmd($templocation),
'exiftool -json -q -a -gps:all -Common "-gps*" -xmp:all -ImageWidth -ImageHeight -Canon -Nikon-AllDates -pdf:all -ee -MIMEType ' . escapeshellarg(
$templocation
),
$output,
$status
);
if ($status != 0) {
// This message is annoying. Appears over and over.
/*\Drupal::service('messenger')
->addMessage(
t('Ups. We could not get EXIF from your file. Sorry.')
);*/
}
else {
if ($status == 0) {

$more_str = implode('', $output);
$json = json_decode($more_str, TRUE);
$json_error = json_last_error();
Expand Down Expand Up @@ -460,9 +452,8 @@ function webform_strawberryfield_preprocess_webform_element_image_file(
}
}

$extension = pathinfo($uri, PATHINFO_EXTENSION);
$is_image = in_array(
$extension,
$fileextension,
['gif', 'png', 'jpg', 'jpeg', 'jp2', 'tiff']
);

Expand Down Expand Up @@ -491,8 +482,8 @@ function webform_strawberryfield_preprocess_webform_element_image_file(
'#uri' => $url,
'#attributes' => [
'class' => ['webform-strawberryfield-image-file'],
'alt' => $file->getFilename(),
'title' => $file->getFilename(),
'alt' => $filename,
'title' => $filename,
'style' => "max-width:{$max_width}px;height:auto",
],
];
Expand Down Expand Up @@ -531,6 +522,5 @@ function webform_strawberryfield_preprocess_webform_element_image_file(
break;
}
}

}
}

0 comments on commit af22cab

Please sign in to comment.