Skip to content

[Sluggable] Make fields option also accept getter methods #2998

@pbreteche

Description

@pbreteche

Feature Request

Some entities could have no meaningful properties, but developer can provide a getter method return string to build slug above.

How to use

Just put in fields option method 's name as any doctrine field name, SlugListener will call entity customGetter method.

    #[Gedmo\Slug(fields: ['customGetter'])]
    private ?string $slug = null;

    public function customGetter(): string
    {
        return 'some string';
    }

Do you think it could be an improvement on how we can build nicer slugs from our entities ?

I think I can work around by introducing a createdAt field and then go with CustomSlugHandler but I don't really like introducing extra data in my URL that has little meaning for my users.

Another approach could be to allow fields option to be empty in Slug attribute if a SlugHandler attribute exists on the same property.

    #[Gedmo\Slug] // no fields defined here
    #[Gedmo\SlugHandler(class: MyCustomHandler::class)]
    private ?string $slug = null;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions