Skip to content

[5.x]: Illegal offset type getPreviewHtml country field #15583

@jeroenlammerts

Description

@jeroenlammerts

What happened?

Description

When having a country field added to the sources of the index page of an entry type you get the error Illegal offset type

Capture 2024-08-23 at 12 12 22

Steps to reproduce

  1. Create a country field
  2. Add the field to an entry type
  3. Add the country field to the index page of the entry

Expected behavior

Not throwing an error and show a preview of the field.

Actual behavior

Throwing the error Illegal offset type

A possible fix could be

craftcms/cms/src/fields/Country.php

    public function getPreviewHtml(mixed $value, ElementInterface $element): string
    {
        if (!$value || !$value instanceof CountryModel) {
            return '';
        }

        $list = Craft::$app->getAddresses()->getCountryRepository()->getList(Craft::$app->language);
        return $list[$value->getCountryCode()] ?? $value;
    }

Craft CMS version

5.3.4

PHP version

8.3

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions