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

[bug] Spoon button not added to slide out due to comparison type error #120

Closed
tomdavies opened this issue Oct 5, 2022 · 0 comments · Fixed by #121
Closed

[bug] Spoon button not added to slide out due to comparison type error #120

tomdavies opened this issue Oct 5, 2022 · 0 comments · Fixed by #121

Comments

@tomdavies
Copy link
Contributor

FYI @lindseydiloreto moved this here from our conversation in Craft Discord

I'm experiencing a bug on 4.0.0 whereby the Spoon button is not added to the field settings slideout on the field layout view, meaning that I can't configure per-field layout settings for Spoon.

I traced the issue to a bug in the JavaScript logic that decides whether to inject the Spoon button. Essentially the logic looks for a stringified ID value in an array of integer IDs, which always fails.

Fixing this in JS is super easy... The thing that's bothering me is why this is only happening for me, and apparently not for you/others. The only thing I could think of was difference PHP versions, though there's nothing relevant that I can see mentioned in the PHP 8.1 release notes.

Installed versions:

  • Craft 4.2.5.2
  • Spoon 4.0.0
  • PHP version 8.1.8
  • MySQL 10.3.35

Full debugging info below.

This is the current doublesecretagency\spoon\services\Field::getMatrixFieldIds()

public function getMatrixFieldIds(): array
{
    // If array of IDs is empty
    if (!$this->_matrixFieldIds) {
        // Populate array of IDs
        $this->_matrixFieldIds = (new Query())
            ->select(['id'])
            ->from('{{%fields}}')
            ->where('type = :type', [':type' => 'craft\fields\Matrix'])
            ->column();
    }

    // Return array of Matrix field IDs
    return $this->_matrixFieldIds;
}

The id col of the fields table is of type int, so this method should always be returning an array of ints already

Over in Configurator.js we have (inline comments added):

// Add "Spoon" button to slideout
handleCreateSettings: function(ev)
{
    var field = ev.target.$container[0];
    var id = $(field).data('id'); // <--- jQuery.data coerces this to an int for us

    // If no ID exists, bail
    if (!id) {
        return;
    }

    // If not a matrix field, bail
    // this check should always *fail* because we should be comparing a string against an array of ints
    if (this.settings.matrixFieldIds.indexOf(id.toString()) === -1) {
        return;
    }
    // ...
}

The only other place I can see this data getting transformed is here in the Loader service

 public function configurator(string $container, string $context): void
    {
        /// ...
        $settings = [
            'context' => $context,
            'matrixFieldIds' => Spoon::$plugin->fields->getMatrixFieldIds(),
            'iconMask' => $iconMask,
        ];

        $view->registerJs('window.configurator = new Spoon.Configurator("'.$container.'", '.Json::encode($settings, JSON_UNESCAPED_UNICODE).');');
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant