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

EnumFilter: Display the value returned by the method defined in "property" #3

Merged
merged 1 commit into from
Aug 16, 2023

Conversation

AndreSchwarzer
Copy link
Contributor

Display the value returned by the method defined in "property"

Similar to #1

Display the value returned by the method defined in "property"

Similar to datomatic#1
@AndreSchwarzer
Copy link
Contributor Author

Code

In the Nova resource:

    public function filters(NovaRequest $request): array
    {
        return [
            EnumBooleanFilter::make('payment', PaymentMethod::class)
                ->name(__('Zahlungsart'))
                ->property('label'),
        ];
    }

The ENUM:

enum PaymentMethod: string
{
    case SpkSepa = 'sparkasse-sepa';
    case SpkWire = 'sparkasse-wire';
    case BwStripeCC = 'billwerk-stripe-cc';
    case BwPayPal = 'billwerk-paypal';

    public function label(): string
    {
        return match ($this) {
            self::SpkSepa => __('Lastschrift'),
            self::SpkWire => __('Überweisung'),
            self::BwStripeCC => __('Kreditkarte'),
            self::BwPayPal => __('PayPal'),
        };
    }
}

How it's displayed

Before After
Screenshot 2023-08-07 at 10 49 22 Screenshot 2023-08-07 at 10 49 06

@AndreSchwarzer
Copy link
Contributor Author

Hey @trippo and @Synchro

is there anything further I can provide for you to get this merged?

@Synchro
Copy link
Contributor

Synchro commented Aug 16, 2023

I don't have commit rights here, so nothing I can do. Do you need this though? I use a label function like that, and I set up my fields like this to use it:

            Enum::make('Type')
                ->attach(CustomerType::class)
                ->options(CustomerType::options())
                ->default(CustomerType::CUSTOMER)
                ->displayUsingLabels()
                ->filterable(),

I think displayUsingLabels() is what you're looking for. The options() method you see on my enum is a trait that looks like this:

declare(strict_types=1);

namespace App\Enums;

trait HasOptions
{
    /**
     * Gather all the values of an Enum and return an array of values and labels suitable for populating select elements
     *
     * @return array
     */
    public static function options(): array
    {
        $options = [];
        foreach (self::cases() as $case) {
            $options[$case->value] = $case->label();
        }
        return $options;
    }
}

displayUsingLabels() is used when displaying the field normally, and options() is used when editing or creating, so you need both.

@trippo trippo merged commit 60e9c3f into datomatic:main Aug 16, 2023
1 check passed
@trippo
Copy link
Contributor

trippo commented Aug 16, 2023

Thanks

@AndreSchwarzer
Copy link
Contributor Author

Hey @Synchro thanks for your suggestion.

I encountered the same issue like I had in #1 with the Filters. The property parameter wasn't used throughout every step/in every place.

With this PR I fixed it for Filters.

@AndreSchwarzer AndreSchwarzer deleted the main-1 branch August 17, 2023 15:13
This pull request was closed.
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

Successfully merging this pull request may close these issues.

3 participants