Get array from fields and check if

Hi,
Always working on my calendar, I’m back to ask you a very particular thing and I’m a bite lost.

My goal is to get different background color from the user choice for each month.
Already now, I can get month name by <?php echo $month->name() ?>
I inserted color fields into site parameters for each months, so 12 fields color.

  mars:
    label: Mars
    type:  color
  avril:
    label: Avril
    type:  color
  mai:
    label: Mai
    type:  color

I don’t know how to : check if the month correspond to the field month name, and get the color to display it.

You can assign the name of the month to a variable and use it to call the color field:

$month = $month->name();
$color = $page->$month();

i assume you use the color picker plugin.
to get the label from the blueprint you could use architect plugin

$currentMonth = str::lower('avril'); // you set this somehow
$month = $page->$currentMonth();
if($month && $month->isNotEmpty()) {
     // color
    echo $month; // or $month->value() 
     // name of $field object
    echo $month->name();
    // label of current month field
    echo Architect::field_label($page->template(), $month->name());
}

I think the Architect plugin would be overkill, as the method @pedroborges suggested is absolutely sufficient. No need to get the label.

I agree that Architect plugin seems to be too complicate for my simple problem.

As I tried to say, the $month is already an array from calendar plugin wich return the month’s name

I have these horrible 12 fields, and I want something like this
$colormonth = array("janvier" => "#5c104a", "février" => "#874646", (etc....) );
It could be the first step to check before
if one key of $colormonth is equal to $month, get $colormonth corresponding value

Why do you need the array, if you can get the value directly from the page value as suggested by @pedroborges?

You could, however, use the following code to create your array:

<?php
// you would set the locale in your config, this is just for testing
setlocale(LC_TIME, "fr_FR");
$colormonth = array();
for ($m=1; $m<=12; $m++) {
  $month = strftime ( '%B',  mktime(0,0,0,$m));
  $colormonth[$month] = $page->$month()->value();
}

But as I said above, I don’t think this is necessary.

Ok thank you very much, I didn’t know that we could get value directly like that.

$monthC = $month->name();
$color = $site->$monthC();
echo $color

and <table style="background:<?php echo $color ?>">

Works great but not for “Février”, “Août”, and “Décembre”, due to the special characters. (I’m already in setlocale(LC_TIME, "fr_FR") )
I tried, for “Août” to change my blueprint to

août:  <----- here
    label: Août
    type:  color
    width: 1/4

First time I see that : when I want to change the color and save, it doens’t take in consideration the value. Can we put special characters into blueprint field value ?

Try

$monthC = str::ascii($month->name());

You solved my problem, working now. Thank you and have a nice day :slight_smile:

1 Like