Skip to content

Instantly share code, notes, and snippets.

@jesperveldhuizen
Last active January 26, 2019 09:36
Show Gist options
  • Save jesperveldhuizen/785d902c9c789c664c0780acf9c7600d to your computer and use it in GitHub Desktop.
Save jesperveldhuizen/785d902c9c789c664c0780acf9c7600d to your computer and use it in GitHub Desktop.
Sulu custom dropdown content ty-e
<?php
namespace AppBundle\Sulu\Content\Type;
use Sulu\Component\Content\Compat\PropertyInterface;
use Sulu\Component\Content\Compat\PropertyParameter;
use Sulu\Component\Content\SimpleContentType;
/**
* ContentType for a dropdown and selecting one option.
* Based on Sulu's own SingleSelect
*/
class Dropdown extends SimpleContentType
{
/**
* @var string
*/
private $template;
public function __construct($template)
{
parent::__construct('Dropdown', '');
$this->template = $template;
}
/**
* {@inheritdoc}
*/
public function getTemplate()
{
return $this->template;
}
/**
* {@inheritdoc}
*/
public function getDefaultParams(PropertyInterface $property = null)
{
return [
'values' => new PropertyParameter('values', [], 'collection'),
];
}
}
Template File:
<select name="{{ id|raw }}"
class="preview-update trigger-save-button form-element"
data-property='{{ property|json_encode }}'
data-mapper-property="{{ property.name }}">
{% for value in params.values.value %}
{% set elementId %}{{ id|raw }}_{{ loop.index }}{% endset %}
<option name="{{ id|raw }}"
value="{{ value.name }}">
{{ value.getTitle(userLocale) }}
</option>
{% endfor %}
</select>
Add to services:
<service id="app.sulu.content.type.dropdown" class="%app.sulu.content.type.dropdown.class%">
<argument>@AppBundle/Sulu/Content/Type/dropdown.html.twig</argument>
<tag name="sulu.content.type" alias="dropdown"/>
</service>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment