Skip to content
Snippets Groups Projects
Commit 95102016 authored by colemanw's avatar colemanw
Browse files

Afform - Do not set LocBlock id if exposed to the form

parent 66c05284
Branches
Tags
No related merge requests found
......@@ -460,8 +460,8 @@ class Submit extends AbstractProcessor {
// Forward FK e.g. Event.loc_block_id => LocBlock
$forwardFkField = self::getFkField($mainEntity['type'], $joinEntityName);
if ($forwardFkField && $values) {
// Add id to values for update op
if ($whereClause) {
// Add id to values for update op, but only if id is not already on the form
if ($whereClause && empty($mainEntity['joins'][$joinEntityName]['fields'][$joinIdField])) {
$values[0][$joinIdField] = $whereClause[0][2];
}
$result = civicrm_api4($joinEntityName, 'save', [
......
......@@ -2,13 +2,14 @@
namespace api\v4\Afform;
use Civi\Api4\Afform;
use Civi\Test\TransactionalInterface;
/**
* Test case for Afform.prefill and Afform.submit with Event records.
*
* @group headless
*/
class AfformEventUsageTest extends AfformUsageTestCase {
class AfformEventUsageTest extends AfformUsageTestCase implements TransactionalInterface {
use \Civi\Test\Api4TestTrait;
/**
......@@ -75,4 +76,110 @@ EOHTML;
$this->assertSame('2234567', $prefill['values'][0]['joins']['LocBlock'][0]['phone_id.phone']);
}
/**
* Test saving & updating
*/
public function testEventLocationUpdate(): void {
$layout = <<<EOHTML
<af-form ctrl="afform">
<af-entity actions="{create: true, update: true}" type="Event" name="Event1" label="Event 1" security="FBAC" />
<fieldset af-fieldset="Event1" class="af-container" af-title="Event 1">
<af-field name="id" />
<af-field name="title" />
<af-field name="start_date" />
<af-field name="event_type_id" />
<div af-join="LocBlock">
<af-field name="id" />
<af-field name="email_id.email" />
<af-field name="address_id.street_address" />
</div>
</fieldset>
<button class="af-button btn btn-primary" crm-icon="fa-check" ng-click="afform.submit()">Submit</button>
</af-form>
EOHTML;
$this->useValues([
'layout' => $layout,
'permission' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
]);
// Create a new event with a new location
$submit = Afform::submit()
->setName($this->formName)
->setValues([
'Event1' => [
[
'fields' => [
'title' => 'Event Title 1',
'start_date' => '2021-01-01 00:00:00',
'event_type_id' => 1,
],
'joins' => [
'LocBlock' => [
[
'email_id.email' => 'test1@te.st',
'address_id.street_address' => '12345',
],
],
],
],
],
])->execute();
$event1 = $submit[0]['Event1'][0]['id'];
$loc1 = $submit[0]['Event1'][0]['joins']['LocBlock'][0]['id'];
// Create a 2nd new event with a new location
$submit = Afform::submit()
->setName($this->formName)
->setValues([
'Event1' => [
[
'fields' => [
'title' => 'Event Title 2',
'start_date' => '2022-01-01 00:00:00',
'event_type_id' => 1,
],
'joins' => [
'LocBlock' => [
[
'id' => $loc1,
],
],
],
],
],
])->execute();
$event2 = $submit[0]['Event1'][0]['id'];
$this->assertGreaterThan($event1, $event2);
$this->assertSame($loc1, $submit[0]['Event1'][0]['joins']['LocBlock'][0]['id']);
// Update event 1 with a new location
$submit = Afform::submit()
->setName($this->formName)
->setValues([
'Event1' => [
[
'fields' => [
'id' => $event1,
'title' => 'Event Title 1 Updated',
],
'joins' => [
'LocBlock' => [
[
'id' => NULL,
'address_id.street_address' => '12345',
],
],
],
],
],
])->execute();
$this->assertSame($event1, $submit[0]['Event1'][0]['id']);
$this->assertGreaterThan($loc1, $submit[0]['Event1'][0]['joins']['LocBlock'][0]['id']);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment