Skip to content

Commit

Permalink
refactor: replace with text block type
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Waldstein committed Jan 24, 2025
1 parent 2ce51ba commit 64ae975
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
20 changes: 11 additions & 9 deletions src/DonationForms/Actions/ConvertDonationFormBlocksToFieldsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Give\DonationForms\Actions;

use Exception;
use Give\DonationForms\Repositories\DonationFormRepository;
use Give\DonationForms\Rules\AuthenticationRule;
use Give\DonationForms\Rules\BillingAddressCityRule;
Expand All @@ -10,6 +11,7 @@
use Give\DonationForms\Rules\GatewayRule;
use Give\DonationForms\Rules\PhoneIntlInputRule;
use Give\FormBuilder\BlockModels\DonationAmountBlockModel;
use Give\FormBuilder\BlockTypes\TextBlockType;
use Give\Framework\Blocks\BlockCollection;
use Give\Framework\Blocks\BlockModel;
use Give\Framework\FieldsAPI\Authentication;
Expand Down Expand Up @@ -129,6 +131,7 @@ protected function convertInnerBlockToNode(BlockModel $block, int $blockIndex)
}

/**
* @unreleased updated to use TextBlockType
* @since 3.19.4 add max rule to company field
* @since 3.9.0 Add "givewp/donor-phone" block
* @since 3.0.0
Expand All @@ -137,6 +140,7 @@ protected function convertInnerBlockToNode(BlockModel $block, int $blockIndex)
* @throws NameCollisionException
*
* @throws EmptyNameException
* @throws Exception
*/
protected function createNodeFromBlockWithUniqueAttributes(BlockModel $block, int $blockIndex)
{
Expand Down Expand Up @@ -195,15 +199,13 @@ protected function createNodeFromBlockWithUniqueAttributes(BlockModel $block, in
case "givewp/company":
return Text::make('company')->rules('max:255');

case "givewp/text":
return Text::make(
$block->hasAttribute('fieldName') ?
$block->getAttribute('fieldName') :
$block->getShortName() . '-' . $blockIndex
)->storeAsDonorMeta(
$block->hasAttribute('storeAsDonorMeta') ? $block->getAttribute('storeAsDonorMeta') : false
)->description($block->getAttribute('description'))
->defaultValue($block->getAttribute('defaultValue'));
case TextBlockType::name():
$textBlockType = new TextBlockType($block);

return Text::make($textBlockType->getFieldName($blockIndex))
->storeAsDonorMeta($textBlockType->storeAsDonorMeta)
->description($textBlockType->description)
->defaultValue($textBlockType->defaultValue);

case "givewp/terms-and-conditions":
return $this->createNodeFromConsentBlock($block, $blockIndex)
Expand Down
8 changes: 8 additions & 0 deletions src/FormBuilder/BlockTypes/TextBlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ public static function name(): string
{
return 'givewp/text';
}

/**
* @unreleased
*/
public function getFieldName(int $blockIndex): string
{
return !empty($this->fieldName) ? $this->fieldName : $this->block->getShortName() . '-' . $blockIndex;
}
}
45 changes: 43 additions & 2 deletions tests/Unit/FormBuilder/BlockTypes/TestTextBlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Give\Tests\Unit\FormBuilder\BlockTypes;

use Exception;
use Give\FormBuilder\BlockTypes\TextBlockType;
use Give\Framework\Blocks\BlockModel;
use Give\Tests\TestCase;
use Give\Tests\TestTraits\RefreshDatabase;
Expand All @@ -26,7 +27,7 @@ public function testNameShouldMatch(): void
]
);

$blockType = new \Give\FormBuilder\BlockTypes\TextBlockType($blockModel);
$blockType = new TextBlockType($blockModel);

$this->assertSame('givewp/text', $blockType::name());
}
Expand Down Expand Up @@ -67,7 +68,7 @@ public function testAttributesShouldMatchProperties(): void
]
);

$blockType = new \Give\FormBuilder\BlockTypes\TextBlockType($blockModel);
$blockType = new TextBlockType($blockModel);

$this->assertSame('Test Label', $blockType->label);
$this->assertSame('Test Description', $blockType->description);
Expand All @@ -94,4 +95,44 @@ public function testAttributesShouldMatchProperties(): void
$blockType->conditionalLogic
);
}

/**
* @unreleased
* @throws Exception
*/
public function testGetFieldNameShouldReturnCustomName(): void
{
$blockModel = BlockModel::make(
[
'name' => 'givewp/text',
'attributes' => [
'fieldName' => 'custom_field_name',
]
]
);

$blockType = new TextBlockType($blockModel);

$this->assertSame('custom_field_name', $blockType->getFieldName(1));
}

/**
* @unreleased
* @throws Exception
*/
public function testGetFieldNameShouldReturnNameWithIndex()
{
$blockModel = BlockModel::make(
[
'name' => 'givewp/text',
'attributes' => [
'fieldName' => '',
]
]
);

$blockType = new TextBlockType($blockModel);

$this->assertSame('text-1', $blockType->getFieldName(1));
}
}

0 comments on commit 64ae975

Please sign in to comment.