Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterSwatch::getShape(): Return value must be of type array|string, Drupal\Core\StringTranslation\TranslatableMarkup

Created on 12 September 2022, over 2 years ago
Updated 19 January 2023, over 2 years ago

Problem/Motivation

Getting: Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterSwatch::getShape(): Return value must be of type array|string, Drupal\Core\StringTranslation\TranslatableMarkup

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Needs work

Version

3.0

Component

Code

Created by

heddn Nicaragua

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¨πŸ‡¦Canada liquidcms

    If 3.x requires PHP 8.x then it should have a requirement in its composer.json file stating this.

    Drupal requirements have been messed up as of late as far as PHP requirements. If you look at D9.4 status report it states 8.x is a requirement; but it is not.

  • πŸ‡ΊπŸ‡ΈUnited States pianomansam

    I just ran into this PHP 7 incompatibility. What can be done to address this?

  • πŸ‡§πŸ‡ͺBelgium rp7

    I also just ran into this PHP 7 incompatibility. This requirement should definitely be in the composer.json file.

  • πŸ‡«πŸ‡·France sashainparis Provins

    "Make it clear" means to me: in composer.json :-)

    Regards,

  • Status changed to Fixed 2 months ago
  • πŸ‡¨πŸ‡¦Canada nickdickinsonwilde Victoria, BC (T'So-uke lands)

    Setting back to fixed.
    At time of release PHP 7.4 was past end of life.
    definitely would have been better to include that in the dependencies but should be moot point by now.

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • πŸ‡¬πŸ‡§United Kingdom therobyouknow

    how does patch #2 fix the issue exactly?

    Looking at the patch itself, it is changing one of the allowed return types from string to MarkupInterface on function protected function getShape:

    -  protected function getShape(?string $shape = NULL): array|string {
    +  protected function getShape(?string $shape = NULL): array|MarkupInterface {

    This example is the concept of multiple return types / union, i.e. specifying what multiple return types of the value returned are allowed. In other words, as a programmer, you can specify more than one return type for the value that your function returns. In this case of this module code, the types that could be returned were array and MarkupInterface.

    According to https://stackoverflow.com/questions/37033142/is-it-possible-to-specify-m... this was introduced since PHP 8 onwards so that means the direct version before that, which was 7.4, won't support it.

    I don't really know what the patch does to address this lack of support for multiple return types in php 7.4, because the patch just changes one of the types from string to MarkupInterface - there are still multiple return types defined - which won't work on php 7.4 according to th e above stackoverflow link.

    Also unsure what version the patch should be applied on. If it was 3.0.x-dev (as in the version number field of the issue) then the code content in 3.0.x-dev 2 years ago would be different to 3.0.x-dev now, is that right? That's my assumption that a dev version 3.0.x-dev is not fixed to a particular git commit like other release stabilities are. The patch failed to apply for me. I would have preferred 3.0.0 to be specified as to the version that the patch could be applied onto.

    So being unsuccessful making use of the patch, I just removed the multiple return types in web/modules/contrib/color_field/src/Plugin/Field/FieldFormatter/ColorFieldFormatterSwatch.php and web/modules/contrib/color_field/src/Plugin/Field/FieldFormatter/ColorFieldFormatterText.php which seems to have got me past that error so seems to have worked, e.g.

    -  protected function getColorFormat(?string $format = NULL): array|MarkupInterface {
    +  protected function getColorFormat(?string $format = NULL) {

    But really I should be using php 8.1 but it's beyond my control at the moment.

  • πŸ‡¬πŸ‡§United Kingdom therobyouknow

    I've provided my own patch `color_field-php74-compatibility-3309269.patch` to remove the multi-type returns that are supported from PHP 8 onwards, i.e. not in PHP 7.4.

    Doing so doesn't change the functionality. All it does is remove the strictness as to what types the body of code within those functions can return. The multi-type returns are a good thing to have as they help the good practises of type safety and defensive programming. But because they aren't supported in PHP 7.4 that's why I've removed them.

    The patch provided by one of the maintainers (whom I am most grateful for their work in this module and that reminds me a bit of Stephen Merchant ;) ), https://www.drupal.org/project/color_field/issues/3309269#comment-14690125 πŸ› Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterSwatch::getShape(): Return value must be of type array|string, Drupal\Core\StringTranslation\TranslatableMarkup Needs work didn't work for me running PHP 7.4 , because it doesn't remove the multiple return type declaration, it just changes one of the types.

    Helping others use my patch:

    I put my patch in a folder called patches in my top level project root (i.e. where composer.json is located)

    This is my composer.json file which contains lines to apply my patch:

    {
        "name": "drupal/legacy-project",
        "description": "Project template for Drupal 9 projects with composer following drupal/drupal layout",
        "type": "project",
        "license": "GPL-2.0-or-later",
        "homepage": "https://www.drupal.org/project/drupal",
        "support": {
            "docs": "https://www.drupal.org/docs/user_guide/en/index.html",
            "chat": "https://www.drupal.org/node/314178"
        },
        "repositories": [
            {
                "type": "composer",
                "url": "https://packages.drupal.org/8"
            }
        ],
        "require": {
            "composer/installers": "^1.9",
            "cweagans/composer-patches": "^1.7",
            "drupal/classy_paragraphs": "^2.0",
            "drupal/color_field": "^3.0",
            "drupal/core-composer-scaffold": "^9.4",
            "drupal/core-project-message": "^9.4",
            "drupal/core-recommended": "^9.4",
            "drupal/core-vendor-hardening": "^9.4",
            "drupal/entity_usage": "^2.0@beta",
            "drupal/fontawesome": "^2.26",
            "drupal/paragraphs": "^1.17",
            "drupal/structure_sync": "^2.0",
            "drupal/viewsreference": "^1.8",
            "drush/drush": "^11.6"
        },
        "conflict": {
            "drupal/drupal": "*"
        },
        "minimum-stability": "dev",
        "prefer-stable": true,
        "config": {
            "allow-plugins": {
                "composer/installers": true,
                "cweagans/composer-patches": true,
                "dealerdirect/phpcodesniffer-composer-installer": true,
                "drupal/core-composer-scaffold": true,
                "drupal/core-project-message": true,
                "drupal/core-vendor-hardening": true
            },
            "sort-packages": true
        },
        "extra": {
            "drupal-scaffold": {
                "locations": {
                    "web-root": "web/"
                }
            },
            "installer-paths": {
                "web/core": [
                    "type:drupal-core"
                ],
                "web/libraries/{$name}": [
                    "type:drupal-library"
                ],
                "web/modules/contrib/{$name}": [
                    "type:drupal-module"
                ],
                "web/profiles/contrib/{$name}": [
                    "type:drupal-profile"
                ],
                "web/themes/contrib/{$name}": [
                    "type:drupal-theme"
                ],
                "drush/Commands/contrib/{$name}": [
                    "type:drupal-drush"
                ],
                "web/modules/custom/{$name}": [
                    "type:drupal-custom-module"
                ],
                "web/profiles/custom/{$name}": [
                    "type:drupal-custom-profile"
                ],
                "web/themes/custom/{$name}": [
                    "type:drupal-custom-theme"
                ]
            },
            "drupal-core-project-message": {
                "include-keys": [
                    "homepage",
                    "support"
                ],
                "post-create-project-cmd-message": [
                    "<bg=blue;fg=white>                                                         </>",
                    "<bg=blue;fg=white>  Congratulations, you’ve installed the Drupal codebase  </>",
                    "<bg=blue;fg=white>  from the drupal/legacy-project template!               </>",
                    "<bg=blue;fg=white>                                                         </>",
                    "",
                    "<bg=yellow;fg=black>Next steps</>:",
                    "  * Install the site: https://www.drupal.org/docs/8/install",
                    "  * Read the user guide: https://www.drupal.org/docs/user_guide/en/index.html",
                    "  * Get support: https://www.drupal.org/support",
                    "  * Get involved with the Drupal community:",
                    "      https://www.drupal.org/getting-involved",
                    "  * Remove the plugin that prints this message:",
                    "      composer remove drupal/core-project-message"
                ]
            },
            "patches": {
                "drupal/color_field": {
                    "3309269 patch to get color_field working with php 7.4": "patches/color_field-php74-compatibility-3309269.patch"
                }
            }
        }
    }
    

    How I created the patch:

    Lovely simple guide, https://drupal.stackexchange.com/a/287849/1082 - drupal.org didn't have such an easy guide to create patches at the time, maybe it now does; lots of poncing and faffing about in the documentation, perhaps those in the know with their unicorns stickers and DrupalCon stickers on their macs had insider knowledge lol.

    Anyway I hope not to be on PHP 7.4 for long, not ideal at all. But hope this helps others who are in the same situation.

  • πŸ‡¬πŸ‡§United Kingdom therobyouknow
  • πŸ‡¬πŸ‡§United Kingdom therobyouknow

    Updated patch, same as previous but removes multi-type return in another function.

Production build 0.71.5 2024