Undefined index: key

I’m setting background colour using color palette but the issue I’m having is that is causes an error when the key value isn’t selected in the panel - I thought this should be combated by “<?php if($page->palette()->isNotEmpty()): ?>” or setting the blueprint default but neither fix the error

<body data-theme="<?php if($page->palette()->isNotEmpty()): ?><?php $palette = $page->palette()->yaml(); echo $palette['key'] ?><?php else: ?>light<?php endif ?>">

Blueprint:

palette:
    type: color-palette
    label: Background Colour
    width: 1/2
    default: light
    display: single
    size: large
    help: Page Colour Scheme
    options:
      light:
        background: '#E5E5E5'
      grey:
        background: '#75787B'
      dark:
        background: '#333133'
    default: 'light'

What sort of field is that? A select field? If so, using yaml doesn’t make sense. You are trying to convert this field to an array using yaml, but there is no index “key”.

Hey, it’s a color-palette field using the color-palette plugin!

Ah, ok, and what do you get when you

dump($page->palette()->yaml());

On a side note: It’s not a good idea to put all this php code into the data-theme attribute. Store the result of your code in a variable, then echo that out into the attribute:

<?php
$palette = $page->palette()->yaml(); 
$color = $palette['key'] ?? 'light';
?>

<body data-theme="<?php echo $color ?>">
1 Like

Hi texnixe, thank you

this is the result from dump($page->palette()->yaml());

Array
(
    [background] => #333133
    [key] => dark
)

and when no key has been chosen in the panel

Array
(
    [background] => #E5E5E5
)

That’s what I thought, with the code I posted above you avoid the pitfall that key does not exist.

1 Like

Thank you so much for your help :slight_smile:

1 Like