Created
November 11, 2022 12:04
-
-
Save gglnx/33b74794af4462f51a55532c71bb3071 to your computer and use it in GitHub Desktop.
Field with enum as value type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* German state | |
*/ | |
enum GermanState: string | |
{ | |
case BW = 'Baden-Württemberg'; | |
case BY = 'Bayern'; | |
case BE = 'Berlin'; | |
case BB = 'Brandenburg'; | |
case HB = 'Bremen'; | |
case HH = 'Hamburg'; | |
case HE = 'Hessen'; | |
case MV = 'Mecklenburg-Vorpommern'; | |
case NI = 'Niedersachsen'; | |
case NW = 'Nordrhein-Westfalen'; | |
case RP = 'Rheinland-Pfalz'; | |
case SL = 'Saarland'; | |
case SN = 'Sachsen'; | |
case ST = 'Sachsen-Anhalt'; | |
case SH = 'Schleswig-Holstein'; | |
case TH = 'Thüringen'; | |
/** | |
* Gets state by it's ISO code | |
* | |
* @param string $isoCode | |
* @return self|null | |
*/ | |
public static function tryFromIsoCode(string $isoCode): ?self | |
{ | |
$enumFqn = sprintf('%s::%s', self::class, strtoupper($isoCode)); | |
return defined($enumFqn) ? constant($enumFqn) : null; | |
} | |
/** | |
* Return all states as array | |
* | |
* @return string[] | |
* @phpstan-return value-of<self>[] | |
*/ | |
public static function all(): array | |
{ | |
return array_map( | |
fn (self $case) => $case->value, | |
self::cases(), | |
); | |
} | |
/** | |
* Return all states as options array | |
* | |
* @return array<int, array{value: string, label: value-of<self>}> | |
*/ | |
public static function options(): array | |
{ | |
return array_map( | |
fn (self $case) => [ | |
'value' => $case->name, | |
'label' => $case->value, | |
], | |
self::cases(), | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Craft; | |
use craft\base\ElementInterface; | |
use craft\base\Field; | |
use craft\base\PreviewableFieldInterface; | |
use craft\base\SortableFieldInterface; | |
use craft\fields\conditions\OptionsFieldConditionRule; | |
use craft\helpers\Cp; | |
/** | |
* Dropdown field with all German states | |
*/ | |
class GermanStateField extends Field implements SortableFieldInterface, PreviewableFieldInterface | |
{ | |
/** | |
* @inheritdoc | |
*/ | |
public static function displayName(): string | |
{ | |
return Craft::t('site', 'German State'); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public static function valueType(): string | |
{ | |
return GermanState::class . '|null'; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function normalizeValue(mixed $value, ?ElementInterface $element = null): mixed | |
{ | |
if ($value instanceof GermanState) { | |
return $value; | |
} | |
if (is_string($value)) { | |
return GermanState::tryFromIsoCode($value); | |
} | |
return null; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function serializeValue(mixed $value, ?ElementInterface $element = null): mixed | |
{ | |
if ($value instanceof GermanState) { | |
return $value->name; | |
} | |
return null; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function getTableAttributeHtml(mixed $value, ElementInterface $element): string | |
{ | |
/** @var GermanState|null $value */ | |
return $value?->label ?? ''; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function getElementConditionRuleType(): ?string | |
{ | |
return OptionsFieldConditionRule::class; | |
} | |
/** | |
* Returns all options | |
* | |
* @return array<int, array{label: string, value: string}> | |
*/ | |
public function getOptions(): array | |
{ | |
return [['label' => '', 'value' => '']] + GermanState::options(); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
protected function inputHtml(mixed $value, ?ElementInterface $element = null): string | |
{ | |
/** @var GermanState|null $value */ | |
return Cp::selectHtml([ | |
'id' => $this->getInputId(), | |
'name' => $this->handle, | |
'describedBy' => $this->describedBy, | |
'value' => $value?->name, | |
'options' => $this->getOptions(), | |
'required' => $this->required, | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment