Skip to content
Emeka Mbah edited this page May 20, 2023 · 4 revisions

Alert Macro

The Macro provides a flexible way to extend the functionality of alert.

Example: In this example, we will create a custom alert type called Text

  1. In the boot method of the AppServiceProvider, register a macro
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Digitlimit\Alert\Facades\Alert;
use Digitlimit\Alert\Message\MessageInterface;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Alert::macro('text', function (string $message) : MessageInterface 
        {
            return Alert::from('text', $message);
        });
    }
}
  1. Create a view component for Text alert type
php artisan make:component Text

Then edit the generated component as below.

<?php

namespace App\View\Components;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
use Digitlimit\Alert\Alert;

class Text extends Component
{
    /**
     * Set the default tag.
     */
    public string $defaultTag = Alert::DEFAULT_TAG;

    /**
     * Alert instance.
     */
    public Alert $alert;
    
    /**
     * Create a new component instance.
     */
    public function __construct(Alert $alert)
    {
        $this->alert = $alert;
    }

    /**
     * Get the view / contents that represent the component.
     */
    public function render(): View|Closure|string
    {
        return view('components.text');
    }
}
  1. Create Text alert type

The class MUST

  • Implement: Digitlimit\Alert\Message\MessageInterface
  • Extend: Digitlimit\Alert\Message\AbstractMessage;
<?php

namespace App\Types;

use Digitlimit\Alert\Helpers\Helper;
use Digitlimit\Alert\Message\AbstractMessage;
use Digitlimit\Alert\Message\MessageInterface;
use Digitlimit\Alert\SessionInterface;

class Text extends AbstractMessage implements MessageInterface
{
    /**
     * Indicate if text is underlined
     */
    public bool $underline = false;

    /**
     * Indicate if text is bold
     */
    public bool $bold = false;

    /**
     * Create a new field alert instance.
     *
     * @return void
     */
    public function __construct(
        protected SessionInterface $session,
        public ?string $message
    ) {
        $this->id($this->key() . '-' . Helper::randomString());
    }

    /**
     * Message store key for the field alert.
     */
    public function key(): string
    {
        return 'text';
    }

    /**
     * Bold the text
     */
    public function bold(): self
    {
        $this->bold = true;

        return $this;
    }

    /**
     * Underline the text
     */
    public function underline(): self
    {
        $this->underline = true;

        return $this;
    }
}
  1. Register the Component and Type Class in the Alert config file If you have not already published the alert config file, do so.
php artisan vendor:publish --provider="Digitlimit\Alert\AlertServiceProvider"
<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Alert Types
    |--------------------------------------------------------------------------
    |
    | Here you may register your custom alert types
    |
    */

    'types' => [
        ....
        'text' => [
            'view'      => 'alert-text',
            'alert'     => \App\Types\Text::class,
            'component' => \App\View\Components\Text::class,
        ],
    ],
];
  1. Update the view component which was generated initially resources/views/components/text.blade.php
@php
    $tag  = $attributes->get('tag', $defaultTag);
    $text = $alert->tagged('text', $tag);
@endphp
@if($text)
    <div  
    
    {{ $attributes->merge(['class' => 'form-text text-'.$text->level]) }}
    
    @style([
        'text-decoration: underline' => $text->underline,
        'font-weight: bold'          => $text->bold,
    ])>
        {{ $text->message }}
    </div>
@endif
  1. Finally, we can use our custom alert
 <x-alert-text />

Examples

In the application

  Alert::text('This is really awesome!')
  ->bold()
  ->underline()
  ->info()
  ->flash();

In the view

@extends('layouts.default')

@section('content')
  <div class="container-xxl flex-grow-1 container-p-y">
    <div class="row">
      <div class="col-md-12 mb-4">
        <x-alert-text />
      </div>
    </div>

    <div class="row">
      @include('form.profile')
    </div>        
  </div>

  @include('partials.footer')
@endsection

The result

image

Clone this wiki locally