Skip to content

Commit 5820c7b

Browse files
committed
Updates caching to clear properly
fix(PatternkitBlock, jsoneditor)
1 parent 37d566e commit 5820c7b

File tree

3 files changed

+57
-25
lines changed

3 files changed

+57
-25
lines changed

js/patternkit.jsoneditor.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@ sourceURL=patternkit.jsoneditor.js
12
/**
23
* @file
34
* Provides Twig Pattern Library Editing Functionality.
@@ -128,18 +129,37 @@
128129
}
129130
);
130131
JSONEditor.plugins.sceditor.emoticonsEnabled = false;
131-
132-
editor.on('ready', function () {
132+
var saveSchema = function () {
133+
$('#schema_instance_config').val(JSON.stringify(editor.getValue()));
133134
if (window.M) {
134135
window.M.updateTextFields();
135136
}
136-
});
137-
editor.on('change', function () {
138-
document.getElementById('schema_instance_config').value = JSON.stringify(editor.getValue());
137+
};
138+
139+
editor.on('ready', function () {
139140
if (window.M) {
140141
window.M.updateTextFields();
141142
}
142143
});
144+
editor.on('change', saveSchema);
145+
$('[data-drupal-selector="edit-actions-submit"]').on('input', function (e) {
146+
saveSchema();
147+
});
148+
// Drupal triggers Ajax submit via input events.
149+
// This is before allowing other events, so we need to add a pre-hook
150+
// to trigger the editor update with latest field values.
151+
// @TODO Add handling for AJAX errors and re-attach.
152+
Drupal.Ajax.prototype.beforeSubmit = function(formValues, element, options) {
153+
debugger;
154+
editor.disable();
155+
saveSchema();
156+
for (var v = 0; v < formValues.length; v++) {
157+
if (formValues[v]['name'] === 'settings[instance_config]') {
158+
formValues[v]['value'] = JSON.stringify(editor.getValue());
159+
}
160+
}
161+
editor.destroy();
162+
};
143163
});
144164
}
145165
};

src/JSONSchemaEditorTrait.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,14 @@ public function schemaEditor(array $schema, PatternEditorConfig $config): array
104104
}
105105
}
106106
// @todo Move to own JS file & Drupal Settings config var.
107-
$markup = <<<HTML
108-
<div id="editor-shadow-injection-target" style="all: initial; background: white; display: inline-block; width: 100%;"></div>
109-
HTML;
110-
111107
return [
112-
'#type' => 'markup',
113-
'#markup' => $markup,
108+
'#type' => 'html_tag',
109+
'#tag' => 'div',
110+
'#value' => '',
111+
'#attributes' => [
112+
'id' => 'editor-shadow-injection-target',
113+
'style' => ['all: initial; background: white; display: inline-block; width: 100%;']
114+
],
114115
'#attached' => [
115116
'drupalSettings' => [
116117
'patternkitEditor' => $editor_settings,

src/Plugin/Block/PatternkitBlock.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,23 @@ public function blockSubmit($form, FormStateInterface $form_state) {
265265
$this->setConfiguration($updated_config + $configuration);
266266
// Invalidate the block cache to update custom block-based derivatives.
267267
$this->blockManager->clearCachedDefinitions();
268+
// Invalidate custom cache.
269+
// @todo Replace with cache_backend->set().
270+
$language = \Drupal::languageManager()->getCurrentLanguage();
271+
$context = $this->getContextValues();
272+
$configuration = $this->getConfiguration();
273+
$pattern_id = $this->getDerivativeId();
274+
$instance_id = $configuration['instance_uuid'] ?? $pattern_id;
275+
$cid = "patternkit:{$pattern_id}:{$instance_id}:";
276+
$cid .= md5(
277+
$this->serializer::encode([
278+
\Drupal::currentUser()->isAuthenticated(),
279+
$context,
280+
$configuration,
281+
$language->getId(),
282+
])
283+
);
284+
$this->cache->delete($cid);
268285
parent::blockSubmit($form, $form_state);
269286
}
270287

@@ -288,13 +305,7 @@ public function build() {
288305

289306
// If an instance configuration provides a UUID, use it. If not, we should
290307
// not cache this item because the uuid will be different each time.
291-
if (empty($configuration['instance_uuid'])) {
292-
$cacheable = FALSE;
293-
$instance_id = $this->getDerivativeId();
294-
}
295-
else {
296-
$instance_id = $configuration['instance_uuid'];
297-
}
308+
$instance_id = $configuration['instance_uuid'] ?? $pattern_id;
298309

299310
// Create the cache key to be used for this object. Note that we are relying
300311
// on code elsewhere to clear this cache on modification. The md5 against
@@ -303,7 +314,7 @@ public function build() {
303314
// logged-in check to prevent cached admin links from appearing in frontend.
304315
// It also mitigates the difference between esi delivery when logged in vs
305316
// not.
306-
// @todo Replace with getCacheTags().
317+
// @todo Replace with getCacheTags() and cache_backend->get().
307318
$language = \Drupal::languageManager()->getCurrentLanguage();
308319
$cid = "patternkit:{$pattern_id}:{$instance_id}:";
309320
$cid .= md5(
@@ -330,7 +341,7 @@ public function build() {
330341

331342
// Initialize our static (if necessary).
332343
if ($is_processed === NULL) {
333-
$is_processed = array();
344+
$is_processed = [];
334345
}
335346

336347
// If we've already processed this module, don't add it to active again.
@@ -352,12 +363,12 @@ public function build() {
352363
}
353364

354365
// If the item is cache-able, fetch it and return it.
355-
if ($cacheable === TRUE) {
356-
// Attempt to fetch the cached pane.
357-
$cached = $this->cache->get($cid, 'cache_patternkit');
366+
if ($cacheable) {
367+
// Attempt to fetch the cached block.
368+
$cached = $this->cache->get($cid);
358369
if ($cached !== FALSE) {
359370

360-
// Set flag to let other modules know content is patternkit stuff.
371+
// Set flag to let other modules know content is patternkit cache.
361372
$cached->patternkit = 1;
362373

363374
return $cached->data;
@@ -411,7 +422,7 @@ public function build() {
411422
'#attached' => $config['pkdata']['attachments'] ?? [],
412423
];
413424
// Save to the cache bin (if caching is enabled).
414-
if ($cacheable === TRUE) {
425+
if ($cacheable) {
415426
$this->cache->set($cid, $content, time() + $ttl, ['cache_patternkit']);
416427
}
417428

0 commit comments

Comments
 (0)