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

Programmatically uploading assets as base64 doesn't work #2855

Closed
boboldehampsink opened this issue May 8, 2018 · 0 comments
Closed

Programmatically uploading assets as base64 doesn't work #2855

boboldehampsink opened this issue May 8, 2018 · 0 comments
Assignees

Comments

@boboldehampsink
Copy link
Contributor

Description

According to my understanding of the code, adding an asset programmatically via base64 is supposed to go like this:

        'headerImage' => [
            'filenames' => [
                'home-header-image.jpg',
            ],
            'data' => [
                'data:image/jpeg;base64,'.base64_encode(file_get_contents('src/img/temp/home-header-image.jpg')),
            ],
        ],

However, I get the following exception:

Exception 'TypeError' with message 'Argument 1 passed to craft\helpers\StringHelper::toLowerCase() must be of the type string, array given, called in /app/user/vendor/craftcms/cms/src/helpers/Db.php on line 448'

It seems to me that this feature is not fully/correctly implemented. Here is the craft\fields\Asset::normalizeValue source:

    public function normalizeValue($value, ElementInterface $element = null)
    {
        // If data strings are passed along, make sure the array keys are retained.
        if (isset($value['data']) && !empty($value['data'])) {
            /** @var Asset $class */
            $class = static::elementType();
            /** @var ElementQuery $query */
            $query = $class::find()
                ->siteId($this->targetSiteId($element));

            // $value might be an array of element IDs
            if (is_array($value)) {
                $query
                    ->id(array_filter($value))
                    ->fixedOrder();

                if ($this->allowLimit === true && $this->limit) {
                    $query->limit($this->limit);
                } else {
                    $query->limit(null);
                }

                return $query;
            }
        }

        return parent::normalizeValue($value, $element);
    }

As you can see it checks if the data key is present, but that's about it. The query code doesn't make sense to me in relation to the data key values.

Steps to reproduce

  1. Create a content migration where you add an image inline, as shown in the example above.

Additional info

  • Craft version: Pro 3.0.5
  • PHP version: 7.2.5
  • Database driver & version: PostgreSQL 10.3 (Debian 10.3)
  • Plugins & versions: Amazon S3 (1.0.8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants