From 8ae62abb0252e0480e572f7a33a645505fcc18d1 Mon Sep 17 00:00:00 2001 From: Diego Pino Navarro Date: Tue, 9 Aug 2022 23:07:06 -0400 Subject: [PATCH 1/3] Larger refactor Will of course test more tomorrow. This has been keeping me awake for too long and is a powerful/useful tool for Allison and Katie (if it works) --- .../Action/AmiStrawberryfieldCSVexport.php | 1 - .../Action/AmiStrawberryfieldJsonAsText.php | 7 +++ .../AmiStrawberryfieldJsonAsWebform.php | 60 +++++++++---------- .../Action/AmiStrawberryfieldJsonPatch.php | 6 ++ 4 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/Plugin/Action/AmiStrawberryfieldCSVexport.php b/src/Plugin/Action/AmiStrawberryfieldCSVexport.php index 6026b4c..0cc53c6 100644 --- a/src/Plugin/Action/AmiStrawberryfieldCSVexport.php +++ b/src/Plugin/Action/AmiStrawberryfieldCSVexport.php @@ -575,5 +575,4 @@ public function setConfiguration(array $configuration) { ); // TODO: Change the autogenerated stub } - } diff --git a/src/Plugin/Action/AmiStrawberryfieldJsonAsText.php b/src/Plugin/Action/AmiStrawberryfieldJsonAsText.php index 433f4aa..4bb69af 100644 --- a/src/Plugin/Action/AmiStrawberryfieldJsonAsText.php +++ b/src/Plugin/Action/AmiStrawberryfieldJsonAsText.php @@ -304,5 +304,12 @@ public function getPluginDefinition() { return parent::getPluginDefinition(); // TODO: Change the autogenerated stub } + /** + * {@inheritdoc} + */ + public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { + return $object->access('update', $account, $return_as_object); + } + } diff --git a/src/Plugin/Action/AmiStrawberryfieldJsonAsWebform.php b/src/Plugin/Action/AmiStrawberryfieldJsonAsWebform.php index a9e37d4..e523176 100644 --- a/src/Plugin/Action/AmiStrawberryfieldJsonAsWebform.php +++ b/src/Plugin/Action/AmiStrawberryfieldJsonAsWebform.php @@ -62,7 +62,6 @@ public function buildPreConfigurationForm(array $element, array $values, FormSta public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form_state->setAlwaysProcess(TRUE); $webform = $this->AmiUtilityService->getWebforms(); - $form_state->disableCache(); $form['#tree'] = TRUE; $form['webform'] =[ '#type' => 'select', @@ -85,7 +84,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta ]; $form['elements_rendered'] = [ '#tree' => TRUE, - '#type' => 'fieldset', + '#type' => 'container', '#prefix' => '
', '#suffix' => '
', ]; @@ -102,7 +101,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta } if ($webform_id) { - /* @var \Drupal\webform\Entity\Webform $webform_entity */ $webform_entity = $this->entityTypeManager->getStorage('webform')->load($webform_id); $anyelement = $webform_entity->getElementsInitializedAndFlattened('update'); @@ -142,8 +140,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta if ($webform_entity && $chosen_element) { $myelement = $webform_entity->getElementDecoded($chosen_element); - - //$myelement2 = \Drupal::service('plugin.manager.webform.element')->processElements($myelement); $libraries = $webform_entity->getSubmissionForm()['#attached']['library'] ?? []; $form['#attached']['library'] = ($form['#attached']['library'] ?? []) + $libraries; $cleanelement = []; @@ -154,16 +150,11 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta } $cleanelement['#required'] = FALSE; $cleanelement['#validated'] = FALSE; + $form['elements_rendered']['jsonfind_element_'.$chosen_element]= $cleanelement; - $cleanelement['#default_value'] = $form_state->getValue('jsonfind_element', NULL); - $form['jsonfind_element']= $cleanelement; - - $form['jsonfind_element']['#title'] = $this->t('Value to Search for in @elementkey JSON key', [ '@elementkey' => $chosen_element]); - $form['jsonreplace_element']['#title'] = $this->t('Value to replace with in @elementkey JSON key', [ '@elementkey' => $chosen_element]); - $form['jsonreplace_element']= $cleanelement; - $form['jsonreplace_element']['#name'] = 'jsonreplace_element'; - $form['jsonfind_element']['#name'] = 'jsonfind_element'; - $form['jsonfind_element']['#default_value'] = $form_state->getValue('jsonreplace_element', NULL); + $form['elements_rendered']['jsonfind_element_'.$chosen_element]['#title'] = $this->t('Value to Search for in @elementkey JSON key', [ '@elementkey' => $chosen_element]); + $form['elements_rendered']['jsonreplace_element_'.$chosen_element]= $cleanelement; + $form['elements_rendered']['jsonreplace_element_'.$chosen_element]['#title'] = $this->t('Value to replace with in @elementkey JSON key', [ '@elementkey' => $chosen_element]); } @@ -180,23 +171,15 @@ public function webformAjaxCallback(array $form, FormStateInterface $form_state) } public function webformElementAjaxCallback(array $form, FormStateInterface $form_state) { - $element['elements_rendered'] = [ - '#tree' => TRUE, - '#type' => 'fieldset', - '#prefix' => '
', - '#suffix' => '
', - ]; - $element['elements_rendered']['jsonfind_element'] = $form['jsonfind_element']; - $element['elements_rendered']['jsonreplace_element'] = $form['jsonreplace_element']; - return $element; + return $form['elements_rendered']; } public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { // Hacky but its the way we can do this dynamically - $jsonfind = $form_state->getValue('jsonfind_element', NULL) ?? ($form_state->getUserInput()['jsonfind_element'] ?? []); - $jsonreplace = $form_state->getValue('jsonreplace_element', NULL) ?? ($form_state->getUserInput()['jsonreplace_element'] ?? []); $chosen_element = $form_state->getValue(['webform_elements','elements_for_this_form'], NULL); if ($chosen_element) { + $jsonfind = $form_state->getValue(['elements_rendered','jsonfind_element_'.$chosen_element], NULL) ?? ($form_state->getUserInput()['jsonfind_element_'.$chosen_element] ?? []); + $jsonreplace = $form_state->getValue(['elements_rendered','jsonreplace_element_'.$chosen_element], NULL) ?? ($form_state->getUserInput()['jsonreplace_element_'.$chosen_element] ?? []); $jsonfind_ready[$chosen_element] = $jsonfind; $jsonreplace_ready[$chosen_element] = $jsonreplace; $this->configuration['jsonfind'] = json_encode($jsonfind_ready) ?? '{}'; @@ -257,11 +240,21 @@ public function execute($entity = NULL) { } } if (!$isAssociativeOriginal) { - foreach($fullvaluesmodified[$key] as &$item) { - if ($item == $decoded_jsonfind[$key]) { - // Exact Array to Array 1:1 match - $item = $decoded_jsonreplace[$key]; - $patched = TRUE; + // We have a few things to catch here + // Before trying to iterate over each member trying to replace a value + // We will check IF the $decoded_jsonfind[$key] is actually == $fullvaluesmodified[$key] + // e.g ismemberof: [] and decoded_jsonreplace[$key] == [] + if ($fullvaluesmodified[$key] == $decoded_jsonfind[$key]) { + $fullvaluesmodified[$key] = $decoded_jsonreplace[$key]; + $patched = TRUE; + } + else { + foreach ($fullvaluesmodified[$key] as &$item) { + if ($item == $decoded_jsonfind[$key]) { + // Exact Array to Array 1:1 match + $item = $decoded_jsonreplace[$key]; + $patched = TRUE; + } } } } @@ -443,6 +436,13 @@ public function getPluginDefinition() { return parent::getPluginDefinition(); // TODO: Change the autogenerated stub } + /** + * {@inheritdoc} + */ + public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { + return $object->access('update', $account, $return_as_object); + } + /** * {@inheritdoc} */ diff --git a/src/Plugin/Action/AmiStrawberryfieldJsonPatch.php b/src/Plugin/Action/AmiStrawberryfieldJsonPatch.php index 8771209..93b0271 100644 --- a/src/Plugin/Action/AmiStrawberryfieldJsonPatch.php +++ b/src/Plugin/Action/AmiStrawberryfieldJsonPatch.php @@ -128,5 +128,11 @@ public function getPluginDefinition() { return parent::getPluginDefinition(); // TODO: Change the autogenerated stub } + /** + * {@inheritdoc} + */ + public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { + return $object->access('update', $account, $return_as_object); + } } From 521f52ce78136f18eb514f61ac5aeb3023fd46c0 Mon Sep 17 00:00:00 2001 From: Diego Pino Navarro Date: Tue, 9 Aug 2022 23:59:56 -0400 Subject: [PATCH 2/3] Make Multivalued replace much better Much better logic for replacement for my Favorite nerds. - e.g if you pass now as "find" two Subjects and BOTH exist in the source (but other too) we will replace those two only...with whatever you pass. If you try to match e.g 3, all 3 match and your replacement is one, we will remove the 3 and push the new one at the end. I think @aksm @alliomeria @karomabiles this is the way. Will showcase if anyone wants to see this tomorrow (please smile) D --- .../AmiStrawberryfieldJsonAsWebform.php | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/Plugin/Action/AmiStrawberryfieldJsonAsWebform.php b/src/Plugin/Action/AmiStrawberryfieldJsonAsWebform.php index e523176..899c95d 100644 --- a/src/Plugin/Action/AmiStrawberryfieldJsonAsWebform.php +++ b/src/Plugin/Action/AmiStrawberryfieldJsonAsWebform.php @@ -249,6 +249,8 @@ public function execute($entity = NULL) { $patched = TRUE; } else { + // Now if original is indexed we do not know if the replacement is a value or also indexed + // So try first like original is indexed but each member is an array and compare... foreach ($fullvaluesmodified[$key] as &$item) { if ($item == $decoded_jsonfind[$key]) { // Exact Array to Array 1:1 match @@ -256,6 +258,38 @@ public function execute($entity = NULL) { $patched = TRUE; } } + // We are not going to do selective a few versus another? + // We can! + if (!$patched && is_array($decoded_jsonfind[$key]) && !StrawberryfieldJsonHelper::arrayIsMultiSimple($decoded_jsonfind[$key])) { + // Still we need to be sure ALL things to be searched for exist. A single difference means no patching + // So we traverse differently here + // Only if all needles are in the haystack + // we do the actual replacement. + $all_found = []; + $fullvaluesmodified_for_key_stashed = $fullvaluesmodified[$key]; + foreach ($decoded_jsonfind[$key] as $item) { + // And to be sure we make a STRICT comparison + $found = array_search($item, $fullvaluesmodified[$key], TRUE); + if ($found !== FALSE) { + // Since we do not know if the Match/search for are more items than the replacements we will delete the found and add any new ones at the end. + unset($fullvaluesmodified[$key][$found]); + $patched = TRUE; + } + else { + $patched = FALSE; + break; + } + } + // If after this we decide we could not patch + // We return the original value. + if (!$patched) { + $fullvaluesmodified[$key] = $fullvaluesmodified_for_key_stashed; + } + else { + // Here we merge the already stripped from the Match pattern source with the replacements + $fullvaluesmodified[$key] = array_values(array_merge($fullvaluesmodified[$key], $decoded_jsonreplace[$key])); + } + } } } else { From 10c48e96df7ad70880320580f6d409799e91b796 Mon Sep 17 00:00:00 2001 From: Diego Pino Navarro Date: Wed, 10 Aug 2022 13:07:51 -0400 Subject: [PATCH 3/3] Get elements Initialized - Seems like removing webform keys makes no change at all once we initialize. - Will also keep the access key. Because permissions? Some changes here related also to https://github.com/esmero/webform_strawberryfield/commit/afc0d2b0bacd2e605ac34010125b4892aab03884 --- src/Plugin/Action/AmiStrawberryfieldJsonAsWebform.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Plugin/Action/AmiStrawberryfieldJsonAsWebform.php b/src/Plugin/Action/AmiStrawberryfieldJsonAsWebform.php index 899c95d..4243aaa 100644 --- a/src/Plugin/Action/AmiStrawberryfieldJsonAsWebform.php +++ b/src/Plugin/Action/AmiStrawberryfieldJsonAsWebform.php @@ -139,14 +139,15 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta } if ($webform_entity && $chosen_element) { - $myelement = $webform_entity->getElementDecoded($chosen_element); + $myelement = $webform_entity->getElementInitialized($chosen_element); $libraries = $webform_entity->getSubmissionForm()['#attached']['library'] ?? []; $form['#attached']['library'] = ($form['#attached']['library'] ?? []) + $libraries; $cleanelement = []; foreach($myelement as $key => $value) { - if (strpos($key, '#webform') === FALSE && strpos($key, '#access_') === FALSE) { + /*if (strpos($key, '#webform4') === FALSE && strpos($key, '#access_') === FALSE) { $cleanelement[$key] = $value; - } + }*/ + $cleanelement[$key] = $value; } $cleanelement['#required'] = FALSE; $cleanelement['#validated'] = FALSE;