[FLOW3-general] How to set/get/show M:N relations using checkboxes instead of selectbox?
Stephen Bungert
stephenbungert at yahoo.de
Fri Mar 8 20:12:15 CET 2013
Hi.
I needed to do this to.
I have books and these ccan be related to genres.
I render a list of genres with checboxes. I created this viewHelper to
render the checkboxes checked attribute:
Maybe not the best way, but it may help you too.
Stephen.
<?php
namespace Gamebook\Gamebook\ViewHelpers\Form;
/* *
* This script belongs to the TYPO3 Flow package "Gamebook.Gamebook". *
* *
*
*/
use TYPO3\Flow\Annotations as Flow;
/**
* This ViewHelper decides if a checkbox/radio should be checked.
*
* = Examples =
*
* <code title="Example">
* <f:form.checkbox checked="{gb:form.checked(record: genre,
selectedRecords: book.genres)}" name="genresToAttach[genre]"
value="{genre}" />
* </code>
* <output>
* <input type="checkbox" checked="checked"
value="6fa1d167-8ab6-4dd6-8099-202dd951fc3c"
name="genresToAttach[genre][__identity]">
* </output>
*/
class CheckedViewHelper extends
\TYPO3\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
* @Flow\Inject
* @var \TYPO3\Flow\Persistence\PersistenceManagerInterface
*/
protected $persistenceManager;
/**
* Returns the attribute value for the 'checked' attribute in radio and
checkboxes.
*
* @param object $record The record to check against to see if it
should be checked or not.
* @param \Doctrine\ORM\PersistentCollection $selectedRecords All the
selected records that the parent record has selected.
* @return string $checkedState The value for the attribute 'checked'
in a checkbox or radio.
*/
public function render($record, \Doctrine\ORM\PersistentCollection
$selectedRecords) {
$checkedState = '';
if (is_object($record) && !empty($selectedRecords))
{
$recordIdentity =
$this->persistenceManager->getIdentifierByObject($record);
foreach ($selectedRecords as $possibleMatch) {
if ($recordIdentity ==
$this->persistenceManager->getIdentifierByObject($possibleMatch))
{
$checkedState = 'checked';
break;
}
}
}
return $checkedState;
}
}
?>
More information about the FLOW3-general
mailing list