I created a service provider with wp acorn make:provider PatternsServiceProvider
to add a helper to add an image to a pattern with a cover image similar to how WooCommerce seems to do it. So now I have wp-content/themes/cafejp/app/Providers/PatternsServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Helpers\PatternsHelper;
class PatternsServiceProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register(): void
{
$this->app->singleton('patternshelper', function () {
return new PatternsHelper();
});
}
/**
* Bootstrap services.
*/
public function boot(): void
{
//
}
}
I also registered the provider in wp-content/themes/cafejp/config/app.php
using
<?php
return [
'providers' => [
// Other service providers
App\Providers\PatternsServiceProvider::class,
],
// Other configurations...
];
loading this helper wp-content/themes/cafejp/app/Helpers/PatternsHelper.php
<?php
namespace App\Helpers;
class PatternsHelper
{
public function get_image_url($image_name)
{
// Assuming images are stored in the `resources/images` directory.
return get_template_directory_uri() . '/resources/images/' . $image_name;
}
}
But how can I now load the image in wp-content/themes/cafejp/resources/views/block-patterns/drinks.blade.php
:
@inject('patternsHelper', 'patternshelper')
<!-- wp:cover {"url":"<?php echo esc_url( App\Helpers\PatternsHelper::get_image_url('2402_3200_matthieu-joannon-6cilddtotgm-unsplash.webp') ); ?>","dimRatio":0,"overlayColor":"black","isUserOverlayColor":true,"isDark":false,"metadata":{"categories":["fandb"],"patternName":"cafejp/drinks","name":"Drinks Menu"},"align":"full","className":"cafejp-drinks-menu-pattern"} -->
<div class="wp-block-cover alignfull is-light cafejp-drinks-menu-pattern"><span aria-hidden="true" class="wp-block-cover__background has-black-background-color has-background-dim-0 has-background-dim"></span><img class="wp-block-cover__image-background wp-image-293" alt="" src="https://cafejpcoen.test/wp-content/uploads/2024/07/2402_3200_matthieu-joannon-6cilddtotgm-unsplash.webp" style="object-position:53% 46%" data-object-fit="cover" data-object-position="53% 46%"/><div class="wp-block-cover__inner-container"><!-- wp:group {"layout":{"type":"flex","flexWrap":"nowrap","justifyContent":"right"}} -->
<div class="wp-block-group"><!-- wp:group {"style":{"spacing":{"padding":{"right":"var:preset|spacing|60","left":"var:preset|spacing|60"}}},"layout":{"type":"flex","orientation":"vertical"}} -->
<div class="wp-block-group" style="padding-right:var(--wp--preset--spacing--60);padding-left:var(--wp--preset--spacing--60)"><!-- wp:heading {"style":{"elements":{"link":{"color":{"text":"var:preset|color|white"}}}},"textColor":"white","fontSize":"6xl"} -->
<h2 class="wp-block-heading has-white-color has-text-color has-link-color has-6-xl-font-size">Drinks</h2>
<!-- /wp:heading --></div>
<!-- /wp:group -->
...
It seems things do not load properly as I get errors that the method is not static . But I thought things should load in just fine.