PHP code example of contao-community-alliance / events-create-options

1. Go to this page and download the library: Download contao-community-alliance/events-create-options library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

contao-community-alliance / events-create-options example snippets


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'),
);

$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';
};

$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
);

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;
	}
}

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);
		}
	),
);