Skip to content

Commit

Permalink
Add ability to use param in breadcrumb title
Browse files Browse the repository at this point in the history
Now we can add parameters in the title as:
    /**
     * @breadcrumb("{book}")
     */
    public function showAction(Book $book) { }
  • Loading branch information
Floran Brutel authored and Floran Brutel committed Dec 13, 2013
1 parent d74f73f commit a61e8d9
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
27 changes: 27 additions & 0 deletions BreadcrumbTrail/Trail.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public function getTemplate()
* @param Boolean $routeAbsolute Whether to generate an absolute URL
* @param integer $position Position of the breadcrumb (default = 0)
* @param mixed $attributes Additional attributes for the breadcrumb
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @return self
*/
public function add($breadcrumb_or_title, $routeName = null, $routeParameters = array(), $routeAbsolute = false, $position = 0, $attributes = array())
Expand All @@ -87,6 +89,31 @@ public function add($breadcrumb_or_title, $routeName = null, $routeParameters =
$request = $this->container->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE);

if ($request !== null) {
preg_match_all('#\{(?P<variable>\w+).?(?P<function>\w*):?(?P<parameters>(\w|,| )*)\}#', $breadcrumb_or_title, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
foreach ($matches as $match) {
$varName = $match['variable'][0];
$functionName = $match['function'][0];
$parameters = explode(',', $match['parameters'][0]);

if($request->attributes->has($varName)) {
$object = $request->attributes->get($varName);

if(empty($functionName)) {
$objectValue = (string) $object;
}
elseif(is_callable(array($object, $fullFunctionName = 'get'.$functionName))
|| is_callable(array($object, $fullFunctionName = 'has'.$functionName))
|| is_callable(array($object, $fullFunctionName = 'is'.$functionName))) {
$objectValue = call_user_func_array(array($object, $fullFunctionName),$parameters);
}
else {
throw new \RuntimeException(sprintf('Function "%s" not found.', $functionName));
}

$breadcrumb_or_title = str_replace($match[0][0], $objectValue, $breadcrumb_or_title);
}
}

foreach ($routeParameters as $key => $value) {
if (is_numeric($key)) {
$routeParameters[$value] = $request->get($value);
Expand Down
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,92 @@ class MyController extends Controller
*/
}
}
```

#### Title with @ParamConverter

The [@ParamConverter](http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#annotation-configuration) of the SensioFrameworkExtraBundle convert request parameters like 'id' to objects then injected as controller method arguments:

It is possible to display values ​​of these objects in the breadcrumb.


```
...
use APY\BreadcrumbTrailBundle\Annotation\Breadcrumb;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
/**
* @Route("/book/{id}")
* @Breadcrumb("Books")
* @Breadcrumb("{book}")
*/
public function aShowAction(Book $book)
{
/*
This action will show the following breacrumb trail:
Books > result of __toString method of $book's Object
*/
}
/**
* @Route("/book/{id}")
* @Breadcrumb("Books")
* @Breadcrumb("{book.title}")
*/
public function bShowAction(Book $book)
{
/*
This action will show the following breacrumb trail:
Books > result of getTitle method of $book's Object
The bundle tries to call the methods : getTitle, hasTitle or isTitle
*/
}
/**
* @Route("/book/{id}")
* @Breadcrumb("Books")
* @Breadcrumb("{book.title:argument1}")
*/
public function cShowAction(Book $book)
{
/*
This action will show the following breacrumb trail:
Books > result of getTitle('argument1') method of $book's Object
*/
}
/**
* @Route("/book/{id}")
* @Breadcrumb("Books")
* @Breadcrumb("{book.title:argument1: argument2}")
*/
public function dShowAction(Book $book)
{
/*
This action will show the following breacrumb trail:
Books > result of getTitle('argument1', ' argument2') method of $book's Object
*/
}
```

---
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</service>

<service id="apy_breadcrumb_trail.annotation.listener" class="%apy_breadcrumb_trail.annotation.listener.class%">
<tag name="kernel.event_listener" event="kernel.controller" method="onKernelController" />
<tag name="kernel.event_listener" event="kernel.controller" method="onKernelController" priority="-1" />
<argument type="service" id="annotation_reader" />
<argument type="service" id="apy_breadcrumb_trail" />
</service>
Expand Down

0 comments on commit a61e8d9

Please sign in to comment.