Skip to content

[LiveComponent] LiveProp: Pass the property name as second parameter of the modifier callback #2744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/LiveComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.26.0

- `LiveProp`: Pass the property name as second parameter of the `modifier` callable

## 2.25.0

- Add support for [Symfony UID](https://symfony.com/doc/current/components/uid.html) hydration/dehydration
Expand Down
41 changes: 41 additions & 0 deletions src/LiveComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2625,6 +2625,47 @@ This way you can also use the component multiple times in the same page and avoi
<twig:SearchModule alias="q1" />
<twig:SearchModule alias="q2" />

.. versionadded:: 2.26

The property name is passed into the modifier function since LiveComponents 2.26.

The ``modifier`` function can also take the name of the property as a secondary parameter.
It can be used to perform more generic operations inside of the modifier that can be re-used for multiple props::

abstract class AbstractSearchModule
{
#[LiveProp(writable: true, url: true, modifier: 'modifyQueryProp')]
public string $query = '';

protected string $urlPrefix = '';

public function modifyQueryProp(LiveProp $liveProp, string $propName): LiveProp
{
if ($this->urlPrefix) {
return $liveProp->withUrl(new UrlMapping(as: $this->urlPrefix.'-'.$propName));
}
return $liveProp;
}
}

#[AsLiveComponent]
class ImportantSearchModule extends AbstractSearchModule
{
}

#[AsLiveComponent]
class SecondarySearchModule extends AbstractSearchModule
{
protected string $urlPrefix = 'secondary';
}

.. code-block:: html+twig

<twig:ImportantSearchModule />
<twig:SecondarySearchModule />

The ``query`` value will appear in the URL like ``/search?query=my+important+query&secondary-query=my+secondary+query``.

Validating the Query Parameter Values
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion src/LiveComponent/src/Metadata/LivePropMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function withModifier(object $component): self
throw new \LogicException(\sprintf('Method "%s::%s()" given in LiveProp "modifier" does not exist.', $component::class, $modifier));
}

$modifiedLiveProp = $component->{$modifier}($this->liveProp);
$modifiedLiveProp = $component->{$modifier}($this->liveProp, $this->getName());
if (!$modifiedLiveProp instanceof LiveProp) {
throw new \LogicException(\sprintf('Method "%s::%s()" should return an instance of "%s" (given: "%s").', $component::class, $modifier, LiveProp::class, get_debug_type($modifiedLiveProp)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testWithModifier()
$component
->expects($this->once())
->method('modifyProp')
->with($liveProp)
->with($liveProp, 'propWithModifier')
->willReturn($liveProp->withFieldName('customField'));

$livePropMetadata = $livePropMetadata->withModifier($component);
Expand Down
Loading