Skip to content

Commit

Permalink
Merge pull request #119 from esmero/ISSUE-118
Browse files Browse the repository at this point in the history
ISSUE-118: I might have solved forever the problems with Webform based search and replace
  • Loading branch information
DiegoPino authored Aug 10, 2022
2 parents 119d322 + 10c48e9 commit d802a87
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 34 deletions.
1 change: 0 additions & 1 deletion src/Plugin/Action/AmiStrawberryfieldCSVexport.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,5 +575,4 @@ public function setConfiguration(array $configuration) {
); // TODO: Change the autogenerated stub
}


}
7 changes: 7 additions & 0 deletions src/Plugin/Action/AmiStrawberryfieldJsonAsText.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


}
101 changes: 68 additions & 33 deletions src/Plugin/Action/AmiStrawberryfieldJsonAsWebform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -85,7 +84,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
];
$form['elements_rendered'] = [
'#tree' => TRUE,
'#type' => 'fieldset',
'#type' => 'container',
'#prefix' => '<div id="webform-elements-render-wrapper">',
'#suffix' => '</div>',
];
Expand All @@ -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');
Expand Down Expand Up @@ -141,29 +139,23 @@ 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);
$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;
$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 <em>@elementkey</em> JSON key', [ '@elementkey' => $chosen_element]);
$form['jsonreplace_element']['#title'] = $this->t('Value to replace with in <em>@elementkey</em> 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 <em>@elementkey</em> 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 <em>@elementkey</em> JSON key', [ '@elementkey' => $chosen_element]);
}


Expand All @@ -180,23 +172,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' => '<div id="webform-elements-render-wrapper">',
'#suffix' => '</div>',
];
$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) ?? '{}';
Expand Down Expand Up @@ -257,11 +241,55 @@ 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 {
// 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
$item = $decoded_jsonreplace[$key];
$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]));
}
}
}
}
Expand Down Expand Up @@ -443,6 +471,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}
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Plugin/Action/AmiStrawberryfieldJsonPatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit d802a87

Please sign in to comment.