Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x]: craft\fieldlayoutelements\TextField does not save type attribute because it is overridden by TextField::class when saving field layout. #13988

Closed
xlientrc opened this issue Dec 2, 2023 · 2 comments
Assignees

Comments

@xlientrc
Copy link

xlientrc commented Dec 2, 2023

What happened?

Description

When saving a FieldLayout that has native fields defined like the example at https://craftcms.com/docs/4.x/extend/element-types.html#native-layout-elements The type attribute does not get saved because it gets overridden by the class name.

The below example with a number type

// Plugin
Event::on(
    FieldLayout::class,
    FieldLayout::EVENT_DEFINE_NATIVE_FIELDS,
    function(DefineFieldLayoutFieldsEvent $event) {

        $fieldLayout = $event->sender;

        switch ($fieldLayout->type) {
            case ResultElement::class:
                $event->fields[] = [
                    'label' => self::t('Priority'),
                    'class' => \craft\fieldlayoutelements\TextField::class,
                    'attribute' => 'priority',
                    'type' => 'number',
                    'step' => 1,
                    'min' => 0,
                ];
                break;
        }
    }
);

$service = $this->getResults();
$projectConfig->onAdd(Results::PROJECT_CONFIG_PATH . '.fieldLayout', [$service, 'handleChangedFieldLayout'])
    ->onUpdate(Results::PROJECT_CONFIG_PATH . '.fieldLayout', [$service, 'handleChangedFieldLayout'])
    ->onRemove(Results::PROJECT_CONFIG_PATH . '.fieldLayout', [$service, 'handleDeletedFieldLayout']);

// Controller
public function actionSave(): ?Response
{
    $this->requirePostRequest();

    $fieldLayout = Craft::$app->getFields()->assembleLayoutFromPost();

    if (!$fieldLayout->validate()) {
        Craft::info('Field layout not saved due to validation error.', __METHOD__);

        Craft::$app->getUrlManager()->setRouteParams([
            'fieldLayout' => $fieldLayout,
        ]);

        $this->setFailFlash(Search::t('Couldn’t save result fields.'));

        return null;
    }

    $currentFieldLayout = Craft::$app->getProjectConfig()->get(
        Results::PROJECT_CONFIG_PATH . '.fieldLayout'
    );

    if ($currentFieldLayout) {
        $uid = ArrayHelper::firstKey($currentFieldLayout);
    } else {
        $uid = StringHelper::UUID();
    }

    Craft::$app->getProjectConfig()->set(
        Results::PROJECT_CONFIG_PATH . '.fieldLayout',
        [$uid => $fieldLayout->getConfig()]
    );

    $this->setSuccessFlash(Search::t('Result fields saved.'));

    return $this->redirectToPostedUrl();
}

// Service
public function handleChangedFieldLayout(ConfigEvent $event): void
{
    $data = $event->newValue;

    ProjectConfigHelper::ensureAllFieldsProcessed();
    $fieldsService = Craft::$app->getFields();

    if (empty($data) || empty(reset($data))) {
        // Delete the field layout
        $fieldsService->deleteLayoutsByType(ResultElement::class);
        return;
    }

    // Save the field layout
    $layout = FieldLayout::createFromConfig(reset($data));

    $layout->id = $fieldsService->getLayoutByType(ResultElement::class)->id;
    $layout->type = ResultElement::class;
    $layout->uid = key($data);
    $fieldsService->saveLayout($layout, false);
}

Expected behavior

The field should display as type="number".

Actual behavior

The field displays as type="text"

Craft CMS version

4.5.11.1

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

@i-just
Copy link
Contributor

i-just commented Dec 6, 2023

Hi, thanks for reporting. I raised a PR to adjust this.

@brandonkelly
Copy link
Member

Craft 4.5.12 is out with a new PlainText::$inputType property, and $type is now deprecated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants