diff --git a/Datatable/Action/Action.php b/Datatable/Action/Action.php index 91c23008..8c5aef86 100644 --- a/Datatable/Action/Action.php +++ b/Datatable/Action/Action.php @@ -176,7 +176,7 @@ public function configureOptions(OptionsResolver $resolver) $resolver->setAllowedTypes('route', array('null', 'string')); $resolver->setAllowedTypes('route_parameters', array('null', 'array', 'Closure')); $resolver->setAllowedTypes('icon', array('null', 'string')); - $resolver->setAllowedTypes('label', array('null', 'string')); + $resolver->setAllowedTypes('label', array('null', 'string', 'Closure')); $resolver->setAllowedTypes('confirm', 'bool'); $resolver->setAllowedTypes('confirm_message', array('null', 'string')); $resolver->setAllowedTypes('attributes', array('null', 'array', 'Closure')); @@ -290,6 +290,22 @@ public function setLabel($label) return $this; } + /** + * Call label closure. + * + * @param array $row + * + * @return string + */ + public function callLabelClosure(array $row = array()) + { + if ($this->label instanceof Closure) { + return call_user_func($this->label, $row); + } + + return $this->label; + } + /** * Get confirm. * diff --git a/Datatable/Column/ActionColumn.php b/Datatable/Column/ActionColumn.php index 27d32ae3..affca77e 100644 --- a/Datatable/Column/ActionColumn.php +++ b/Datatable/Column/ActionColumn.php @@ -83,6 +83,7 @@ public function renderSingleField(array &$row) $parameters = array(); $attributes = array(); $values = array(); + $labels = array(); /** @var Action $action */ foreach ($this->actions as $actionKey => $action) { @@ -136,12 +137,15 @@ public function renderSingleField(array &$row) $values[$actionKey] = null; } } + + $labels[$actionKey] = $action->callLabelClosure($row); } $row[$this->getIndex()] = $this->twig->render( $this->getCellContentTemplate(), array( 'actions' => $this->actions, + 'labels' => $labels, 'route_parameters' => $parameters, 'attributes' => $attributes, 'values' => $values, diff --git a/Resources/doc/columns.md b/Resources/doc/columns.md index 10578300..3f1927a5 100644 --- a/Resources/doc/columns.md +++ b/Resources/doc/columns.md @@ -434,21 +434,21 @@ A Column to display CRUD action labels or buttons. ### Action options -| Option | Type | Default | Required | Description | -|---------------------|------------------------|---------|----------|-------------| -| route | null or string | null | | The name of the Action route. | -| route_parameters | null, array or Closure | null | | The route parameters. | -| icon | null or string | null | | An icon for the Action. | -| label | null or string | null | | A label for the Action. | -| confirm | bool | false | | Show confirm message if true. | -| confirm_message | null or string | null | | The confirm message. | -| attributes | null, array or Closure | null | | HTML Tag attributes (except 'href' and 'value'). | -| button | bool | false | | Render a button instead of a link. | -| button_value | null or string | null | | The button value. | -| button_value_prefix | bool | false | | Use the Datatable-Name as prefix for the button value. | -| render_if | null or Closure | null | | Render an Action only if conditions are TRUE. | -| start_html | null or string | null | | HTML code before the Tag. | -| end_html | null or string | null | | HTML code after the Tag. | +| Option | Type | Default | Required | Description | +|---------------------|-------------------------|---------|----------|-------------| +| route | null or string | null | | The name of the Action route. | +| route_parameters | null, array or Closure | null | | The route parameters. | +| icon | null or string | null | | An icon for the Action. | +| label | null, string or Closure | null | | A label for the Action. | +| confirm | bool | false | | Show confirm message if true. | +| confirm_message | null or string | null | | The confirm message. | +| attributes | null, array or Closure | null | | HTML Tag attributes (except 'href' and 'value'). | +| button | bool | false | | Render a button instead of a link. | +| button_value | null or string | null | | The button value. | +| button_value_prefix | bool | false | | Use the Datatable-Name as prefix for the button value. | +| render_if | null or Closure | null | | Render an Action only if conditions are TRUE. | +| start_html | null or string | null | | HTML code before the Tag. | +| end_html | null or string | null | | HTML code after the Tag. | ### Example @@ -535,6 +535,16 @@ $this->columnBuilder 'start_html' => '
', 'end_html' => '
', ), + array( + 'route' => 'comments_show', + 'route_parameters' => array( + 'id' => 'id', + ), + 'label' => function($row) { + return sprintf('Show %s comments', '$row['comment_count']); // 'comment_count' may be a virtual column + }, + 'confirm' => false, + ), ), )) ; diff --git a/Resources/views/render/action.html.twig b/Resources/views/render/action.html.twig index 2e7754d5..932d23df 100644 --- a/Resources/views/render/action.html.twig +++ b/Resources/views/render/action.html.twig @@ -8,7 +8,7 @@ #} {% import _self as macros %} -{% macro link_title(action) %} +{% macro link_title(action, label) %} {% if action.label is same as(null) and action.icon is same as(null) %} {% if action.route is not same as(null) %} {{ action.route }} @@ -16,7 +16,7 @@ null {% endif %} {% else %} - {{ action.label }} + {{ label }} {% endif %} {% endmacro %} @@ -65,13 +65,13 @@ {% if action.button is same as(false) %} {{ action.startHtml|raw }}
- {{ macros.link_title(action) }} + {{ macros.link_title(action, labels[actionKey]) }} {{ action.endHtml|raw }} {% else %} {{ action.startHtml|raw }} {{ action.endHtml|raw }} {% endif %}