Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/contao-community-alliance/events-create-options
options_callback event and helpers for Contao Open Source CMS
https://github.com/contao-community-alliance/events-create-options
Last synced: about 2 months ago
JSON representation
options_callback event and helpers for Contao Open Source CMS
- Host: GitHub
- URL: https://github.com/contao-community-alliance/events-create-options
- Owner: contao-community-alliance
- Created: 2014-01-01T23:12:22.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2018-06-06T15:46:35.000Z (over 6 years ago)
- Last Synced: 2024-03-25T21:13:47.026Z (10 months ago)
- Language: PHP
- Size: 11.7 KB
- Stars: 0
- Watchers: 10
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Events: Create options
Event and helper classes to provide option_callback's via events.
In your DCA, define the `options_callback` with the factory class `CreateOptionsEventCallbackFactory`.
```php
use ContaoCommunityAlliance\Contao\Events\CreateOptions\CreateOptionsEventCallbackFactory;$GLOBALS['TL_DCA']['tl_foo']['fields']['some_select'] = array(
'inputType' => 'select',
...
'options_callback' => CreateOptionsEventCallbackFactory::createCallback('tl_foo.some_select.create-options'),
);
```Now you can fill the options with an event listener, listening on the event named `tl_foo.some_select.create-options`.
```php
$GLOBALS['TL_EVENTS']['tl_foo.some_select.create-options'][] = function($event) {
$options = $event->getOptions();$options['value1'] = 'label 1';
$options['value2'] = 'label 2';
$options['value3'] = 'label 3';
};
```Manipulate the options with a second event listener is pretty easy.
```php
$GLOBALS['TL_EVENTS']['tl_foo.some_select.create-options'][] = array(
function($event) {
$options = $event->getOptions();// remove a default value
unset($options['value2']);// add a new value
$options['value4'] = 'label 4';
},
-10 // we need a lower priority here, to make sure this listener is triggered after the default listener
);
```See the [event dispatcher documentation](https://github.com/contao-community-alliance/event-dispatcher#listen-on-events)
for more examples how to listen on an event.## Custom event
By default, an event of type `ContaoCommunityAlliance\Contao\Events\CreateOptions\CreateOptionsEvent` is used.
If you want your own event type, you can pass the class or a factory method as second parameter to `CreateOptionsEventCallbackFactory::createCallback()`.First you need to write your own create-options event class.
```php
class MyCreateOptionsEvent extends \ContaoCommunityAlliance\Contao\Events\CreateOptions\CreateOptionsEvent
{
protected $additionalData;function __construct($additionalData, \DataContainer $dataContainer, \ArrayObject $options = null)
{
parent::__construct($dataContainer, $options);
$this->additionalData = $additionalData;
}public function getAdditionalData()
{
return $this->additionalData;
}
}
```Then you need to add your factory to `CreateOptionsEventCallbackFactory::createCallback()`.
```php
use ContaoCommunityAlliance\Contao\Events\CreateOptions\CreateOptionsEventCallbackFactory;$GLOBALS['TL_DCA']['tl_foo']['fields']['some_select'] = array(
'inputType' => 'select',
...
'options_callback' => CreateOptionsEventCallbackFactory::createCallback(
'tl_foo.some_select.create-options',
function($dataContainer) {
return new \MyCreateOptionsEvent(array('some' => 'value'), $dc);
}
),
);
```