Skip to content

Commit

Permalink
Updated for use with Placeholder package
Browse files Browse the repository at this point in the history
  • Loading branch information
AngryMoustache committed Aug 29, 2023
1 parent 0fc0ccf commit 0d43950
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
17 changes: 8 additions & 9 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,27 @@ class Inquiry extends Model
}
```

After doing this, you'll also need to define what fields you want to be able to use in the mail template. To do this you'll need to define the fields in the `getMailVariables` method.

The keys in this array will be the variables that show up in the mail template as labels, and the values will be the values that will be used in the mail template, when sent.
After doing this, you'll also need to define what fields you want to be able to use in the mail template. To do this you'll need to define the fields in the `getPlaceholderVariables` method of the model. This method should return an array of `Codedor\FilamentPlaceholderInput\PlaceholderVariable` objects.

```php
namespace App\Models;

use Codedor\FilamentMailTemplates\Models\Traits\HasMails;
use Codedor\FilamentPlaceholderInput\PlaceholderVariable;
use Illuminate\Database\Eloquent\Model;

class Inquiry extends Model
{
use HasMails;

public function getMailVariables(): array
public function getPlaceholderVariables(): array
{
return [
'first name' => $this->first_name,
'last name' => $this->last_name,
'e-mail' => $this->email,
'message body' => $this->message,
'inquiry type' => $this->inquiryType?->working_title,
PlaceholderVariable::make('first name', 'First name', $this->first_name),
PlaceholderVariable::make('last name', 'Last name', $this->last_name),
PlaceholderVariable::make('e-mail', 'E-mail', $this->email),
PlaceholderVariable::make('message body', 'Message content', $this->message),
PlaceholderVariable::make('inquiry type', 'Used inquiry type', $this->inquiryType?->working_title),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function mount($record): void
$this->updatedCurrentLocale($this->currentLocale);
}

public function updatedCurOrentLocale(string $locale): void
public function updatedCurrentLocale(string $locale): void
{
$this->preview = $this->record
->getMailTemplate()
Expand Down
5 changes: 4 additions & 1 deletion src/Mail/MailableTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Codedor\FilamentMailTemplates\MailTemplateBuilder;
use Codedor\FilamentMailTemplates\Models\MailHistory;
use Codedor\FilamentMailTemplates\Models\MailTemplate;
use Codedor\FilamentPlaceholderInput\PlaceholderVariable;
use Illuminate\Bus\Queueable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Mail\Mailable;
Expand Down Expand Up @@ -94,7 +95,9 @@ public function parseVariables(string $content): string
return $content;
}

$data = $this->item->getMailVariables();
$data = collect($this->item->getPlaceholderVariables())

Check failure on line 98 in src/Mail/MailableTemplate.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Model::getPlaceholderVariables().
->mapWithKeys(fn (PlaceholderVariable $value) => [$value->getKey() => $value->getValue()])
->toArray();

return preg_replace_callback(
'/{{ (?<keyword>.*?) }}/',
Expand Down
2 changes: 1 addition & 1 deletion src/Models/MailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function getOnlineAttribute()
public function getEmailsFor(string $type): Collection
{
return $this->getAllEmails()
->filter(fn ($email) => $email['type'] === $type)
->filter(fn ($email) => ($email['type'] ?? $email) === $type)
->pluck('email');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Models/Traits/HasMails.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function registerMail($identifier): MailTemplateBuilder
return $builder;
}

public function getMailVariables(): array
public function getPlaceholderVariables(): array
{
return [];
}
Expand Down

0 comments on commit 0d43950

Please sign in to comment.