0

Im getting an expecting string given array error on a variable in my fieldtype. When output / dumped to the template it is a string. This only happens when used within matrix field.

The trim function here is the specific line throwing the error:

$settings['colours'] = explode(',', str_replace(' ', '', trim($settings['colours'])));

getInputHtml() & prepValue()

public function prepValue($value)
{
    $colour = new FruitStyle_PaletteModel();

    if(is_array($value))
    {
        $colour->hex = $value['hex'];
        $colour->opacity = $value['opacity'];
    }

    return $colour;
}

public function getInputHtml($name, $value)
{
    craft()->templates->includeJsResource('fruitstyle/js/FruitPalette.js');
    craft()->templates->includeCssResource('fruitstyle/css/palette.css');
    craft()->templates->includeCssResource('fruitstyle/fonts/fruiticons/style.css');

    $id = craft()->templates->formatInputId($name);
    craft()->templates->includeJs('new FruitPalette("'.craft()->templates->namespaceInputId($id).'");');

    // String or JSON
    $settings = $this->getSettings();
    if(filter_var($settings['colours'], FILTER_VALIDATE_URL))
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $settings['colours']);
        $colours = curl_exec($ch);
        curl_close($ch);
        $settings['colours'] = JsonHelper::decode($colours, true);
    }
    else
    {
        $settings['colours'] = explode(',', str_replace(' ', '', trim($settings['colours'])));
    }

    return craft()->templates->render('fruitstyle/_fieldtype/palette/input', array(
        'id' => $id,
        'name' => $name,
        'value' => $value,
        'settings' => $settings,
        'specialOptions' => $this->_getSpecialOptions(),
    ));
}

PHP Error

1 Answer 1

2

It appears as though $settings['colours'] is an array... which you're then trying to trim and explode.

Can you var_dump($settings['colours'])?

p.s. Nice to see "colours" spelt "correctly" :)

5
  • Is this a plugin that you've developed? Is it maybe saving the colours as a list of colours or RGB values?
    – Rick
    Commented Aug 25, 2015 at 2:17
  • I voted you up in spite of your gleeful misspelling of the word "colors". :)
    – Brad Bell
    Commented Aug 25, 2015 at 2:46
  • Gents, this is the confusing bit that I was trying to explain (badly) when var_dump or dump() in the template its says that it's a string!! Baffled. Commented Aug 25, 2015 at 7:38
  • @BradBell he spelled it perfectly fine. Commented Aug 25, 2015 at 13:34
  • So if you insert a line before line 86 of FruitStyle_PaletteFieldType.php and add var_dump($settings["colours"]); ... does it report a string?
    – Rick
    Commented Aug 25, 2015 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.