Skip to content

Commit 631af7c

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

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/LiveComponent/doc/index.rst

+13-10
Original file line numberDiff line numberDiff line change
@@ -2617,7 +2617,9 @@ definition::
26172617

26182618
Then the ``query`` value will appear in the URL like ``https://my.domain/search?q=my+query+string``.
26192619

2620-
If you need to change the parameter name on a specific page, you can leverage the :ref:`modifier <modifier>` option::
2620+
If you need to change the parameter name on a specific page, you can leverage the :ref:`modifier <modifier>` option
2621+
which takes a reference to a public function in your component that takes the ``LiveProp`` and the ``name``
2622+
of the property and must return a modified ``LiveProp`` object::
26212623

26222624
// ...
26232625
use Symfony\UX\LiveComponent\Metadata\UrlMapping;
@@ -2629,27 +2631,28 @@ If you need to change the parameter name on a specific page, you can leverage th
26292631
public string $query = '';
26302632

26312633
#[LiveProp]
2632-
public ?string $alias = null;
2634+
public string $urlPrefix = '';
26332635

2634-
public function modifyQueryProp(LiveProp $liveProp): LiveProp
2636+
public function modifyQueryProp(LiveProp $liveProp, string $propName): LiveProp
26352637
{
2636-
if ($this->alias) {
2637-
$liveProp = $liveProp->withUrl(new UrlMapping(as: $this->alias));
2638-
}
2639-
return $liveProp;
2638+
return $this->urlPrefix
2639+
? $liveProp->withUrl(new UrlMapping($this->urlPrefix . ucfirst($propName)))
2640+
: $liveProp;
26402641
}
26412642
}
26422643

26432644
.. code-block:: html+twig
26442645

2645-
<twig:SearchModule alias="q" />
2646+
<twig:SearchModule urlPrefix="example" />
2647+
2648+
Now the ``query`` value will appear in the URL like: ``https://my.domain/search?exampleQuery``
26462649

26472650
This way you can also use the component multiple times in the same page and avoid collisions in parameter names:
26482651

26492652
.. code-block:: html+twig
26502653

2651-
<twig:SearchModule alias="q1" />
2652-
<twig:SearchModule alias="q2" />
2654+
<twig:SearchModule urlPrefix="example1" />
2655+
<twig:SearchModule urlPrefix="example2" />
26532656

26542657
Validating the Query Parameter Values
26552658
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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
}

0 commit comments

Comments
 (0)