Skip to content

Commit

Permalink
[FEATURE] Field type "Pass"
Browse files Browse the repository at this point in the history
  • Loading branch information
nhovratov committed Aug 16, 2024
1 parent a1aedf0 commit 065409a
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Classes/Definition/Factory/ContentBlockCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ private function processFields(ProcessingInput $input, ProcessedFieldsResult $re
$fieldTypeName = $result->fieldType::getName();
$fieldTypeEnum = FieldType::tryFrom($fieldTypeName);
$input->languagePath->addPathSegment($result->identifier);
$field = $this->initializeFieldLabelAndDescription($input, $result, $field);
if ($fieldTypeEnum !== FieldType::PASS) {
$field = $this->initializeFieldLabelAndDescription($input, $result, $field);
}
if ($fieldTypeEnum === FieldType::FLEXFORM) {
$field = $this->processFlexForm($input, $field);
}
Expand Down Expand Up @@ -410,6 +412,7 @@ private function handleRootField(array $rootField, ProcessingInput $input, Proce
$fields = match ($fieldTypeEnum) {
Fieldtype::PALETTE => $this->handlePalette($input, $result, $rootField),
FieldType::TAB => $this->handleTab($input, $result, $rootField),
FieldType::PASS => [$rootField],
default => $this->handleDefault($input, $result, $rootField)
};
return $fields;
Expand Down
4 changes: 4 additions & 0 deletions Classes/FieldType/FieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ enum FieldType: string
case LINEBREAK = 'Linebreak';
case TAB = 'Tab';
case UUID = 'Uuid';
case PASS = 'Pass';

public static function isValidFlexFormField(FieldTypeInterface $fieldType): bool
{
$fieldTypeEnum = FieldType::tryFrom($fieldType::getName());
if ($fieldTypeEnum?->isStructureField()) {
return false;
}
if ($fieldTypeEnum === FieldType::PASS) {
return false;
}
return $fieldTypeEnum !== self::FLEXFORM;
}

Expand Down
63 changes: 63 additions & 0 deletions Classes/FieldType/PassFieldType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace TYPO3\CMS\ContentBlocks\FieldType;

/**
* @internal Not part of TYPO3's public API.
*/
final class PassFieldType implements FieldTypeInterface
{
private mixed $default = '';

public static function getName(): string
{
return 'Pass';
}

public static function getTcaType(): string
{
return 'passthrough';
}

public static function isSearchable(): bool
{
return false;
}

public static function createFromArray(array $settings): PassFieldType
{
$self = new self();
$self->default = $settings['default'] ?? $self->default;
return $self;
}

public function getTca(): array
{
$config['type'] = self::getTcaType();
if ($this->default !== '') {
$config['default'] = $this->default;
}
$tca['config'] = $config;
return $tca;
}

public function getSql(string $column): string
{
return "`$column` VARCHAR(255) DEFAULT '' NOT NULL";
}
}
7 changes: 5 additions & 2 deletions Classes/Generator/TcaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,11 @@ protected function getOptionKey(string|array $option, TcaFieldDefinition $tcaFie
*/
protected function determineLabelAndDescription(ContentTypeInterface $typeDefinition, TcaFieldDefinition $overrideColumn, array $column): array
{
$fieldType = $overrideColumn->getFieldType();
$tcaFieldType = $fieldType::getTcaType();
if ($tcaFieldType === 'passthrough') {
return $column;
}
$name = $typeDefinition->getName();
$labelPath = $overrideColumn->getLabelPath();
if ($this->languageFileRegistry->isset($name, $labelPath)) {
Expand All @@ -567,9 +572,7 @@ protected function determineLabelAndDescription(ContentTypeInterface $typeDefini
if ($this->languageFileRegistry->isset($name, $descriptionPath)) {
$column['description'] = $descriptionPath;
}
$fieldType = $overrideColumn->getFieldType();
$itemsFieldTypes = ['select', 'radio', 'check'];
$tcaFieldType = $fieldType::getTcaType();
if (in_array($tcaFieldType, $itemsFieldTypes, true)) {
$items = $column['config']['items'] ?? [];
foreach ($items as $index => $item) {
Expand Down
2 changes: 2 additions & 0 deletions Documentation/YamlReference/FieldTypes/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ some additional options, which are not available in TCA to ease the usage.
* :ref:`Link <field_type_link>`
* :ref:`Number <field_type_number>`
* :ref:`Palette <field_type_palette>`
* :ref:`Pass <field_type_pass>`
* :ref:`Password <field_type_password>`
* :ref:`Radio <field_type_radio>`
* :ref:`Relation <field_type_relation>`
Expand Down Expand Up @@ -227,6 +228,7 @@ Field options, which can be defined inside the :yaml:`fields` array.
Link/Index
Number/Index
Palette/Index
Pass/Index
Password/Index
Radio/Index
Relation/Index
Expand Down
40 changes: 40 additions & 0 deletions Documentation/YamlReference/FieldTypes/Pass/Index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.. include:: /Includes.rst.txt
.. _field_type_pass:

====
Pass
====

:php:`type => 'passthrough' // TCA`

The :yaml:`Pass` type provides a virtual field, which is not visible in the
backend. It is useful for extension logic handling this value independently.

Settings
========

.. confval:: default
:name: pass-default

:Required: false
:Type: mixed
:Default: ''

Default value set if a new record is created.

.. note::

This does not work right now in some circumstances. See issue: https://forge.typo3.org/issues/104646

Example
=======

Minimal
-------

.. code-block:: yaml
name: example/pass
fields:
- identifier: pass
type: Pass
62 changes: 62 additions & 0 deletions Tests/Unit/FieldTypes/PassFieldTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace TYPO3\CMS\ContentBlocks\Tests\Unit\FieldTypes;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\ContentBlocks\FieldType\PassFieldType;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

final class PassFieldTypeTest extends UnitTestCase
{
public static function getTcaReturnsExpectedTcaDataProvider(): iterable
{
yield 'truthy values' => [
'config' => [
'default' => 'foo',
],
'expectedTca' => [
'config' => [
'type' => 'passthrough',
'default' => 'foo',
],
],
];

yield 'falsy values' => [
'config' => [
'default' => 0,
],
'expectedTca' => [
'config' => [
'type' => 'passthrough',
'default' => 0,
],
],
];
}

#[DataProvider('getTcaReturnsExpectedTcaDataProvider')]
#[Test]
public function getTcaReturnsExpectedTca(array $config, array $expectedTca): void
{
$fieldConfiguration = PassFieldType::createFromArray($config);

self::assertSame($expectedTca, $fieldConfiguration->getTca());
}
}
2 changes: 2 additions & 0 deletions Tests/Unit/Fixtures/FieldTypeRegistryTestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use TYPO3\CMS\ContentBlocks\FieldType\LinkFieldType;
use TYPO3\CMS\ContentBlocks\FieldType\NumberFieldType;
use TYPO3\CMS\ContentBlocks\FieldType\PaletteFieldType;
use TYPO3\CMS\ContentBlocks\FieldType\PassFieldType;
use TYPO3\CMS\ContentBlocks\FieldType\PasswordFieldType;
use TYPO3\CMS\ContentBlocks\FieldType\RadioFieldType;
use TYPO3\CMS\ContentBlocks\FieldType\RelationFieldType;
Expand Down Expand Up @@ -74,6 +75,7 @@ public static function create(): FieldTypeRegistry
new TextareaFieldType(),
new TextFieldType(),
new UuidFieldType(),
new PassFieldType(),
];
$keyedFieldTypes = [];
foreach ($fieldTypes as $fieldType) {
Expand Down
13 changes: 13 additions & 0 deletions Tests/Unit/Generator/TcaGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ public static function checkTcaFieldTypesDataProvider(): iterable
'identifier' => 'uuid',
'type' => 'Uuid',
],
[
'identifier' => 'pass',
'type' => 'Pass',
],
[
'identifier' => 'collection',
'type' => 'Collection',
Expand Down Expand Up @@ -409,6 +413,9 @@ public static function checkTcaFieldTypesDataProvider(): iterable
'description' => 'LLL:EXT:foo/ContentBlocks/example/Source/Language/Labels.xlf:uuid.description',
'config' => [],
],
't3ce_example_pass' => [
'config' => [],
],
],
],
't3ce_testblock' => [
Expand Down Expand Up @@ -557,6 +564,12 @@ public static function checkTcaFieldTypesDataProvider(): iterable
],
'exclude' => true,
],
't3ce_example_pass' => [
'label' => 'pass',
'config' => [
'type' => 'passthrough',
],
],
],
'palettes' => [
't3ce_example_palette_1' => [
Expand Down

0 comments on commit 065409a

Please sign in to comment.