Skip to content

HasConfigurableItems Trait

The HasConfigurableItems trait provides a flexible way to configure Filament resources, pages, and other components through configuration files, allowing for centralized customization without modifying class files.

Features

This trait enables:

  • Configuration of Filament components via configuration files
  • Hierarchical fallback for configuration values
  • Support for closures as configuration values
  • Automatic module detection and configuration key generation

Key Methods

getConfigValue(string $key, mixed $default = null): mixed

Retrieves a configuration value with hierarchical fallbacks.

  • Parameters:

    • $key (string): The configuration key to retrieve
    • $default (mixed): The default value if no configuration is found
  • Returns:

    • The configured value, or the default if not found

The trait overrides several navigation-related methods to make them configurable:

  • getNavigationIcon()
  • getActiveNavigationIcon()
  • getNavigationGroup()
  • getNavigationLabel()
  • getModelLabel()
  • getNavigationSort()
  • getCluster()

Configuration Structure

The trait looks for configuration values in two places:

  1. First in module-manager.modules.[module_name].[class_name]
  2. Then in [module_name].[class_name]

Where:

  • [module_name] is the snake_case version of the module name
  • [class_name] is the snake_case version of the class name

Usage Example

Configuration File

php
// config/blog.php
return [
    'post_resource' => [
        'navigationIcon' => 'heroicon-o-document-text',
        'navigationGroup' => 'Content Management',
        'navigationSort' => 10,
        'modelLabel' => 'Article',
        'navigationLabel' => 'Blog Posts',
    ],
];

Resource Class

php
<?php

namespace Modules\Blog\Filament\Resources;

use Filament\Resources\Resource;
use ModuleManager\ModuleManager\Concerns\HasConfigurableItems;

class PostResource extends Resource
{
    use HasConfigurableItems;

    // No need to define navigation properties - they'll be loaded from config

    // The rest of your resource implementation...
}

Notes

  • This trait is useful for creating module templates that can be customized through configuration
  • Configuration can be specified at the application level for all modules or at the individual module level
  • The trait automatically integrates with the module detection system to find the correct configuration keys
  • When a closure is provided as a configuration value, it will be evaluated when accessed