F
Filament3mo ago
Prodex

Weird override behavior

Hi, I'm overriding the Shield resource, so I don't have to publish the stubs. But I just don't understand why some methods can be overwritten but some others not. For example: I can easierly overwrite shouldRegisterNavigation() or getNavigationLabel(), but I can't overwrite getPluralModelLabel(). Both getNavigationLabel() and getPluralModelLabel() are used the same way in the base Shield resource. But only getNavigationLabel() works, the other method (getPluralModelLabel) doesn't even get called. Can someone explain to me, why this is happening? Full code:
class ShieldOverrideResource extends RoleResource
{
public static function getModelLabel(): string
{
return __('employee.permission_roles'); // doesn't work
}

public static function getPluralModelLabel(): string
{
return 'test'; // doesn't work
}

public static function getNavigationLabel(): string
{
return 'test'; // works
}

public static function getNavigationParentItem(): string
{
return __('employee.plural'); // works
}

public static function shouldRegisterNavigation(): bool
{
return true; // works
}
}
class ShieldOverrideResource extends RoleResource
{
public static function getModelLabel(): string
{
return __('employee.permission_roles'); // doesn't work
}

public static function getPluralModelLabel(): string
{
return 'test'; // doesn't work
}

public static function getNavigationLabel(): string
{
return 'test'; // works
}

public static function getNavigationParentItem(): string
{
return __('employee.plural'); // works
}

public static function shouldRegisterNavigation(): bool
{
return true; // works
}
}
For reference, this is the shield base class resource:
public static function getPluralModelLabel(): string
{
return __('filament-shield::filament-shield.resource.label.roles');
}

public static function getNavigationLabel(): string
{
return __('filament-shield::filament-shield.nav.role.label');
}
public static function getPluralModelLabel(): string
{
return __('filament-shield::filament-shield.resource.label.roles');
}

public static function getNavigationLabel(): string
{
return __('filament-shield::filament-shield.nav.role.label');
}
Solution:
The reason some method overrides work while others don’t is due to how they are scoped and utilized. For example, model label and model plural label overrides do not work because you’re only overriding the RoleResource. To make those overrides work, you also need to extend the ListRoles page of the resource and bind your custom implementation in a service provider, just like you did with the resource. The same applies to sub-navigation. To get sub-navigation working, you need to extend both the EditRole and ViewRole pages of the RoleResource with your custom implementation of those pages. Once you’ve done that, bind them in a service provider. Then, within your custom RoleResource (ShieldOverrideResource) implementation, override the getPages() method to provide your implementation for those pages, and implement getRecordSubNavigation() accordingly....
Jump to solution
1 Reply
Solution
Bezhan
Bezhan3mo ago
The reason some method overrides work while others don’t is due to how they are scoped and utilized. For example, model label and model plural label overrides do not work because you’re only overriding the RoleResource. To make those overrides work, you also need to extend the ListRoles page of the resource and bind your custom implementation in a service provider, just like you did with the resource. The same applies to sub-navigation. To get sub-navigation working, you need to extend both the EditRole and ViewRole pages of the RoleResource with your custom implementation of those pages. Once you’ve done that, bind them in a service provider. Then, within your custom RoleResource (ShieldOverrideResource) implementation, override the getPages() method to provide your implementation for those pages, and implement getRecordSubNavigation() accordingly.
Want results from more Discord servers?
Add your server