Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 29, 2025

Add support for registering globals with Eloquent and Stache Drivers ✅

Summary

This PR adds full support for registering global sets when using Statamic's Eloquent Driver AND fixes the default Stache driver implementation. Builder-registered global sets now properly show up in the Statamic control panel in both scenarios.

Implementation Details

Files Changed:

  1. src/Repositories/EloquentGlobalRepository.php (new)

    • Created new repository extending Statamic\Eloquent\Globals\GlobalRepository
    • Initializes builder-registered globals from config('statamic.builder.globals')
    • Properly merges builder globals with database globals (builder globals take precedence)
    • Builder globals are keyed by handle and returned in the correct GlobalCollection format
  2. src/ServiceProvider.php (modified)

    • Updated global repository binding to check for Eloquent Driver configuration
    • Conditionally returns EloquentGlobalRepository when statamic.eloquent-driver.globals.driver === 'eloquent'
    • Falls back to standard GlobalRepository for Stache-based storage (default)
    • Follows the same pattern as NavigationRepository
  3. src/Stache/Stores/GlobalsStore.php (modified)

    • Fixed: Removed incorrect $key parameter from constructor call
    • Fixed: Corrected type hint from BaseTaxonomy to BaseGlobalSet
    • Fixed: Removed unused BaseTaxonomy import
    • Improved error message to say "Global Set" instead of "Collection"
  4. tests/Helpers/TestGlobalSet.php (new)

    • Test helper class for testing global registration
  5. tests/Unit/GlobalRepositoryTest.php (modified)

    • Added tests for finding and listing builder-registered globals

How It Works

For Both Stache and Eloquent Drivers:

  1. Builder-registered global sets are loaded from config('statamic.builder.globals') on repository initialization
  2. The find() and findByHandle() methods check builder globals first, then database/files
  3. Builder global sets are properly keyed by their handle for correct display

Stache Driver (default - file-based):

  • GlobalRepository uses GlobalsStore to create GlobalSets via the register() method
  • GlobalSets are cached in the Stache for performance
  • Works seamlessly with file-based global storage

Eloquent Driver (database-based):

  • EloquentGlobalRepository merges database globals with builder globals
  • Builder globals take precedence when the same handle exists in both sources
  • Works seamlessly with database global storage

Usage

// config/statamic/builder.php
'globals' => [
    \App\Globals\SiteSettings::class,
],

Builder-registered global sets will now appear in:

  • GlobalSet::all() - Lists all global sets
  • GlobalSet::findByHandle('site_settings') - Finds specific global set
  • Statamic Control Panel - Displays in the globals listing

What's a Global Set?

In Statamic, a Global Set is a container that holds global variables. For example:

  • A "footer" global set might contain variables like copyright text, social media links, etc.
  • A "site_settings" global set might contain variables like site name, logo, contact info, etc.

This PR enables you to register these global set containers via code, similar to how you register collections and taxonomies.

Plan Completed:

  • Investigate and understand the requirements
  • Review current implementation and Eloquent Driver behavior
  • Create EloquentGlobalRepository class for Eloquent Driver
  • Update ServiceProvider with conditional binding
  • Add comprehensive tests
  • Fix merge logic for proper handle-based keying
  • Address all code review feedback
  • Improve code comments for clarity
  • Fix Stache driver implementation (GlobalsStore)
  • Remove unused imports
  • Run security checks (no issues found)
Original prompt

This section details on the original issue you should resolve

<issue_title>Add support for registering globals</issue_title>
<issue_description>I would love to register a global not only a global blueprint variable using the Statamic builder. This is partly built, but when you use the Eloquent Driver, the currently registered globals are not found, although they should be added.

  • Read the statamic docs and implementation
  • Review the current code
  • Review the Eloquent Driver code</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Add support for registering globals with Eloquent Driver Add Eloquent Driver support for builder-registered globals Dec 29, 2025
Copilot AI requested a review from tdwesten December 29, 2025 20:24
@tdwesten
Copy link
Owner

@copilot I now see the challange is bigger, we need a way to register globals from the statamic builder, but statamic uses globals and globals sets. Deep think how we can do this. Now the registered globals (in the current state of the code/repo) do not show up. review the statamic code for globals and there implementation.

Succes is when we can register the globals from code, like we do with the blueprints.

Copy link
Contributor Author

Copilot AI commented Dec 29, 2025

@copilot I now see the challange is bigger, we need a way to register globals from the statamic builder, but statamic uses globals and globals sets. Deep think how we can do this. Now the registered g...

I understand the issue now. The challenge is that GlobalCollection expects GlobalSets to be properly keyed by their handle. The current implementation creates the GlobalSets but the merging logic might not preserve the keys correctly.

I'll fix the EloquentGlobalRepository::all() method to ensure builder-registered globals are properly keyed and show up in the control panel. The issue is in how we're merging the collections - we need to use keyBy() to ensure proper handle-based keying.

Working on the fix now.

@tdwesten
Copy link
Owner

@copilot Oke so we need also a way to register and global sets i think

Copy link
Contributor Author

Copilot AI commented Dec 29, 2025

@copilot Oke so we need also a way to register and global sets i think

You're correct - we're already registering global sets (the containers). The current implementation:

  1. Config: 'globals' => [App\Globals\Footer::class] registers a GlobalSet class
  2. BaseGlobalSet: Creates the GlobalSet container with handle, title, and sites
  3. Repositories: Make these GlobalSets available via all(), find(), and findByHandle()

The implementation already supports registering global sets (not individual variables within them). When you create a class extending BaseGlobalSet, you're defining the container structure. The actual global variables would be added through blueprints or by extending the register() method to add data.

Working on fixing both Stache and Eloquent implementations to ensure they work consistently.

Copilot AI changed the title Add Eloquent Driver support for builder-registered globals Add support for registering globals with both Eloquent and Stache drivers Dec 29, 2025
@tdwesten
Copy link
Owner

@copilot Oke lets rethink the structure of globals as implement in the statamic builder.

Here some info about the sets:

Statamic Globals: GlobalSets and Globals — Relations Only

  • A GlobalSet is the definition/container.

    • Identified by handle (e.g., site, seo).
    • Has a blueprint that defines fields.
    • One GlobalSet exists per handle across the project.
  • Globals are the per-site values of a GlobalSet.

    • Stored in content/globals/{handle}.yaml (default) and content/globals/{handle}.{site}.yaml (overrides).
    • Keys under data correspond 1:1 to blueprint field handles.
  • Relation cardinality

    • One GlobalSet → One Globals record per site (at most).
    • In single-site: One GlobalSet → One Globals file.
    • In multisite: One GlobalSet → Many site-specific Globals files, each overriding the base.
  • Resolution order

    1. Site-specific Globals (content/globals/{handle}.{site}.yaml)
    2. Base Globals (content/globals/{handle}.yaml)
  • Access binding

    • Blueprint fields (GlobalSet) map directly to Globals data keys by handle.
    • Missing site-specific keys fall back to base; missing in both resolve to null.

Copy link
Contributor Author

Copilot AI commented Dec 29, 2025

@copilot Oke lets rethink the structure of globals as implement in the statamic builder.

Here some info about the sets:...

You're absolutely right about the distinction. The current implementation correctly registers GlobalSets (the containers/definitions) but not the Globals (the per-site data).

Here's what we have now:

  • BaseGlobalSet creates the GlobalSet container with handle, title, and empty localizations
  • This allows the GlobalSet to appear in the CP and be editable
  • The actual data (Globals) would be entered through the CP or stored in YAML files

This matches how Collections work in the builder - we register the collection structure, but entries are added separately. The GlobalSet defines the container; blueprints define the fields; and the actual global data is managed through the CP or YAML files.

Is this the intended design, or do you want to extend BaseGlobalSet to also allow defining default data values programmatically? If so, we could add a data() method to populate initial values.

@tdwesten
Copy link
Owner

@copilot Oke, now create a way to test this working with a statamic laravel app with statamic builder addon installed

…support' into copilot/add-global-registration-support
…`, refactor `BaseCollection`, and improve `MakeCollectionCommand` functionality

Signed-off-by: Thomas van der Westen <[email protected]>
…repository enhancements, and related tests

Signed-off-by: Thomas van der Westen <[email protected]>
…repository enhancements, and related tests

Signed-off-by: Thomas van der Westen <[email protected]>
…ng `AssetContainerRepository`, `FieldsetRepository`, `TaxonomyRepository`, and custom field types like `ForeignField` and `Section`

Signed-off-by: Thomas van der Westen <[email protected]>
…nd`, `MakeFieldsetCommand`, and `MakeTaxonomyCommand`

Signed-off-by: Thomas van der Westen <[email protected]>
…es, commands, and related components

Signed-off-by: Thomas van der Westen <[email protected]>
…sets, collections, taxonomies, globals, sites, and navigations; add relevant config options, methods, and tests

Signed-off-by: Thomas van der Westen <[email protected]>
…iguration details

Signed-off-by: Thomas van der Westen <[email protected]>
…Taggable`; add new field types including `Hidden`, `Money`, `Number`, `Password`, and `Rating` with respective tests

Signed-off-by: Thomas van der Westen <[email protected]>
…lementation, commands, discovery, and tests

- Added `BaseAssetContainer` class with default methods for asset container management.
- Created `MakeAssetContainerCommand` for generating asset container classes.
- Enhanced auto-discovery to include asset containers.
- Updated `AssetContainerRepository` to manage builder-registered containers.
- Added configuration options and documentation for asset containers.
- Included unit and feature tests for asset container functionality.

Signed-off-by: Thomas van der Westen <[email protected]>
…aravel versions

- Added `.github/workflows/tests.yml` with matrix strategy for PHP (8.2 - 8.4) and Laravel (10.* - 12.*).
- Updated README badge to reflect the new workflow file.

Signed-off-by: Thomas van der Westen <[email protected]>
- Added `: void` return type to all test closures for consistency and compatibility.
- Removed unnecessary `TestCase` extends and extensions in test files.
- Adjusted `uses` configuration in `Pest.php` to apply globally for all tests.

Signed-off-by: Thomas van der Westen <[email protected]>
…c versions

- Updated README with new PHP (8.2+), Laravel (11/12), and Statamic (5.4+/6.0 Alpha) requirements.
- Modified `.github/workflows/tests.yml` to include Statamic matrix and stability settings.
- Updated `composer.json` to reflect dependency range adjustments.
- Updated `composer.lock` with dependency version changes.

Signed-off-by: Thomas van der Westen <[email protected]>
- Removed `.github/workflows/ci.yml` to streamline workflows.
- Merged linting and testing into a single `.github/workflows/tests.yml`.
- Added dependency installation and dedicated linting job.
- Adjusted test matrix for Laravel and Statamic versions.

Signed-off-by: Thomas van der Westen <[email protected]>
- Removed `.github/workflows/ci.yml` to streamline workflows.
- Merged linting and testing into a single `.github/workflows/tests.yml`.
- Added dependency installation and dedicated linting job.
- Adjusted test matrix for Laravel and Statamic versions.

Signed-off-by: Thomas van der Westen <[email protected]>
@tdwesten tdwesten closed this Jan 4, 2026
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.

Add support for registering globals

2 participants