New component not found - laravel

I've a problem with custom component for email in Laravel 5.8
I'have create a new file : /resources/vendor/mail/html/training.blade.php
In my view file (folder : /resources/views/emails/training/) i have :
#component('mail::training')
instead of :
#component('mail::message')
But i've got this error :
View [training] not found ...
Note : if i edit a file like : /resources/views/emails/header.blade.php
I see the modification in the email.
I miss somethings how create a new component ?
Thanks.
EDIT :
I found the solution. You need to duplicate the file in each dir :
/vendor/mail/html AND /vendor/mail/text

Every vendor component is registred in the serviceProvider of that package. If you want to make your own blade component follow the documentation.
https://laravel.com/docs/5.8/blade#components-and-slots
Components and slots provide similar benefits to sections and layouts; however, some may find the mental model of components and slots easier to understand. First, let's imagine a reusable "alert" component we would like to reuse throughout our application:
<!-- /resources/views/alert.blade.php -->
<div class="alert alert-danger">
{{ $slot }}
</div>
The {{ $slot }} variable will contain the content we wish to inject into the component. Now, to construct this component, we can use the #component Blade directive:
#component('alert')
<strong>Whoops!</strong> Something went wrong!
#endcomponent

if you put your component in "views" folder and remove the prefix 'mail::' in #component('mail::training') it should work successfully.
to make Laravel reads views from "views/vendor/mail/html" you can simply add this line resource_path('views/vendor/mail/html') to view.php in config folder.

Related

Difference between #aware and #props directives in laravel 9

The #aware Laravel Blade template directive was introduced in Laravel 8, but I still don't understand what it does differently from the already existing #props directive.
For instance, if I use a Blade template like this:
<x-view-content :page="$page ?? ''" />
and the component used above is defined in views/components/view-content.blade.php like this:
#aware(['page'])
<div>
{{ $page }}
</div>
I get the page successfully rendered inside the component. However, replacing #aware(['page']) with #props(['page']) produces the same result.
I would like to know what the differences between them are.
#aware is for accessing data from a parent component where you have #props. You can find here all relevant information. In your case you don't have child component.

Laravel: Conditional rendering with dynamic components not working

I want to have a dynamic component that can conditionally render child dynamic components.
Let's imagine having a parent component Parent.php
In my view blade this is then called something like this
(Of course, this wouldn't be parent but a method that resolves that name)
<x-dynamic-component :elements="$elements" component="parent" />
Within that component, based on the $elements variable, it should render other dynamic components
$element could be for instance Radio.php or Checkbox.php
So what I'm trying to do within my parent component is this:
<div>
#foreach($elements as $element)
#if($element->hasDynamicComponent())
<x-component-dynamic :component="$element->getComponentName()" />
#endif
#endforeach
The problem is, that Laravel nevertheless parses those lines regardless of this if statement.
I'm getting the following error message:
Unable to locate a class or view for component [component-dynamic]. (View: .....)
This is because $element->hasDynamicComponent() is false and therefore getComponentName is empty. Of course Laravel cannot resolve a component without a name.
The thing is Laravel shouldn't even try to resolve this, because hasDynamicComponent is false.

How to render multiple livewire component as string?

I have multiple livewire components e.g. opd.patient-count, opd.visit-count, opd.checkup-count and so on. I would like to store these components name into database then call by user role. Anyway, It does not work once I tried as below.
foreach ...
echo '<livewire:opd.patient-count />';
endforeach ...
Any advice or guidance on this would be greatly appreciated, Thanks.
As I understand your problem, You can achieve using this syntax instead
#foreach($components as $eachComponent)
#livewire($eachComponent,['componentData' => $data])
#endforeach
in this code above, the component name is assigned accordingly using a blade #foreach directive and iterate over each element and make the component.

Theme is caching previous user name

We are using CAS to login to our Drupal instance. This is working correctly and displaying the correct user content (blocks etc. based on roles). What is not working correctly is the small snippet in the theme that says welcome . It keeps showing the previous user who logged in.
How do I set this in bigpipe?
The code looks like this in the theme: <span id="user_name">{{user.displayname}}</span>
Is there a way to tell bigpipe not to cache this?
This code snippet is on one of our twig files header.twig.html which is a partial.
I ended up putting this in a block, and just referencing the block section in the theme instead of just pulling that, then I used the block to be ignored for caching.
Thanks!
I used this post with other resources to solve a similar problem. We were including {{ user.displayname }} in a twig template for the header on all pages of our site. Some users were seeing other users' names in the header after signing in. We wanted to solve the problem while impacting caching as little as possible. Here, I share in detail what we did in the hope that it will help others. I'll use the specific names used in our code. Readers will need to adjust to their own names. (The code follows our prescribed format, so please forgive that it isn't standard.)
Step 1
Create a custom module. Custom module creation is covered adequately in other places, so I won't give details here. Our custom module is named rsc.
Step 2
Create the folder modules/custom/rsc/src/Plugin/Block and in it create a file named DisplayName.php.
Step 3
In the file DisplayName.php, include the following:
<?php
namespace Drupal\rsc\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\user\Entity\User;
/**
* A block to show the user's display name
*
* #Block(
* id = "display_name",
* admin_label = "Display Name"
* )
*/
class DisplayName extends BlockBase // The class name must match the filename
{
public function build()
{
$user = User::load(\Drupal::currentUser()->id());
return
[
'#type' => 'markup',
'#markup' => $user->getDisplayName(),
'#cache' => ['contexts' => ['user']]
];
}
}
Step 4
Clear the Drupal cache to make the new block available at Block Layout.
Step 5
Go to Admin > Structure > Block Layout and place the Display Name block in the Hidden Blocks For Referencing - Not Displayed section (bottom of the list on our site). In the Configure Block dialog, edit the Machine name to display_name to match id in the code above. Clear the Display title checkbox and save. Doing this makes the block available for use in twig templates.
Step 6
In the twig template for the header, replace
{{ user.displayname }}
with
{{ drupal_entity('block', 'display_name') }}
The drupal_entity function is part of the twig_tweak module, which we were already using. It allows insertion of a custom block into a twig template. If you're not using this module, you'll need to install it, or find another method of including a block in a twig template.
Step 7
Clear the Drupal cache to make the modified template take effect.
If you see anything about this that can be improved, please comment.

Laravel blade adding a class for a particular uri using a wildcard

Im trying to make a menu but some of my pages have custom uris based on a random string
like /page/show/hn87qh208h2u3gf8o7g87
I've tried a few various implementations, like the one below, but its not working. Has anyone come across this before?
I'm lead to believe from searching that the wildcard would work within blade like it does in php, but its seems not, it could be because i'm using the latest version and its been removed/changed maybe?
{{ Request::is('/page/show/*') ? 'active' : '' }}
Provide a name to your particular route as below:
Suppose you have route as below:
Route::get('page/show/{id}', 'Path\To\Your\Controller\Name#methodName');
Rewrite it as :
Route::get('page/show/{id}', ['as' => 'show-page','uses' => 'Path\To\Your\Controller\Name#methodName'])
OR
Route::get('page/show/{id}', 'Path\To\Your\Controller\Name#methodName')->nane('show-page');
In view file change the code as below:
{{ Request::route()->getName() == 'show-page' ? 'active' : '' }}
I hope may this will help you.

Resources