Right Way to Add New Walker to Sage 9

I now have QWp6t’s Foundation.php walker working using psr-4 autloading, instead of by requiring the foundation walker file after the Soil plugin has loaded using after_setup_theme().

The problem is that Soil’s NavWalker class is in a file named nav-walker.php. According to the psr-4 spec #3.3 listed here:

“When loading a file that corresponds to a fully qualified class name, the terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.”

So the nav-walker.php file name and NavWalker class name need to match.

I did the following:

  1. Changed the file name from nav-walker.php to NavWalker.php.

  2. Soil’s modules are loaded based on file name, so in Sage’s setup.php file I changed line 28 to add_theme_support('soil-NavWalker').

  3. Added the following to Bedrock’s composer.json file (multi-line code formatting isn’t working in ordered lists):

    “autoload”: {
    “psr-4”: {
    “Roots\Soil\Nav\”: “web/app/plugins/soil/modules/”
    }
    }

  4. Ran composer update and confirmed the mapping had been added to Bedrock’s /vendor/composer/autoload_psr4.php.

  5. Finally, in Sage’s functions.php file I removed the after_setup_theme() hook I had previously used to load the foundation walker file and instead added the file to Sage’s required files array:
    ['helpers', 'setup', 'filters', 'admin', 'theme-options', 'nav-foundation-walker']

And voila, it works!

It would be great if the Soil changes could be merged so that it can work with autoloading.

Merry Christmas! (if that’s your thing)

4 Likes