-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Feature/make collection configurable #5719
Changes from all commits
0f47f5e
beddb3e
86a2cf9
c3728a2
2fc73fd
b9f972b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,27 @@ class FormCollection extends AbstractHelper | |
*/ | ||
protected $shouldWrap = true; | ||
|
||
/** | ||
* This is the default wrapper that the collection is wrapped into | ||
* | ||
* @var string | ||
*/ | ||
protected $wrapper = '<fieldset>%2$s%1$s%3$s</fieldset>'; | ||
|
||
/** | ||
* This is the default label-wrapper | ||
* | ||
* @var string | ||
*/ | ||
protected $labelWrapper = '<legend>%s</legend>'; | ||
|
||
/** | ||
* Where shall the template-data be inserted into | ||
* | ||
* @var string | ||
*/ | ||
protected $templateWrapper = '<span data-template="%s"></span>'; | ||
|
||
/** | ||
* The name of the default view helper that is used to render sub elements. | ||
* | ||
|
@@ -98,11 +119,6 @@ public function render(ElementInterface $element) | |
} | ||
} | ||
|
||
// If $templateMarkup is not empty, use it for simplify adding new element in JavaScript | ||
if (!empty($templateMarkup)) { | ||
$markup .= $templateMarkup; | ||
} | ||
|
||
// Every collection is wrapped by a fieldset if needed | ||
if ($this->shouldWrap) { | ||
$label = $element->getLabel(); | ||
|
@@ -116,14 +132,19 @@ public function render(ElementInterface $element) | |
); | ||
} | ||
|
||
$label = $escapeHtmlHelper($label); | ||
|
||
$markup = sprintf( | ||
'<fieldset><legend>%s</legend>%s</fieldset>', | ||
$label, | ||
$markup | ||
$label = sprintf( | ||
$this->labelWrapper, | ||
$escapeHtmlHelper($label) | ||
); | ||
} | ||
$markup = sprintf( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Ocramius This is a BC/Break. Before at this poing only markup.templatemarkup was returned. Now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The point is, you couldn't get a fieldset without a label/legend before that. Now you always get a fieldset - either with or without legend depending on whether a label is set for the fieldset. So it's more predictable what you get. And when you set Agreed, If you see it as a BC-Break, I could add another property containing a default that wraps everything up into nothing (i.e. The further question is, whether it's a BC-Break or a fix of an unintended behaviour. As no tests are failing and no existing tests have been changed I can't say whether it was intended behaviour in the first place or not. For my use-case it was unintended behaviour so I tried to fix it best as I could. |
||
$this->wrapper, | ||
$markup, | ||
$label, | ||
$templateMarkup | ||
); | ||
} else { | ||
$markup .= $templateMarkup; | ||
} | ||
|
||
return $markup; | ||
|
@@ -139,18 +160,20 @@ public function renderTemplate(CollectionElement $collection) | |
{ | ||
$elementHelper = $this->getElementHelper(); | ||
$escapeHtmlAttribHelper = $this->getEscapeHtmlAttrHelper(); | ||
$fieldsetHelper = $this->getFieldsetHelper(); | ||
|
||
$templateMarkup = ''; | ||
|
||
$elementOrFieldset = $collection->getTemplateElement(); | ||
|
||
if ($elementOrFieldset instanceof FieldsetInterface) { | ||
$templateMarkup .= $this->render($elementOrFieldset); | ||
$templateMarkup .= $fieldsetHelper($elementOrFieldset); | ||
} elseif ($elementOrFieldset instanceof ElementInterface) { | ||
$templateMarkup .= $elementHelper($elementOrFieldset); | ||
} | ||
|
||
return sprintf( | ||
'<span data-template="%s"></span>', | ||
$this->templateWrapper, | ||
$escapeHtmlAttribHelper($templateMarkup) | ||
); | ||
} | ||
|
@@ -260,4 +283,95 @@ protected function getFieldsetHelper() | |
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get the wrapper for the collection | ||
* | ||
* @return string | ||
*/ | ||
public function getWrapper() | ||
{ | ||
return $this->wrapper; | ||
} | ||
|
||
/** | ||
* Set the wrapper for this collection | ||
* | ||
* The string given will be passed through sprintf with the following three | ||
* replacements: | ||
* | ||
* 1. The content of the collection | ||
* 2. The label of the collection. If no label is given this will be an empty | ||
* string | ||
* 3. The template span-tag. This might also be an empty string | ||
* | ||
* The preset default is <pre><fieldset>%2$s%1$s%3$s</fieldset></pre> | ||
* | ||
* @param string $wrapper | ||
* | ||
* @return self | ||
*/ | ||
public function setWrapper($wrapper) | ||
{ | ||
$this->wrapper = $wrapper; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set the label-wrapper | ||
* The string will be passed through sprintf with the label as single | ||
* parameter | ||
* This defaults to '<legend>%s</legend>' | ||
* | ||
* @param string $labelWrapper | ||
* | ||
* @return self | ||
*/ | ||
public function setLabelWrapper($labelWrapper) | ||
{ | ||
$this->labelWrapper = $labelWrapper; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get the wrapper for the label | ||
* | ||
* @return string | ||
*/ | ||
public function getLabelWrapper() | ||
{ | ||
return $this->labelWrapper; | ||
} | ||
|
||
/** | ||
* Ge the wrapper for the template | ||
* | ||
* @return string | ||
*/ | ||
public function getTemplateWrapper() | ||
{ | ||
return $this->templateWrapper; | ||
} | ||
|
||
/** | ||
* Set the string where the template will be inserted into | ||
* | ||
* This string will be passed through sprintf and has the template as single | ||
* parameter | ||
* | ||
* THis defaults to '<span data-template="%s"></span>' | ||
* | ||
* @param string $templateWrapper | ||
* | ||
* @return self | ||
*/ | ||
public function setTemplateWrapper($templateWrapper) | ||
{ | ||
$this->templateWrapper = $templateWrapper; | ||
|
||
return $this; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why 2 before 1 and not make it more predictable with 1 before 2 (and change sprintf params)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've ordered the parameters by importance.
As the content of the fieldset seemed to me the most important one. A legend, which possibly might not be existent is placed before the content in HTML-output, but I couldn't see it as important as the content, so I placed it as second. And the third parameter would then be the template-span-tag that might also be empty.