Skip to content

Commit d3c8127

Browse files
committed
[LiveComponent] add LiveProp name to modifier function
1 parent ab44abf commit d3c8127

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/LiveComponent/doc/index.rst

+41
Original file line numberDiff line numberDiff line change
@@ -2651,6 +2651,47 @@ This way you can also use the component multiple times in the same page and avoi
26512651
<twig:SearchModule alias="q1" />
26522652
<twig:SearchModule alias="q2" />
26532653

2654+
.. versionadded:: 2.25
2655+
2656+
The property name is passed into the modifier function since LiveComponents 2.25.
2657+
2658+
The ``modifier`` function can also take the name of the property as a secondary parameter.
2659+
It can be used to perform more generic operations inside of the modifier that can be re-used for multiple props::
2660+
2661+
abstract class AbstractSearchModule
2662+
{
2663+
#[LiveProp(writable: true, url: true, modifier: 'modifyQueryProp')]
2664+
public string $query = '';
2665+
2666+
protected string $urlPrefix = '';
2667+
2668+
public function modifyQueryProp(LiveProp $liveProp, string $propName): LiveProp
2669+
{
2670+
if ($this->urlPrefix) {
2671+
return $liveProp->withUrl(new UrlMapping(as: $this->urlPrefix.'-'.$propName));
2672+
}
2673+
return $liveProp;
2674+
}
2675+
}
2676+
2677+
#[AsLiveComponent]
2678+
class ImportantSearchModule extends AbstractSearchModule
2679+
{
2680+
}
2681+
2682+
#[AsLiveComponent]
2683+
class SecondarySearchModule extends AbstractSearchModule
2684+
{
2685+
protected string $urlPrefix = 'secondary';
2686+
}
2687+
2688+
.. code-block:: html+twig
2689+
2690+
<twig:ImportantSearchModule />
2691+
<twig:SecondarySearchModule />
2692+
2693+
The ``query`` value will appear in the URL like ``/search?query=my+important+query&secondary-query=my+secondary+query``.
2694+
26542695
Validating the Query Parameter Values
26552696
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26562697

src/LiveComponent/src/Metadata/LivePropMetadata.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function withModifier(object $component): self
135135
throw new \LogicException(\sprintf('Method "%s::%s()" given in LiveProp "modifier" does not exist.', $component::class, $modifier));
136136
}
137137

138-
$modifiedLiveProp = $component->{$modifier}($this->liveProp);
138+
$modifiedLiveProp = $component->{$modifier}($this->liveProp, $this->getName());
139139
if (!$modifiedLiveProp instanceof LiveProp) {
140140
throw new \LogicException(\sprintf('Method "%s::%s()" should return an instance of "%s" (given: "%s").', $component::class, $modifier, LiveProp::class, get_debug_type($modifiedLiveProp)));
141141
}

src/LiveComponent/tests/Unit/Metadata/LivePropMetadataTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testWithModifier()
2929
$component
3030
->expects($this->once())
3131
->method('modifyProp')
32-
->with($liveProp)
32+
->with($liveProp, 'propWithModifier')
3333
->willReturn($liveProp->withFieldName('customField'));
3434

3535
$livePropMetadata = $livePropMetadata->withModifier($component);

0 commit comments

Comments
 (0)