Get custom namespace content/partial in hook

So have this namespace in my config/views.php

/*
    |--------------------------------------------------------------------------
    | View Namespaces
    |--------------------------------------------------------------------------
    |
    | Blade has an underutilized feature that allows developers to add
    | supplemental view paths that may contain conflictingly named views.
    | These paths are prefixed with a namespace to get around the conflicts.
    | A use case might be including views from within a plugin folder.
    |
     */

    'namespaces' => [
        'SVG' => get_theme_file_path() . '/resources/assets/svg',
    ],

Which allows me to grab SVGs within my blade templates much like normal partials like so:

<div class="icon">
    @include('SVG::icons.phone')
</div>

I am wanting to be able to call one of those icons within a wp_menu a acf field I have that matches whats in that icons folder:

add_filter('wp_nav_menu_objects', function ($items, $args) {
    foreach ($items as &$item) {
        if (get_field('use_icon', $item)) {
			if($icon = get_field('svg_icon', $item)) {
				// THIS LINE DOESNT WORK
				$item->title =  App\template('SVG::icons.' . $icon);
			}
        }
    }
    return $items;
}, 10, 2);

I get this error:


Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined function App\App\template() in /Users/mike/sites/glu-recruit/web/app/themes/glu-theme/app/filters.php:129 Stack trace: 
#0 /Users/mike/sites/glu-recruit/web/wp/wp-includes/class-wp-hook.php(303): App\{closure}(Array, Object(stdClass)) 
#1 /Users/mike/sites/glu-recruit/web/wp/wp-includes/plugin.php(189): WP_Hook->apply_filters(Array, Array) 
#2 /Users/mike/sites/glu-recruit/web/wp/wp-includes/nav-menu-template.php(226): apply_filters('wp_nav_menu_obj...', Array, Object(stdClass)) 
#3 /Users/mike/sites/glu-recruit/web/app/uploads/cache/f4e3467db59f0e198d9e321de1c372a12ebd6032.php(20): wp_nav_menu(Object(stdClass)) 
#4 /Users/mike/sites/glu-recruit/web/app/themes/glu-theme/vendor/illuminate/view/Engines/PhpEngine.php(43): include('/Users/mike/sit...') 
#5 /Users/mike/sites/glu-recruit/web/app/themes/glu-theme/vendor/illuminate/view/Engines/CompilerEngine.php(59): Illuminate\View\Engines\PhpEngine->evaluatePath('/Users/mike/sit...', Array in /Users/mike/sites/glu-recruit/web/app/themes/glu-theme/app/filters.php on line 129

I am a bit lost as to how to get it to work. I know that you can use normal partials within hooks with the following:

<?php
echo App\template('partials.some-partial');

This is what lead me to my current solution that doesn’t work obviously so any help would be gratefully received :slight_smile:

Your error says:

Call to undefined function App\App\template() 

Which means this is being called in a file that already has the App namespace, and it’s prepending it. Namespacing functionality doesn’t check to see if it’s adding the same namespace segment twice (and indeed it couldn’t possibly know if that behavior is accidental or intended): It just prepends if there’s no leading slash, and doesn’t if there isn’t.

You could do either of the following:

$item->title =  \App\template('SVG::icons.' . $icon);
// or
$item->title =  template('SVG::icons.' . $icon);

and it should work.

1 Like

You, sir… are a life saver!

I was wondering why it was doing the double \App\App

Thank you for your help.

1 Like

This topic was automatically closed after 42 days. New replies are no longer allowed.